• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1194
  • Last Modified:

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?
0
PiedBird
Asked:
PiedBird
1 Solution
 
Jai STech ArchCommented:
is it possible for you to have any ERROR HANDLER...
like TRY CATCH FINALLY...
0
 
PiedBirdAuthor Commented:
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.

0
 
ericwong27Commented:

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
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
PiedBirdAuthor Commented:
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.
0
 
JonMnyCommented:
are you using  CheckForIllegalCrossThreadCalls = False
in your form load?


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

Can you explain more about illegal thread calls?
0
 
PiedBirdAuthor Commented:
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>
0
 
JonMnyCommented:
add this to the app.config file

<?xml version ="1.0"?><configuration>   <runtime>      <legacyUnhandledExceptionPolicy enabled="1"/>   </runtime></configuration>


see this link for more details
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1338404&SiteID=1
0
 
PiedBirdAuthor Commented:
Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now