PiedBird
asked on
Serial port usb
My vb windows app listens for data on a serial port. The port is provided by usb to serial harware. The program works fine unless the usb to serial cable is removed whilst the port is open. If that happens we sort of crash. How do I tell my program not to worry if somebody removes the port - it means they were finished listening anyway?
ASKER
but mate, where would I put it?
I use this code (without proper understanding I confess):
Private Sub SerialPort1_DataReceived(B yVal sender As Object, _
ByVal e As System.IO.Ports.SerialData ReceivedEv entArgs) Handles SerialPort1.DataReceived
Try
invkDisplay(SerialPort1.Re adLine())
Catch ex As Exception
ex.ToString()
End Try
End Sub
Private Delegate Sub DisplayDelegate(ByVal Text As String)
Private Sub invkDisplay(ByVal Text As String)
Dim disp As New DisplayDelegate( _
AddressOf SerialDataReceived)
Dim ar() As Object = {Text}
' call the client form on the UI thread
' to update the display
Me.BeginInvoke(disp, ar)
End Sub
From what I can see my code isn't active until serialport1 (visual basic object) receives data. The error occurs when serialport1 (hardware) is disconnected from the computer.
I use this code (without proper understanding I confess):
Private Sub SerialPort1_DataReceived(B
ByVal e As System.IO.Ports.SerialData
Try
invkDisplay(SerialPort1.Re
Catch ex As Exception
ex.ToString()
End Try
End Sub
Private Delegate Sub DisplayDelegate(ByVal Text As String)
Private Sub invkDisplay(ByVal Text As String)
Dim disp As New DisplayDelegate( _
AddressOf SerialDataReceived)
Dim ar() As Object = {Text}
' call the client form on the UI thread
' to update the display
Me.BeginInvoke(disp, ar)
End Sub
From what I can see my code isn't active until serialport1 (visual basic object) receives data. The error occurs when serialport1 (hardware) is disconnected from the computer.
How about this?
Try
Using comPort As SerialPort = My.Computer.Ports.OpenSeri
Do
Dim line As String = comPort.ReadLine()
If line Is Nothing Then
Exit Do
Else
buffer.AppendLine(line)
End If
Loop
End Using
Catch ex As Exception
MessageBox.Show(ex.Message
End Try
ASKER
I don't think that helps me because the serial data is sent from external device at an unpredictable time. My program keeps the port open to listen for each package of data.
are you using CheckForIllegalCrossThread Calls = False
in your form load?
What does the stack trace look like when the error happens
in your form load?
What does the stack trace look like when the error happens
ASKER
1. No
2. Don't know
Can you explain more about illegal thread calls?
2. Don't know
Can you explain more about illegal thread calls?
ASKER
Here is some stack trace JM. Does that tell us the error arrives when the app tries to dispose of the port?
denied.</Message><StackTra ce> at System.IO.Ports.InternalRe sources.Wi nIOError(I nt32 errorCode, String str)
at System.IO.Ports.SerialStre am.Dispose (Boolean disposing)
at System.IO.Ports.SerialStre am.Finaliz e()</Stack Trace><Exc eptionStri ng>System. Unauthoriz edAccessEx ception: Access to the port is denied.
at System.IO.Ports.InternalRe sources.Wi nIOError(I nt32 errorCode, String str)
at System.IO.Ports.SerialStre am.Dispose (Boolean disposing)
at System.IO.Ports.SerialStre am.Finaliz e()</Excep tionString ></Excepti on></Trace Record>
denied.</Message><StackTra
at System.IO.Ports.SerialStre
at System.IO.Ports.SerialStre
at System.IO.Ports.InternalRe
at System.IO.Ports.SerialStre
at System.IO.Ports.SerialStre
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks
like TRY CATCH FINALLY...