vb编写双机通信,要求加入crc,怎么加啊?超急。

2024-12-19 21:12:27
推荐回答(3个)
回答1:

这是一个普遍的CRC16校验for VB的
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

这个偶用在单片机和电脑进行ModbusRTU协议通讯时用,很正常。

回答2:

我倒是有现成的,但是分好少

回答3:

************************************************************crc16校验程序:功能:接收字节crc校验,返回校验值
unsigned
int
crc_16(unsigned
int
puicrcword,unsigned
char
pucchechingchar)
形参:
pucchechingchar
需要校验数据
puicrcword
校验后的数据
*************************************************************/unsigned
int
crc_16(unsigned
int
puicrcword,
unsigned
char
pucchechingchar)
else
puicrcword
>>=
1;
}return
puicrcword;
}