计算函数: #ifndef HAVE_CRC_ACCUMULATE /** * @brief Accumulate the X.25 CRC by adding one char at a time. * * The checksum function adds the hash of one char at a time to the * 16 bit checksum (uint16_t). * * @param data new char to hash * @param crcAccum the already accumulated checksum **/ staticinlinevoid crc_accumulate(uint8_t data, uint16_t*crcAccum)
{ /*Accumulate one byte of data into the CRC*/ uint8_t tmp;
这个问题关键在于计算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);
}