Private Sub MSComm1_OnComm()
Dim BytReceived() As Byte
Dim strBuff As String
Dim i As Integer
Select Case MSComm1.CommEvent'事件发生
Case 2
MSComm1.InputLen = 0 '读入缓冲区全部内容
strBuff = MSComm1.Input '读入到缓冲区
If MSComm1.InputMode = comInputModeBinary Then
BytReceived() = strBuff '如果是二进制接收模式则进行数据处理,否则直接显示字符串
For i = 0 To UBound(BytReceived)
If Len(Hex(BytReceived(i))) = 1 Then
strData = strData & "0" & Hex(BytReceived(i)) & " "
'如果只有一个字符,则前补0,如F显示0F,最后补空格
Else '方便显示观察如: 00 0F FE
strData = strData & Hex(BytReceived(i)) & " "
End If
Next
Text1 = strData
Call hexSend2 ' '发送一个16进制
strData = ""
Else
Text1 = Text1 & strBuff
If MSComm2.PortOpen = False Then
MsgBox "请打开串口B"
End If
On Error GoTo uerror3
MSComm2.Output = strBuff
Label14.Caption = Label14.Caption + Len(strBuff) '发送计数
End If
End Select
uerror3:
End Sub