《crc16计算算法.pdf》由会员分享,可在线阅读,更多相关《crc16计算算法.pdf(6页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、1 CRC 16 计算算法OxFFFF CRC16 CRC16 XOR 字节 CRC16 N=0 CRC16 右移 1 位进位CRC16 XOR POLY CRC 16 N=N+1 N 7 Yes No No Yes 报文结束?Yes No 结束后续子节XOR=异或N=字节的信息位POLY=CRC 16 多项式计算 =1010 0000 0000 0001(生成多项式 =1+x+x +x)在 CRC 16 中,发送的第一个字节为低字节.2 CRC 计算示例 (帧 02 07)CRC 寄存器初始化1111111111111111XOR 第一个字符0000000000000000111111111
2、1111101移位 1011111111111111011010000000000001标志 1,XOR 多项式1101111111111111移位 201101111111111111标志 1,XOR 多项式10100000000000011100111111111110移位 301100111111111100移位 40011001111111111110100000000000011001001111111110移位 501001001111111110移位 60010010011111111110100000000000011000010011111110移位 701000010011
3、111110移位 800100001001111110101000000000000110000001001111100000000000000111XOR 第二个字符1000000100111001移位 10100000010011100110100000000000011110000010011101移位 20111000001001110110100000000000011101000001001111移位 30110100000100111110100000000000013 1100100000100110移位 401100100000100110移位 500110010000010
4、01110100000000000011001001000001000移位 601001001000001000移位 700100100100000100移位 800010010010000010最高有效最低有效帧的 CRC 16 则为:4112例执行 CRC 生成的 C 语言的函数在下面示出。所有的可能的CRC 值都被预装在两个数组中,当计算报文内容时可以简单的索引即可。一个数组含有16 位 CRC 域的所有 256 个可能的高位字节,另一个数组含有地位字节的值。这种索引访问CRC 的方式提供了比对报文缓冲区的每个新字符都计算新的CRC 更快的方法。注意:此函数内部执行高/低 CRC 字节的
5、交换。此函数返回的是已经经过交换的 CRC 值。也就是说,从该函数返回的 CRC 值可以直接放置于报文用于发送。函数使用两个参数:unsigned char*puchMsg;指向含有用于生成 CRC 的二进制数据报文缓冲区的指针unsigned short usDataLen;报文缓冲区的字节数.CRC 生成函数unsigned short CRC16(puchMsg,usDataLen)/unsigned char*puchMsg;/*用于 计 算CRC 的报 文*/unsigned short usDataLen;/*报文中的字节数*/unsigned char uchCRCHi=0 xF
6、F;/*CRC 的高字节初始化*/unsigned char uchCRCLo=0 xFF;/*CRC 的低字节初始化4*/unsigned uIndex;/*CRC 查询表索引*/while(usDataLen-)/*完成整个报文缓冲区*/uIndex=uchCRCLo *puchMsgg+;/*计算CRC */uchCRCLo=uchCRCHi auchCRCHiuIndex;uchCRCHi=auchCRCLouIndex;return(uchCRCHi 8|uchCRCLo);5 高字节表/*高位字节的CRC 值*/static unsigned char auchCRCHi=0 x0
7、0,0 xC1,0 x81,0 x40,0 x01,0 xC0,0 x80,0 x41,0 x01,0 xC0,0 x80,0 x41,0 x00,0 xC1,0 x81,0 x40,0 x01,0 xC0,0 x80,0 x41,0 x00,0 xC1,0 x81,0 x40,0 x00,0 xC1,0 x81,0 x40,0 x01,0 xC0,0 x80,0 x41,0 x01,0 xC0,0 x80,0 x41,0 x00,0 xC1,0 x81,0 x40,0 x00,0 xC1,0 x81,0 x40,0 x01,0 xC0,0 x80,0 x41,0 x00,0 xC1,0 x8
8、1,0 x40,0 x01,0 xC0,0 x80,0 x41,0 x01,0 xC0,0 x80,0 x41,0 x00,0 xC1,0 x81,0 x40,0 x01,0 xC0,0 x80,0 x41,0 x00,0 xC1,0 x81,0 x40,0 x00,0 xC1,0 x81,0 x40,0 x01,0 xC0,0 x80,0 x41,0 x00,0 xC1,0 x81,0 x40,0 x01,0 xC0,0 x80,0 x41,0 x01,0 xC0,0 x80,0 x41,0 x00,0 xC1,0 x81,0 x40,0 x00,0 xC1,0 x81,0 x40,0 x0
9、1,0 xC0,0 x80,0 x41,0 x01,0 xC0,0 x80,0 x41,0 x00,0 xC1,0 x81,0 x40,0 x01,0 xC0,0 x80,0 x41,0 x00,0 xC1,0 x81,0 x40,0 x00,0 xC1,0 x81,0 x40,0 x01,0 xC0,0 x80,0 x41,0 x01,0 xC0,0 x80,0 x41,0 x00,0 xC1,0 x81,0 x40,0 x00,0 xC1,0 x81,0 x40,0 x01,0 xC0,0 x80,0 x41,0 x00,0 xC1,0 x81,0 x40,0 x01,0 xC0,0 x8
10、0,0 x41,0 x01,0 xC0,0 x80,0 x41,0 x00,0 xC1,0 x81,0 x40,0 x00,0 xC1,0 x81,0 x40,0 x01,0 xC0,0 x80,0 x41,0 x01,0 xC0,0 x80,0 x41,0 x00,0 xC1,0 x81,0 x40,0 x01,0 xC0,0 x80,0 x41,0 x00,0 xC1,0 x81,0 x40,0 x00,0 xC1,0 x81,0 x40,0 x01,0 xC0,0 x80,0 x41,0 x00,0 xC1,0 x81,0 x40,0 x01,0 xC0,0 x80,0 x41,0 x0
11、1,0 xC0,0 x80,0 x41,0 x00,0 xC1,0 x81,0 x40,0 x01,0 xC0,0 x80,0 x41,0 x00,0 xC1,0 x81,0 x40,0 x00,0 xC1,0 x81,0 x40,0 x01,0 xC0,0 x80,0 x41,0 x01,0 xC0,0 x80,0 x41,0 x00,0 xC1,0 x81,0 x40,0 x00,0 xC1,0 x81,0 x40,0 x01,0 xC0,0 x80,0 x41,0 x00,0 xC1,0 x81,0 x40,0 x01,0 xC0,0 x80,0 x41,0 x01,0 xC0,0 x8
12、0,0 x41,0 x00,0 xC1,0 x81,0 x40;6 低字节表/*低位字节的CRC 值*/static char auchCRCLo=0 x00,0 xC0,0 xC1,0 x01,0 xC3,0 x03,0 x02,0 xC2,0 xC6,0 x06,0 x07,0 xC7,0 x05,0 xC5,0 xC4,0 x04,0 xCC,0 x0C,0 x0D,0 xCD,0 x0F,0 xCF,0 xCE,0 x0E,0 x0A,0 xCA,0 xCB,0 x0B,0 xC9,0 x09,0 x08,0 xC8,0 xD8,0 x18,0 x19,0 xD9,0 x1B,0 xD
13、B,0 xDA,0 x1A,0 x1E,0 xDE,0 xDF,0 x1F,0 xDD,0 x1D,0 x1C,0 xDC,0 x14,0 xD4,0 xD5,0 x15,0 xD7,0 x17,0 x16,0 xD6,0 xD2,0 x12,0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