-
May 21st, 2022, 10:43 PM
#1
Thread Starter
Addicted Member
[RESOLVED] "The semaphore timeout period has expired", Program Crashes
Banging my head against the wall with this issue. My program uses a serial COM port to connect to an external HC-05 Bluetooth module. The connection and data transfer works great! Now, if the external Bluetooth module is turned off, I get the following "Semaphore Timeout" error and the program crashes after pressing the "Connect" button. There is no way for me to keep the program open.
I know that the program is trying to connect to a nonexistent (turned off) Bluetooth device. How do I prevent the program from crashing and closing by itself. The BT device is paired with the PC even though it is off. How can I manage the error without the program crashing? The program crashes with the line SerialPort2.Open().
Message: The semaphore timeout period has expired.
StackTrace: at System.IO.Ports.InternalResources.WinIOError(Int32 errorCode, String str)
at System.IO.Ports.SerialStream..ctor(String portName, Int32 baudRate, Parity parity, Int32 dataBits, StopBits stopBits, Int32 readTimeout, Int32 writeTimeout, Handshake handshake, Boolean dtrEnable, Boolean rtsEnable, Boolean discardNull, Byte parityReplace)
at System.IO.Ports.SerialPort.Open()
at BT.MainForm.btnDataConnect_Click(Object sender, EventArgs e) in C:\Users\user\Desktop\MyTestFolder\BT.vb:line 8448
Date/Time: 5/21/2022 11:19:07 PM
Code:
Private Sub btnDataConnect_Click(sender As Object, e As EventArgs) Handles btnDataConnect.Click
Try
If Not SerialPort2.IsOpen Then
SerialPort2.Open()
End If
'Catch ex As System.IO.IOException
'SerialPort2.Dispose()
'SerialPort2.Close() 'CLOSING PORT HERE DOESN'T HELP
Catch ex As Exception
txbReceivedData.Text = txbReceivedData.Text & vbCrLf & "HC-05 MODULE BLUETOOTH CONNECTION NOT FOUND..."
Dim el As New ErrorLogger
el.WriteToErrorLog(ex.Message, ex.StackTrace, "Error")
End Try
End Sub
-
May 22nd, 2022, 12:04 AM
#2
Re: "The semaphore timeout period has expired", Program Crashes
You say that the program crashes. Do you mean that it doesn't hit your Catch block? Or that it does hit your Catch block but a further exception is thrown?
Last edited by jmcilhinney; May 22nd, 2022 at 12:07 AM.
-
May 22nd, 2022, 12:33 AM
#3
Thread Starter
Addicted Member
Re: "The semaphore timeout period has expired", Program Crashes
Originally Posted by jmcilhinney
You say that the program crashes. Do you mean that it doesn't hit your Catch block? Or that it does hit your Catch block but a further exception is thrown?
It reaches the Catch block because it writes the error to a text file using WriteToErrorLog. What I mean by crashing is that it closes by itself. The error I get is shown in post 1.
-
May 22nd, 2022, 12:42 AM
#4
Re: "The semaphore timeout period has expired", Program Crashes
So when you run it in the debugger, what happens after it goes through that Catch block?
-
May 22nd, 2022, 12:45 AM
#5
Re: "The semaphore timeout period has expired", Program Crashes
You ought to also be handling the UnhandledException event of the application - every VB WinForms app ought to do that. If another exception is being thrown, you'll hear about it there. Given that it's talking about a Semaphore, there's every possibility that the exception is being thrown on a different thread - the SerialPort does raise its Data Available event on a different thread, after all - so your code could not catch that exception. If you can catch it in the UnhandledException event handler then you can explicitly prevent the app from closing there.
-
May 22nd, 2022, 03:09 PM
#6
Thread Starter
Addicted Member
Re: "The semaphore timeout period has expired", Program Crashes
Jmcilhinney, you definitely pointed me in the right direction. I was getting the semaphore error that pointed to the SerialPort2.Open() line. This line is in a Try-Catch block. When I ran the program in debugger mode I got the error "SerialPort2 is closed" pointing to the unhandled code shown below. I didn't think this code would create a problem when the connection to the COM port is not successful. After commenting this line out, the problem was fixed.
Code:
If SerialPort2.BytesToRead = 0 Then
'txbReceivedData.Text = txbReceivedData.Text & vbCrLf & "TRY USING A DIFFERENT COM PORT"
End If
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|