CRC16常见的标准有以下几种,被用在各个规范中,其算法原理基本一致,就是在数据的输入和输出有所差异,下边把这些标准的差异列出,并给出C语言的算法实现。
CRC16_CCITT:多项式x16+x12+x5+1(0x1021),初始值0x0000,低位在前,高位在后,结果与0x0000异或
CRC16_CCITT_FALSE:多项式x16+x12+x5+1(0x1021),初始值0xFFFF,低位在后,高位在前,结果与0x0000异或
CRC16_XMODEM:多项式x16+x12+x5+1(0x1021),初始值0x0000,低位在后,高位在前,结果与0x0000异或
CRC16_X25:多项式x16+x12+x5+1(0x1021),初始值0x0000,低位在前,高位在后,结果与0xFFFF异或
CRC16_MODBUS:多项式x16+x15+x2+1(0x8005),初始值0xFFFF,低位在前,高位在后,结果与0x0000异或
CRC16_IBM:多项式x16+x15+x2+1(0x8005),初始值0x0000,低位在前,高位在后,结果与0x0000异或
CRC16_MAXIM:多项式x16+x15+x2+1(0x8005),初始值0x0000,低位在前,高位在后,结果与0xFFFF异或
CRC16_USB:多项式x16+x15+x2+1(0x8005),初始值0xFFFF,低位在前,高位在后,结果与0xFFFF异或
模式 |
多项式 |
初始值 |
数据位序 |
结果处理 |
CRC16_CCITT |
x16+x12+x5+1(0x1021) |
0x0000 |
低位在前,高位在后 |
与0x0000异或 |
CRC16_CCITT_FALSE |
x16+x12+x5+1(0x1021) |
0xFFFF |
低位在后,高位在前 |
与0x0000异或 |
CRC16_XMODEM |
x16+x12+x5+1(0x1021) |
0x0000 |
低位在后,高位在前 |
与0x0000异或 |
CRC16_X25 |
x16+x12+x5+1(0x1021) |
0x0000 |
低位在后,高位在前 |
与0xFFFF异或 |
CRC16_ MODBUS |
x16+x15+x2+1(0x8005) |
0xFFFF |
低位在前,高位在后 |
与0x0000异或 |
CRC16_ IBM |
x16+x15+x2+1(0x8005) |
0x0000 |
低位在前,高位在后 |
与0x0000异或 |
CRC16_ MAXIM |
x16+x15+x2+1(0x8005) |
0x0000 |
低位在前,高位在后 |
与0xFFFF异或 |
CRC16_ USB |
x16+x15+x2+1(0x8005) |
0xFFFF |
低位在前,高位在后 |
与0xFFFF异或 |
多项式产生:
如x16+x12+x5+1
x16表示第16位为1,x5表示第5位为1
(1 << 16) | (1 << 12) | (1 << 5) | (1) = 0x11021
但是CRC16只取低16位,写成16进制数就是 0x1021
CRC16的算法原理:
1.根据CRC16的标准选择初值CRCIn的值。
2.将数据的第一个字节与CRCIn高8位异或。
3.判断最高位,若该位为 0 左移一位,若为 1 左移一位再与多项式Hex码异或。
4.重复3直至8位全部移位计算结束。
5.重复将所有输入数据操作完成以上步骤,所得16位数即16位CRC校验码。根据算法原理与标准要求就能简单的写出具体程序:
-
unsigned short CRC16_CCITT(unsigned char *puchMsg, unsigned int usDataLen)
-
{
-
unsigned
short wCRCin =
0x0000;
-
unsigned
short wCPoly =
0x1021;
-
unsigned
char wChar =
0;
-
-
while (usDataLen--)
-
{
-
wChar = *(puchMsg++);
-
InvertUint8(&wChar,&wChar);
-
wCRCin ^= (wChar <<
8);
-
for(
int i =
0;i <
8;i++)
-
{
-
if(wCRCin &
0x8000)
-
wCRCin = (wCRCin <<
1) ^ wCPoly;
-
else
-
wCRCin = wCRCin <<
1;
-
}
-
}
-
InvertUint16(&wCRCin,&wCRCin);
-
return (wCRCin) ;
-
}
-
unsigned short CRC16_CCITT_FALSE(unsigned char *puchMsg, unsigned int usDataLen)
-
{
-
unsigned
short wCRCin =
0xFFFF;
-
unsigned
short wCPoly =
0x1021;
-
unsigned
char wChar =
0;
-
-
while (usDataLen--)
-
{
-
wChar = *(puchMsg++);
-
wCRCin ^= (wChar <<
8);
-
for(
int i =
0;i <
8;i++)
-
{
-
if(wCRCin &
0x8000)
-
wCRCin = (wCRCin <<
1) ^ wCPoly;
-
else
-
wCRCin = wCRCin <<
1;
-
}
-
}
-
return (wCRCin) ;
-
}
-
unsigned short CRC16_XMODEM(unsigned char *puchMsg, unsigned int usDataLen)
-
{
-
unsigned
short wCRCin =
0x0000;
-
unsigned
short wCPoly =
0x1021;
-
unsigned
char wChar =
0;
-
-
while (usDataLen--)
-
{
-
wChar = *(puchMsg++);
-
wCRCin ^= (wChar <<
8);
-
for(
int i =
0;i <
8;i++)
-
{
-
if(wCRCin &
0x8000)
-
wCRCin = (wCRCin <<
1) ^ wCPoly;
-
else
-
wCRCin = wCRCin <<
1;
-
}
-
}
-
return (wCRCin) ;
-
}
-
-
unsigned short CRC16_X25(unsigned char *puchMsg, unsigned int usDataLen)
-
{
-
unsigned
short wCRCin =
0xFFFF;
-
unsigned
short wCPoly =
0x1021;
-
unsigned
char wChar =
0;
-
-
while (usDataLen--)
-
{
-
wChar = *(puchMsg++);
-
InvertUint8(&wChar,&wChar);
-
wCRCin ^= (wChar <<
8);
-
for(
int i =
0;i <
8;i++)
-
{
-
if(wCRCin &
0x8000)
-
wCRCin = (wCRCin <<
1) ^ wCPoly;
-
else
-
wCRCin = wCRCin <<
1;
-
}
-
}
-
InvertUint16(&wCRCin,&wCRCin);
-
return (wCRCin^
0xFFFF) ;
-
}
-
-
unsigned short CRC16_MODBUS(unsigned char *puchMsg, unsigned int usDataLen)
-
{
-
unsigned
short wCRCin =
0xFFFF;
-
unsigned
short wCPoly =
0x8005;
-
unsigned
char wChar =
0;
-
-
while (usDataLen--)
-
{
-
wChar = *(puchMsg++);
-
InvertUint8(&wChar,&wChar);
-
wCRCin ^= (wChar <<
8);
-
for(
int i =
0;i <
8;i++)
-
{
-
if(wCRCin &
0x8000)
-
wCRCin = (wCRCin <<
1) ^ wCPoly;
-
else
-
wCRCin = wCRCin <<
1;
-
}
-
}
-
InvertUint16(&wCRCin,&wCRCin);
-
return (wCRCin) ;
-
}
-
unsigned short CRC16_IBM(unsigned char *puchMsg, unsigned int usDataLen)
-
{
-
unsigned
short wCRCin =
0x0000;
-
unsigned
short wCPoly =
0x8005;
-
unsigned
char wChar =
0;
-
-
while (usDataLen--)
-
{
-
wChar = *(puchMsg++);
-
InvertUint8(&wChar,&wChar);
-
wCRCin ^= (wChar <<
8);
-
for(
int i =
0;i <
8;i++)
-
{
-
if(wCRCin &
0x8000)
-
wCRCin = (wCRCin <<
1) ^ wCPoly;
-
else
-
wCRCin = wCRCin <<
1;
-
}
-
}
-
InvertUint16(&wCRCin,&wCRCin);
-
return (wCRCin) ;
-
}
-
unsigned short CRC16_MAXIM(unsigned char *puchMsg, unsigned int usDataLen)
-
{
-
unsigned
short wCRCin =
0x0000;
-
unsigned
short wCPoly =
0x8005;
-
unsigned
char wChar =
0;
-
-
while (usDataLen--)
-
{
-
wChar = *(puchMsg++);
-
InvertUint8(&wChar,&wChar);
-
wCRCin ^= (wChar <<
8);
-
for(
int i =
0;i <
8;i++)
-
{
-
if(wCRCin &
0x8000)
-
wCRCin = (wCRCin <<
1) ^ wCPoly;
-
else
-
wCRCin = wCRCin <<
1;
-
}
-
}
-
InvertUint16(&wCRCin,&wCRCin);
-
return (wCRCin^
0xFFFF) ;
-
}
-
unsigned short CRC16_USB(unsigned char *puchMsg, unsigned int usDataLen)
-
{
-
unsigned
short wCRCin =
0xFFFF;
-
unsigned
short wCPoly =
0x8005;
-
unsigned
char wChar =
0;
-
-
while (usDataLen--)
-
{
-
wChar = *(puchMsg++);
-
InvertUint8(&wChar,&wChar);
-
wCRCin ^= (wChar <<
8);
-
for(
int i =
0;i <
8;i++)
-
{
-
if(wCRCin &
0x8000)
-
wCRCin = (wCRCin <<
1) ^ wCPoly;
-
else
-
wCRCin = wCRCin <<
1;
-
}
-
}
-
InvertUint16(&wCRCin,&wCRCin);
-
return (wCRCin^
0xFFFF) ;
-
}
-
void InvertUint8(unsigned char *dBuf,unsigned char *srcBuf)
-
{
-
int i;
-
unsigned
char tmp[
4];
-
tmp[
0] =
0;
-
for(i=
0;i<
8;i++)
-
{
-
if(srcBuf[
0]& (
1 << i))
-
tmp[
0]|=
1<<(
7-i);
-
}
-
dBuf[
0] = tmp[
0];
-
-
}
-
void InvertUint16(unsigned short *dBuf,unsigned short *srcBuf)
-
{
-
int i;
-
unsigned
short tmp[
4];
-
tmp[
0] =
0;
-
for(i=
0;i<
16;i++)
-
{
-
if(srcBuf[
0]& (
1 << i))
-
tmp[
0]|=
1<<(
15 - i);
-
}
-
dBuf[
0] = tmp[
0];
-
}
-
void InvertUint32(unsigned int *dBuf,unsigned int *srcBuf)
-
{
-
int i;
-
unsigned
int tmp[
4];
-
-
tmp[
0] =
0;
-
-
for(i=
0;i<
32;i++)
-
{
-
if(srcBuf[
0]& (
1 << i))
-
tmp[
0]|=
1<<(
15 - i);
-
}
-
dBuf[
0] = tmp[
0];
-
}
-
具体验证使用这个工具,内含CRC算法的计算,和后边的博客中提到的其他算法的工具合集
加密解密算法工具集
在这个基础上也加入CRC32 的校验算法
-
//CRC32算法:
-
unsigned int CRC32(unsigned char *puchMsg, unsigned int usDataLen)
-
{
-
int i;
-
unsigned int wCRCin = 0xFFFFFFFF;
-
unsigned int wCPoly = 0x04C11DB7;
-
unsigned int wChar = 0;
-
while (usDataLen--)
-
{
-
wChar = *(puchMsg++);
-
InvertUint8((unsigned char *)&wChar,(unsigned char *)&wChar);
-
wCRCin ^= (wChar
<< 24);
-
for(i = 0;i < 8;i++)
-
{
-
if(wCRCin & 0x80000000)
-
wCRCin = (wCRCin << 1) ^ wCPoly;
-
else
-
wCRCin = wCRCin << 1;
-
}
-
}
-
InvertUint32(&wCRCin,&wCRCin);
-
return (wCRCin ^ 0xFFFFFFFF) ;
-
}
-
对于CRC32可能还有其他的多项式和初始值和结果值是否需要异或以及输入数据是否需要位序倒转等要求在源码中修改
现选择最常用的CRC-16校验,说明它的使用方法。
根据Modbus协议,常规485通讯的信息发送形式如下:
地址 功能码 数据信息 校验码
1byte 1byte nbyte 2byte
CRC校验是前面几段数据内容的校验值,为一个16位数据,发送时,低8位在前,高8为最后。
例如:信息字段代码为: 1011001,校验字段为:1010。
发送方:发出的传输字段为: 1 0 1 1 0 0 1 1 0 10
信息字段 校验字段
接收方:使用相同的计算方法计算出信息字段的校验码,对比接收到的实际校验码,如果相等及信息正确,不相等则信息错误;或者将接受到的所有信息除多项式,如果能够除尽,则信息正确。
常用查表法和计算法。计算方法一般都是:
(1)、预置1个16位的寄存器值0xFFFF,称此寄存器为CRC寄存器;
(2)、把第一个8位二进制数据(既通讯信息帧的第一个字节)与16位的CRC寄存器的低
8位相异或,把结果放于CRC寄存器,高八位数据不变;
(3)、把CRC寄存器的内容右移一位(朝高位)用0填补最高位,并检查右移后的移出位;
(4)、如果移出位为0:重复第3步(再次右移一位);如果移出位为1,CRC寄存器与一多
项式(A001)进行异或;
(5)、重复步骤3和4,直到右移8次,这样整个8位数据全部进行了处理;
(6)、重复步骤2到步骤5,进行通讯信息帧下一个字节的处理;
(7)、将该通讯信息帧所有字节按上述步骤计算完成后,得到的16位CRC寄存器的高、低
字节进行交换;
(8)、最后得到的CRC寄存器内容即为:CRC码。
以上计算步骤中的多项式A001是8005按位颠倒后的结果。
查表法是将移位异或的计算结果做成了一个表,就是将0~256放入一个长度为16位的寄存器中的低八位,高八位填充0,然后将该寄存器与多项式0XA001按照上述3、4步骤,直到八位全部移出,最后寄存器中的值就是表格中的数据,高八位、低八位分别单独一个表。
CRC16常见的标准有以下几种,被用在各个规范中,其算法原理基本一致,就是在数据的输入和输出有所差异,下边把这些标准的差异列出,并给出C语言的算法实现。
CRC16_CCITT:多项式x16+x12+x5+1(0x1021),初始值0x0000,低位在前,高位在后,结果与0x0000异或
CRC16_CCITT_FALSE:多项式x16+x12+x5+1(0x1021),初始值0xFFFF,低位在后,高位在前,结果与0x0000异或
CRC16_XMODEM:多项式x16+x12+x5+1(0x1021),初始值0x0000,低位在后,高位在前,结果与0x0000异或
CRC16_X25:多项式x16+x12+x5+1(0x1021),初始值0x0000,低位在前,高位在后,结果与0xFFFF异或
CRC16_MODBUS:多项式x16+x15+x5+1(0x8005),初始值0xFFFF,低位在前,高位在后,结果与0x0000异或
CRC16_IBM:多项式x16+x15+x5+1(0x8005),初始值0x0000,低位在前,高位在后,结果与0x0000异或
CRC16_MAXIM:多项式x16+x15+x5+1(0x8005),初始值0x0000,低位在前,高位在后,结果与0xFFFF异或
CRC16_USB:多项式x16+x15+x5+1(0x8005),初始值0xFFFF,低位在前,高位在后,结果与0xFFFF异或
1.根据CRC16的标准选择初值CRCIn的值。
2.将数据的第一个字节与CRCIn高8位异或。
3.判断最高位,若该位为 0 左移一位,若为 1 左移一位再与多项式Hex码异或。
4.重复3直至8位全部移位计算结束。
5.重复将所有输入数据操作完成以上步骤,所得16位数即16位CRC校验码。
根据算法原理与标准要求就能简单的写出具体程序:
-
/**
-
**************************************************************************************************
-
* @Brief Single byte data inversion
-
* @Param
-
* @DesBuf: destination buffer
-
* @SrcBuf: source buffer
-
* @RetVal None
-
* @Note (MSB)0101_0101 ---> 1010_1010(LSB)
-
**************************************************************************************************
-
*/
-
void InvertUint8(unsigned char *DesBuf, unsigned char *SrcBuf)
-
{
-
int i;
-
unsigned
char temp =
0;
-
-
for(i =
0; i <
8; i++)
-
{
-
if(SrcBuf[
0] & (
1 << i))
-
{
-
temp |=
1<<(
7-i);
-
}
-
}
-
DesBuf[
0] = temp;
-
}
-
-
/**
-
**************************************************************************************************
-
* @Brief double byte data inversion
-
* @Param
-
* @DesBuf: destination buffer
-
* @SrcBuf: source buffer
-
* @RetVal None
-
* @Note (MSB)0101_0101_1010_1010 ---> 0101_0101_1010_1010(LSB)
-
**************************************************************************************************
-
*/
-
void InvertUint16(unsigned short *DesBuf, unsigned short *SrcBuf)
-
{
-
int i;
-
unsigned
short temp =
0;
-
-
for(i =
0; i <
16; i++)
-
{
-
if(SrcBuf[
0] & (
1 << i))
-
{
-
temp |=
1<<(
15 - i);
-
}
-
}
-
DesBuf[
0] = temp;
-
}
-
-
unsigned short CRC16_CCITT(unsigned char *puchMsg, unsigned int usDataLen)
-
{
-
unsigned
short wCRCin =
0x0000;
-
unsigned
short wCPoly =
0x1021;
-
unsigned
char wChar =
0;
-
-
while (usDataLen--)
-
{
-
wChar = *(puchMsg++);
-
InvertUint8(&wChar, &wChar);
-
wCRCin ^= (wChar <<
8);
-
-
for(
int i =
0; i <
8; i++)
-
{
-
if(wCRCin &
0x8000)
-
{
-
wCRCin = (wCRCin <<
1) ^ wCPoly;
-
}
-
else
-
{
-
wCRCin = wCRCin <<
1;
-
}
-
}
-
}
-
InvertUint16(&wCRCin, &wCRCin);
-
return (wCRCin) ;
-
}
-
-
unsigned short CRC16_CCITT_FALSE(unsigned char *puchMsg, unsigned int usDataLen)
-
{
-
unsigned
short wCRCin =
0xFFFF;
-
unsigned
short wCPoly =
0x1021;
-
unsigned
char wChar =
0;
-
-
while (usDataLen--)
-
{
-
wChar = *(puchMsg++);
-
wCRCin ^= (wChar <<
8);
-
-
for(
int i =
0; i <
8; i++)
-
{
-
if(wCRCin &
0x8000)
-
{
-
wCRCin = (wCRCin <<
1) ^ wCPoly;
-
}
-
else
-
{
-
wCRCin = wCRCin <<
1;
-
}
-
}
-
}
-
return (wCRCin) ;
-
}
-
-
unsigned short CRC16_XMODEM(unsigned char *puchMsg, unsigned int usDataLen)
-
{
-
unsigned
short wCRCin =
0x0000;
-
unsigned
short wCPoly =
0x1021;
-
unsigned
char wChar =
0;
-
-
while (usDataLen--)
-
{
-
wChar = *(puchMsg++);
-
wCRCin ^= (wChar <<
8);
-
-
for(
int i =
0; i <
8; i++)
-
{
-
if(wCRCin &
0x8000)
-
{
-
wCRCin = (wCRCin <<
1) ^ wCPoly;
-
}
-
else
-
{
-
wCRCin = wCRCin <<
1;
-
}
-
}
-
}
-
return (wCRCin) ;
-
}
-
-
unsigned short CRC16_X25(unsigned char *puchMsg, unsigned int usDataLen)
-
{
-
unsigned
short wCRCin =
0xFFFF;
-
unsigned
short wCPoly =
0x1021;
-
unsigned
char wChar =
0;
-
-
while (usDataLen--)
-
{
-
wChar = *(puchMsg++);
-
InvertUint8(&wChar, &wChar);
-
wCRCin ^= (wChar <<
8);
-
-
for(
int i =
0;i <
8;i++)
-
{
-
if(wCRCin &
0x8000)
-
{
-
wCRCin = (wCRCin <<
1) ^ wCPoly;
-
}
-
else
-
{
-
wCRCin = wCRCin <<
1;
-
}
-
}
-
}
-
InvertUint16(&wCRCin, &wCRCin);
-
return (wCRCin^
0xFFFF) ;
-
}
-
-
unsigned short CRC16_MODBUS(unsigned char *puchMsg, unsigned int usDataLen)
-
{
-
unsigned
short wCRCin =
0xFFFF;
-
unsigned
short wCPoly =
0x8005;
-
unsigned
char wChar =
0;
-
-
while (usDataLen--)
-
{
-
wChar = *(puchMsg++);
-
InvertUint8(&wChar, &wChar);
-
wCRCin ^= (wChar <<
8);
-
-
for(
int i =
0; i <
8; i++)
-
{
-
if(wCRCin &
0x8000)
-
{
-
wCRCin = (wCRCin <<
1) ^ wCPoly;
-
}
-
else
-
{
-
wCRCin = wCRCin <<
1;
-
}
-
}
-
}
-
InvertUint16(&wCRCin, &wCRCin);
-
return (wCRCin) ;
-
}
-
-
unsigned short CRC16_IBM(unsigned char *puchMsg, unsigned int usDataLen)
-
{
-
unsigned
short wCRCin =
0x0000;
-
unsigned
short wCPoly =
0x8005;
-
unsigned
char wChar =
0;
-
-
while (usDataLen--)
-
{
-
wChar = *(puchMsg++);
-
InvertUint8(&wChar, &wChar);
-
wCRCin ^= (wChar <<
8);
-
-
for(
int i =
0; i <
8; i++)
-
{
-
if(wCRCin &
0x8000)
-
{
-
wCRCin = (wCRCin <<
1) ^ wCPoly;
-
}
-
else
-
{
-
wCRCin = wCRCin <<
1;
-
}
-
}
-
}
-
InvertUint16(&wCRCin,&wCRCin);
-
return (wCRCin) ;
-
}
-
-
unsigned short CRC16_MAXIM(unsigned char *puchMsg, unsigned int usDataLen)
-
{
-
unsigned
short wCRCin =
0x0000;
-
unsigned
short wCPoly =
0x8005;
-
unsigned
char wChar =
0;
-
-
while (usDataLen--)
-
{
-
wChar = *(puchMsg++);
-
InvertUint8(&wChar, &wChar);
-
wCRCin ^= (wChar <<
8);
-
-
for(
int i =
0; i <
8; i++)
-
{
-
if(wCRCin &
0x8000)
-
{
-
wCRCin = (wCRCin <<
1) ^ wCPoly;
-
}
-
else
-
{
-
wCRCin = wCRCin <<
1;
-
}
-
}
-
}
-
InvertUint16(&wCRCin, &wCRCin);
-
return (wCRCin^
0xFFFF) ;
-
}
-
-
unsigned short CRC16_USB(unsigned char *puchMsg, unsigned int usDataLen)
-
{
-
unsigned
short wCRCin =
0xFFFF;
-
unsigned
short wCPoly =
0x8005;
-
unsigned
char wChar =
0;
-
-
while (usDataLen--)
-
{
-
wChar = *(puchMsg++);
-
InvertUint8(&wChar, &wChar);
-
wCRCin ^= (wChar <<
8);
-
-
for(
int i =
0; i <
8; i++)
-
{
-
if(wCRCin &
0x8000)
-
{
-
wCRCin = (wCRCin <<
1) ^ wCPoly;
-
}
-
else
-
{
-
wCRCin = wCRCin <<
1;
-
}
-
}
-
}
-
InvertUint16(&wCRCin, &wCRCin);
-
return (wCRCin^
0xFFFF) ;
-
}