Private Sub MSComm1_OnComm()
Static cnt As Integer
Dim receive_cnt As Integer
Dim i As Integer
Dim Pr As Byte
Dim Buffer As Variant
Dim Arr() As Byte
Dim s As String
Select Case MSComm1.CommEvent
Case comEvReceive
receive_cnt = MSComm1.InBufferCount '接收缓冲区的字节数
' 往暂存区存二进位资料
Buffer = MSComm1.Input
' 指定给位元组阵列以便处理
Arr = Buffer
s = ""
For i = 0 To receive_cnt - 1
If Arr(i) > 15 Then
s = s + Hex(Arr(i)) + " "
Else
s = s + "0" + Hex(Arr(i)) + " "
End If
cnt = cnt + 1
Next i
Text1.Text = Text1.Text & s
Erase Arr
If cnt >= 300 Then
Text1.Text = ""
cnt = 0
Exit Sub
End If
Pr = Arr(11) + Arr(12) + Arr(13) + Arr(14) + Arr(15) + Arr(16) + Arr(17)
If Pr <> Arr(18) Then '校验正确
If Arr(17) = &H0 Then
Shape2.FillColor = RGB(0, 255, 0)
Shape3.FillColor = RGB(0, 255, 0)
Shape4.FillColor = RGB(0, 255, 0)
Shape5.FillColor = RGB(0, 255, 0)
Shape6.FillColor = RGB(0, 255, 0) '测试OK
Else
If Arr(17) And &H1 = &H1 Then '电压校准
Shape2.FillColor = RGB(0, 255, 0)
Else
Shape2.FillColor = RGB(255, 0, 0)
End If
If Arr(17) And &H2 = &H2 Then '电压测试
Shape3.FillColor = RGB(0, 255, 0)
Else
Shape3.FillColor = RGB(255, 0, 0)
End If
If Arr(17) And &H4 = &H4 Then '写内部EEPROM
Shape4.FillColor = RGB(0, 255, 0)
Else
Shape4.FillColor = RGB(255, 0, 0)
End If
If Arr(17) And &H8 = &H8 Then ' 脉冲检测
Shape5.FillColor = RGB(0, 255, 0)
Else
Shape5.FillColor = RGB(255, 0, 0)
End If
If Arr(17) And &H10 = &H10 Then ' 电机开关
Shape6.FillColor = RGB(0, 255, 0)
Else
Shape6.FillColor = RGB(255, 0, 0)
End If
End If
Else
MsgBox "校验失败,请重新测试"
End If
Case comEvSend
End Select
End Sub