Link to home
Start Free TrialLog in
Avatar of PiedBird
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?
Avatar of Jai S
Jai S
Flag of India image

is it possible for you to have any ERROR HANDLER...
like TRY CATCH FINALLY...
Avatar of PiedBird
PiedBird

ASKER

but mate, where would I put it?
I use this code (without proper understanding I confess):

    Private Sub SerialPort1_DataReceived(ByVal sender As Object, _
    ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
        Try
            invkDisplay(SerialPort1.ReadLine())
        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.

Avatar of ericwong27

How about this?

Try
            Using comPort As SerialPort = My.Computer.Ports.OpenSerialPort("COM1")
                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
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  CheckForIllegalCrossThreadCalls = False
in your form load?


What does the stack trace look like when the error happens
1. No
2. Don't know

Can you explain more about illegal thread calls?
Here is some stack trace JM. Does that tell us the error arrives when the app tries to dispose of the port?

denied.</Message><StackTrace>   at System.IO.Ports.InternalResources.WinIOError(Int32 errorCode, String str)
   at System.IO.Ports.SerialStream.Dispose(Boolean disposing)
   at System.IO.Ports.SerialStream.Finalize()</StackTrace><ExceptionString>System.UnauthorizedAccessException: Access to the port is denied.
   at System.IO.Ports.InternalResources.WinIOError(Int32 errorCode, String str)
   at System.IO.Ports.SerialStream.Dispose(Boolean disposing)
   at System.IO.Ports.SerialStream.Finalize()</ExceptionString></Exception></TraceRecord>
ASKER CERTIFIED SOLUTION
Avatar of JonMny
JonMny

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks