[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Serial port usb

Posted on 2007-09-30
9
Medium Priority
?
1,170 Views
Last Modified: 2013-11-26
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
Comment
Question by:PiedBird
9 Comments
 
LVL 14

Expert Comment

by:Jai S
ID: 19988850
is it possible for you to have any ERROR HANDLER...
like TRY CATCH FINALLY...
0
 

Author Comment

by:PiedBird
ID: 19988968
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
 
LVL 10

Expert Comment

by:ericwong27
ID: 19989539

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:PiedBird
ID: 19989580
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
 
LVL 9

Expert Comment

by:JonMny
ID: 19990753
are you using  CheckForIllegalCrossThreadCalls = False
in your form load?


What does the stack trace look like when the error happens
0
 

Author Comment

by:PiedBird
ID: 19994166
1. No
2. Don't know

Can you explain more about illegal thread calls?
0
 

Author Comment

by:PiedBird
ID: 19994473
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
 
LVL 9

Accepted Solution

by:
JonMny earned 1500 total points
ID: 19995872
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
 

Author Comment

by:PiedBird
ID: 20010425
Thanks
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

826 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question