With MSComm1
Select Case .CommEvent
'*******************接收到报警器数据****************************
Case comEvReceive
.RThreshold = 0 '关闭中断
Gcrc1 = 0: Gcrc2 = 0
'初始化CRC数值
RecData = .Input
'接受侦标志AA
ReciveData(0) = AscB(RecData) '转换
If ReciveData(0) <> &HAA Then .RThreshold = 6: Set RecData = Nothing: Exit Sub '错误退出
Call CRC(ReciveData(0)) 'CRC效验
RecData = .Input
'接受侦标志55
ReciveData(1) = AscB(RecData)
If ReciveData(1) <> &H55 Then .RThreshold = 6: Set RecData = Nothing: Exit Sub
Call CRC(ReciveData(1))
RecData = .Input
'目标地址即PC机地址00
ReciveData(2) = AscB(RecData)
If ReciveData(2) <> &H0 Then .RThreshold = 6: Set RecData = Nothing: Exit Sub
Call CRC(ReciveData(2))
RecData = .Input
'源地址即报警器地址
ReciveData(3) = AscB(RecData)
Call CRC(ReciveData(3))
If CDec(ReciveData(3)) > 16 Or CDec(ReciveData(3)) < 0 Then .RThreshold = 6: Set RecData = Nothing: Exit Sub
RecData = .Input
'数据长度
ReciveData(4) = AscB(RecData)
If CDec(ReciveData(4)) < 3 Then .RThreshold = 6: Set RecData = Nothing: Exit Sub
Call CRC(ReciveData(4))
RecData = .Input
ReciveData(5) = AscB(RecData) '命令字
Call CRC(ReciveData(5))
一个字符一个字符接收