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.