Question Try, Catch, Finally - Problem

mrjlaunch

New member
Joined
Jan 25, 2013
Messages
4
Programming Experience
Beginner
Hi I'm trying to write data to a serial port however I seem to be constantly getting the "catch" situation occurring when I call this code:


VB.NET:
 [COLOR=green]'Send Data on Serial Port[/COLOR]
    [COLOR=#0000ff]Sub[/COLOR] SendSerialData([COLOR=#0000ff]ByVal[/COLOR] Description [COLOR=#0000ff]As String[/COLOR], [COLOR=#0000ff]ByVal[/COLOR] output_data [COLOR=#0000ff]As String[/COLOR]) [COLOR=#008000]'Send strings to ComPort[/COLOR]
        [COLOR=#0000ff]Using[/COLOR] Com_Data [COLOR=#0000ff]As [/COLOR]IO.Ports.[COLOR=#008080]SerialPort[/COLOR] =
            [COLOR=#0000ff]My[/COLOR].Computer.Ports.OpenSerialPort(Com_Port, Com_Baud, IO.Ports.[COLOR=#008080]Parity[/COLOR].None, 8, IO.Ports.[COLOR=#008080]StopBits[/COLOR].One) [COLOR=#008000]'Open ComPort[/COLOR]
            [COLOR=#008000]'Send data to ComPort[/COLOR]
            [COLOR=#0000ff]With[/COLOR] Com_Data
                .WriteTimeout = 5000
                .DtrEnable = [COLOR=#0000ff]False[/COLOR]
            [COLOR=#0000ff]End With[/COLOR]
            [COLOR=#0000ff]Try[/COLOR]
                Com_Data.WriteLine([COLOR=#b22222]"Hello world"[/COLOR]) [COLOR=#008000]'Send string and New Line[/COLOR]
                Com_Data.DiscardInBuffer()
           [COLOR=#0000ff] Catch[/COLOR] ex [COLOR=#0000ff]As[/COLOR] [COLOR=#008080]TimeoutException[/COLOR]
                Recieved_String &= [COLOR=#b22222]"Timed Out!"[/COLOR] [COLOR=#008000]'If timeout occurs set received string as "Timed out"[/COLOR]
            [COLOR=#0000ff]Finally[/COLOR]
                Com_Data.Close() [COLOR=#008000]'Close ComPort[/COLOR]
            [COLOR=#0000ff]End Try
[/COLOR]        [COLOR=#0000ff]End Using[/COLOR][COLOR=#0000ff]
[/COLOR]    [COLOR=#0000ff]End Sub[/COLOR]

The text "Hello world" is indeed written to the serial port however I always see "Timed Out!" on the screen and any other code I insert after "Com_Data.WriteLine" is ignored, so I presume .WriteLine is causing me grief?
 
Last edited:

Herman

Well-known member
Joined
Oct 18, 2011
Messages
882
Location
Montreal, QC, CA
Programming Experience
10+
Well for one thing you are missing a End Using, but I am guessing this is from the transcription.

Try pressing alt-ctrl-e inside visual studio, and set all exceptions to "Throw", then run. It should tell you which unhandled exception is causing you pain.
 

mrjlaunch

New member
Joined
Jan 25, 2013
Messages
4
Programming Experience
Beginner
Thanks Herman,

I found the problem. I was using a virtual serial port bridged to another virtual serial port (via software) so I could interact with the program using HyperTerminal.
Once I swapped over to a real serial port the issue went away.
 
Top Bottom