关于SerialPort的一个问题
下是我的代码
Private Sub SerialPortYKQ1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPortYKQ1.DataReceived
If closingS = True Then Exit Sub
Try
Sleep(100)
listeningS = True
Dim bytesInput() As Byte
Dim intInputLen As Integer
intInputLen = SerialPortYKQ1.BytesToRead
If intInputLen >= 2 Then
ReDim bytesInput(intInputLen - 1)
SerialPortYKQ1.Read(bytesInput, 0, intInputLen)
myfunction(bytesInput, intInputLen)
End If
Catch ex As Exception
BLL.GetIni.WriteLogText(ex.Message)
listeningS = False
Finally
listeningS = False
End Try
End Sub
Delegate Sub _dlmyfunction(ByVal bytesRead() As Byte, ByVal intInputLen As Integer)
Private Sub myfunction(ByVal bytesRead() As Byte, ByVal intInputLen As Integer)
Dim d As New _dlmyfunction(AddressOf ShowSB)
Me.Invoke(d, bytesRead, intInputLen)
End Sub
Private Sub ShowSB(ByVal bytesRead() As Byte, ByVal intInputLen As Integer)
Dim i As Integer = 0
Dim SaS As String = ""
Dim flagPostion1 As Integer = 0
Dim flagpostion2 As Integer = 0
For i = 1 To intInputLen Step 1
SaS = SaS & "," & Hex(bytesRead(i - 1))
Next i
flagPostion1 = InStr(SaS, "AA,55")
If flagPostion1 > 0 Then
bl_TongXun = True
Else
bl_TongXun = False
End If
flagpostion2 = InStr(SaS, "5A,5A")
If flagpostion2 > 0 Then
ChangeDataFromSB(bytesRead, intInputLen)
End If
End Sub
Private Sub ChangeDataFromSB(ByVal bytesInput() As Byte, ByVal intInputLen As Integer)
SendAllPointStatusBefore(PointStatus, Points)'刷新状态
If bl_TongXun = False Then
SendAllPointStatusBefore(PointStatus, Points)
End If
End Sub
Public sub SendAllPointStatusBefore(ByVal PointStatus() As String, ByVal Points As Integer)
Dim Js(15) As Byte
SerialPortYKQ1.Write(Js, 0, 16)
Sleep(200)
End sub