等候呗,等5秒,直到读取到数据,如果是多次接收的问题,你可以判断接收的字符长度嘛。
Ports.Add("COM3")
With Ports("COM3")
.Close()
.Parity = Parity.None
.Handshake = Handshake.None
.StopBits = StopBits.One
.BaudRate = 9600
.DataBits = 8
.ReadBufferSize() = 4096
.WriteBufferSize() = 2048
.Open
End With
Dim b_sent() As Byte = {&H01, &H03, &H00, &H00, &H00, &H01, &H84, &H0A}
Ports("COM3").Write(b_sent, 0, b_sent.length)
Dim s As String
Dim s1 As String
Dim cnt As Integer = 0
Dim sdt As Date = Date.Now
Do While Date.Now < sdt.AddSeconds(5) '等到5秒
cnt = Ports("COM3").BytesToRead
If cnt > 0 Then Exit Do '或者读取到数据
application.doevents
Loop
If cnt > 0 Then '缓冲区是否有数据
Dim Val(cnt - 1) As Byte
Ports("COM3").Read(val,0,cnt)
Dim i As Integer
For i = 0 To cnt-1
s1 = Val(i)
If s1.Length = 1 Then
s = s & 0 & DecToHex(Val(i))
Else
s = s & DecToHex(Val(i))
End If
Next
End If
Output.Show(s)