Serial Port thread

  • Thread starter Thread starter Denis R Charron
  • Start date Start date
D

Denis R Charron

When the serial port is frozen the "Try... Catch" doesn't get the error
because the serial port is using a separate thread.

Is there a way to have the serial thread to communicate with my application
thread?


This is my code:

Try
SerialPort1.Close()
SerialPort1.Dispose()
SerialPort1 = Nothing
System.GC.Collect()
SerialPort1 = New System.IO.Ports.SerialPort
Catch ex As Exception
MessageBox.Show("Error trying to reset the port", "Freelink",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation,
MessageBoxDefaultButton.Button1)
End Try
 
Which code line is "forzen"? I don't see exactly what the problem is,
likely becasue this isn't all of the code. And where is this "other thread"
you speak of?


--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Giving back to the embedded community
http://community.OpenNETCF.com
 
I don't understand what you are trying to do. It looks like you already
have opened a serial port, you are trying to close it, then reopen it? Why?
I don't seen anything that has much to do with threading in your post. What
you might do (IMO):

Private WithEvents SerialPort1 As New System.IO.Ports.SerialPort
'class-level code

'form code

With SerialPort1
.BaudRate = 9600 'or some other speed
.RTSEnable = True
.ComPort = "Com4" 'or, whatever
'.Handshaking = somevalueifneeded
If .IsOpen() = False Then
Try
.Open()
Catch Ex As Exception
MsgBox (ex.ToString)
End Try
If .IsOpen = True Then
EveryThingIsOk() 'perhaps
Else
PerhapsTheMsgBoxSaysWhyNot() 'etc.
End If
End If
End With

Don't close the port until you exit the application (IMO, again). Then

If SerialPort1.IsOpen() Then SerialPort.Close()

I have working example code in my book (see below) that cover a number of
scenarios -- something may be appropriate there.

If you tell use more about what you really need to do, we might be able to
offer more.

Dick

--
Richard Grier (Microsoft MVP - Visual Basic) Hard & Software 12962 West
Louisiana Avenue Lakewood, CO 80228 303-986-2179 (voice) Homepage:
www.hardandsoftware.net Author of Visual Basic Programmer's Guide to Serial
Communications, 4th Edition ISBN 1-890422-28-2 (391 pages) published July
2004, Revised July 2006.
 
Hello,

I can tell you one case where closing and reopening of a already closed
port is necessary: HP rx1950 device after a reset. The RS232 port only
sends garbage on the first time you open it. ActiveSync must handle this
in this way as it has no trouble with it. So HP came away by saying: "we
don't support custommer written apps". Great! ;-)

Greetings

Markus
 
Back
Top