本帖最后由 dy008 于 2013-9-2 23:03 编辑
liangdyc 发表于 2013-9-2 17:41
FE 09 06 01 01 00 00 00 00 00 02 03 51 04 03 10 D3
这是我接收到的一组数据。你试试,我试了,算出来 ...
这个问题关键在于计算CRC的时候是不计算包头的,请把包头0xFE去掉,再按前面说的方法即可计算出正确的CRC字段。程序如下:
public static ushort crc_calculate(byte[] pBuffer, int length)
{
if (length < 1)
{
return 0xffff;
}
// For a "message" of length bytes contained in the unsigned char array
// pointed to by pBuffer, calculate the CRC
// crcCalculate(unsigned char* pBuffer, int length, unsigned short* checkConst) < not needed
ushort crcTmp;
int i;
crcTmp = X25_INIT_CRC;
for (i = 1; i < length; i++) // skips header //注意这里是跳过了包头的!
{
crcTmp = crc_accumulate(pBuffer, crcTmp);
//Console.WriteLine(crcTmp + " " + pBuffer + " " + length);
}
return (crcTmp);
}
另外注意CRC字段是低位在前,最后才是高位!
|