Public Sub openPort()
Dim ioPort As Short = 8
inoutfileHandler = CreateFile _
("COM" & ioPort & ":", _
&HC0000000, 0, 0, 3, 0, 0)
pdcb = New dcb
'设置波特率
GetCommState(inoutfileHandler, pdcb)
pdcb.BaudRate.Parse("9600")
SetCommState(inoutfileHandler, pdcb)
stopThread = False
t1 = New Threading.Thread _
(AddressOf receiveLoop)
t1.Start()
Label1.Text = "打开成功!"
End Sub
Public Sub displayReceivedMessage(ByVal str As String)
If str.Length > 0 Then
TextBox1.Text += str
End If
End Sub
Public Function Send(ByVal message As String) As Integer
Dim value As String = message & vbCrLf
Dim retCode As Integer = WriteFile _
(inoutfileHandler, _
stringToByteArray(value), _
value.Length(), _
numReadWrite, _
0)
writefile(
Return retCode
End Function
Public Sub closePort()
stopThread = True '结束线程
CloseHandle(inoutfileHandler)
End Sub
Public Delegate Sub myDelegate(ByVal str As String)
Public Function stringToByteArray(ByVal str As String) As Byte()