看到的用VB 编写的上位机代码中的通信函数,求大神解释下
PrivateSubMSComm1_OnComm()DimcountAsIntegerDimReceiveArr()AsByte'接收数据数组OnErrorGoToErr...
Private Sub MSComm1_OnComm()
Dim count As Integer
Dim ReceiveArr() As Byte ' 接收数据数组
On Error GoTo Err
Select Case MSComm1.CommEvent ' 每接收1个数就触发一次
Case comEvReceive
count = MSComm1.InBufferCount
ReceiveArr = MSComm1.Input
If (receivedCount = 0) Then
receivedStall = ReceiveArr(0) + 1
End If
For i = 0 To (count - 1) Step 1 ' 数据格式处理
If (ReceiveArr(i) < 16) Then
receiveData = receiveData & "0" + Hex(ReceiveArr(i)) & Space(1) ' 小于16,前面加0
Else
receiveData = receiveData & Hex(ReceiveArr(i)) & Space(1) ' 加空格显示
End If
receivedCount = receivedCount + 1
Next i
If (receivedCount >= receivedStall) Then
Call hexReceive ' 十六进制接收
MSComm1.InBufferCount = 0
receivedCount = 0
receiveData = ""
End If
End Select
Err:
End Sub 展开
Dim count As Integer
Dim ReceiveArr() As Byte ' 接收数据数组
On Error GoTo Err
Select Case MSComm1.CommEvent ' 每接收1个数就触发一次
Case comEvReceive
count = MSComm1.InBufferCount
ReceiveArr = MSComm1.Input
If (receivedCount = 0) Then
receivedStall = ReceiveArr(0) + 1
End If
For i = 0 To (count - 1) Step 1 ' 数据格式处理
If (ReceiveArr(i) < 16) Then
receiveData = receiveData & "0" + Hex(ReceiveArr(i)) & Space(1) ' 小于16,前面加0
Else
receiveData = receiveData & Hex(ReceiveArr(i)) & Space(1) ' 加空格显示
End If
receivedCount = receivedCount + 1
Next i
If (receivedCount >= receivedStall) Then
Call hexReceive ' 十六进制接收
MSComm1.InBufferCount = 0
receivedCount = 0
receiveData = ""
End If
End Select
Err:
End Sub 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询