请教如何在VB程序中查看串口的返回值?

来源:百度知道 编辑:UC知道 时间:2024/06/18 04:37:03

使用MSCOMM控件,按照上下位机的通信协议,编写代码。
Private Sub Form_Load()
MSComm1.Settings = "9600,n,8,1" '暂定无校验,数据位8位,停止位1位
MSComm1.InputMode = comInputModeBinary '采用二进制传输
MSComm1.InBufferCount = 0 '清空接受缓冲区
MSComm1.OutBufferCount = 0 '清空传输缓冲区
MSComm1.RThreshold = 1 '产生MSComm事件
MSComm1.InBufferSize = 1024
MSComm1.PortOpen = True
End Sub

Private Sub MSComm1_OnComm() '接收数据
Dim BytReceived() As Byte
Dim strBuff As String
Select Case MSComm1.CommEvent
Case 2
MSComm1.InputLen = 0
strBuff = MSComm1.Input
BytReceived() = strBuff
Dim i As Integer
For i = 0 To UBound(BytReceived)
If Len(Hex(BytReceived(i))) = 1 Then
strData = strData & "0" & Hex(BytReceived(i))
Else
strData = strData & Hex(BytReceived(i))
End If
Next
Text1 = strData
'按通信协议写接收数据处理代码
End Select
End Sub
请参阅我在下列连接的答复。