比如:
Dim SendStr() As Byte
SendStr(0) = 1 '从站号是1
SendStr(1) = &H3 '读多个字的命令代码
SendStr(2) = 0 '起始地址高字节
SendStr(3) = 0 '起始地址低字节
SendStr(4) = &H0 '数据长度高字节
SendStr(5) = &H9 '数据长度低字节
SendStr(6) =?校验高位
SendStr(7) =?校验低位
'CRC校验码的计算方法,如以下函数,可以得到字节数组变量cmdstring指向的字
符串的CRC校验码。
Function
crc16(ByRef cmdstring() As Byte, ByVal j As Integer)
Dim data As Integer
Dim i As Integer
Addressreg_crc = &HFFFF
For i = 0 To j
Addressreg_crc = Addressreg_crc Xor cmdstring(i)
For j = 0 To 7
data = Addressreg_crc And &H1
If data Then
Addressreg_crc = Int(Addressreg_crc / 2)
Addressreg_crc = Addressreg_crc And &H7FFF
Addressreg_crc = Addressreg_crc Xor &HA001
Else
Addressreg_crc = Addressreg_crc / 2
Addressreg_crc = Addressreg_crc And &H7FFF
End If
Next j
Next i
If Addressreg_crc < 0 Then
Addressreg_crc = Addressreg_crc - &HFFFF0000
End If
HiByte = Addressreg_crc And &HFF
LoByte = (Addressreg_crc And &HFF00) / &H100
End
Function
'调用
Call crc16(SendStr,5)
SendStr(6) = HiByte
SendStr(7) = LoByte
加载中,请稍候......