照着协议看看吧,没有那么复杂。
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
//名称: CRC16------循环容余校验
//----------------------------------------------------
//参数: unsigned car *pucChar-------------接收到的字符
//----------------------------------------------------
//功能: 循环容余校验
//----------------------------------------------------
//返回值:
// 校验码
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
unsigned short CRC16(const unsigned car* pucChar, unsigned int unLen)
{
unsigned short usItem=0xA001;
unsigned short usCRCReg=0xFFFF;
unsigned car ucChar;
for(unsigned int i=0;i<unLen;i++)
{
ucChar=*(pucChar+i);
usCRCReg^=ucChar;
for(int j=0;j<8;j++)
{
bool bLSB=((usCRCReg & 0x0001)== 0x0001);
usCRCReg=usCRCReg>>1;
if(bLSB)
{
usCRCReg^=usItem;
}
}
}
return usCRCReg;
}