CRC校验的作用:
一般用于通信过程中,验证数据完整性;数据有无丢失。
校验原理&程序设计流程图![请添加图片描述](https://i-blog.csdnimg.cn/blog_migrate/fc90d2fbb128080e2cbef3b5ef85a9b7.png)
CRC16——C程序验证
- 源码
#include <stdio.h>
unsigned short RTU_CRC( unsigned char * puchMsg, unsigned short usDataLen );
int main(){
int DataLen=11;
unsigned char crc[] = {
0x0B,0xC1,0x08,0x21,0x92,0x64,0x10,0xD1,0x02,0x01,0x02};
for (int i=0; i<DataLen; i++){
printf("%02X",crc[i]);
}
printf("---CRC check = %0X", RTU_CRC(crc,DataLen));
return 0;
}
static const unsigned char auchCRCHi[] = {
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01,
0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80,
0x41, 0x00, 0xC1, 0x81, 0x40, 0x01