以2进制接收转换为16进制字符串来处理数据. buffer()作为动态Byte字节数组处理接收的2进制字节流. Option Explicit Dim strData As StringPrivate Sub MsComm1_OnComm() Dim buffer() As Byte Dim i As Integer Select Case MsComm1.CommEvent Case comEvReceive buffer = MsComm1.Input For i = 0 To UBound(inByte) If Len(Hex(buffer(i))) = 1 Then strData = strData & "0" & Hex(buffer(i)) Else strData = strData & Hex(buffer(i)) End If Next If Mid(strData,1,4) = "FF00" Then Text1.Text = strData ' strData = "" End If End Select End SubPrivate Sub Form_Load() With MsComm1 .CommPort = 1 .Settings = "9600,n,8,1" .InBufferCount = 0 .InputLen = 0 .RThreshold = 1 .InputMode = comInputModeBinary '以2进制接收 .PortOpen = True End With Text1 = "" End Sub
Buffer 是一个缓冲区。直接读取数据。这里有例子:
http://www.phpchinaz.cn/c/VB_Basic/51_5101_1236005626.html
请参阅本人BLOG
"MSComm 控件的Input 属性及InputMode 属性探究"
buffer()作为动态Byte字节数组处理接收的2进制字节流.
Option Explicit
Dim strData As StringPrivate Sub MsComm1_OnComm()
Dim buffer() As Byte
Dim i As Integer
Select Case MsComm1.CommEvent
Case comEvReceive
buffer = MsComm1.Input
For i = 0 To UBound(inByte)
If Len(Hex(buffer(i))) = 1 Then
strData = strData & "0" & Hex(buffer(i))
Else
strData = strData & Hex(buffer(i))
End If
Next
If Mid(strData,1,4) = "FF00" Then
Text1.Text = strData '
strData = ""
End If
End Select
End SubPrivate Sub Form_Load()
With MsComm1
.CommPort = 1
.Settings = "9600,n,8,1"
.InBufferCount = 0
.InputLen = 0
.RThreshold = 1
.InputMode = comInputModeBinary '以2进制接收
.PortOpen = True
End With
Text1 = ""
End Sub