最近拿到一个通讯协议去做。CRC16,约好是LSB,多项式8408,初始值FFFF。我的程序是大致这样的:
unsigned char Tx_Bytes[10]={0x11,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
void do_crc(unsigned char *message, unsigned int len)
{
int i, j;
unsigned int crc_reg = 0xffff;
unsigned char index;
unsigned int to_xor;
for (i = 0; i < len; i++)
{
index = (crc_reg ^ message[i]) & 0xff;
to_xor = index;
for (j = 0; j < 8; j++)
{
if (to_xor & 0x0001)
to_xor = (to_xor >> 1) ^ 0x8408;
else
to_xor >>= 1;
}
crc_reg = (crc_reg >> 8) ^ to_xor;
}
// return crc_reg;
Tx_Bytes[8]=crc_reg&0xff;
Tx_Bytes[9]=crc_reg>>8;
}
这样吧数据的最后2个作为CRC的结果,但是和对方通讯总是接不上,CRC校验有问题,麻烦各位帮看看这段程序,是我这边有问题不?多谢多谢~ |