1.CRC校验原理
在代数编码理论中,一个数值可以表示为一个多项式。例如:一个十进制数值2892,可以用多项式表示为2x3 + 8x2 + 9x + 2(x=10)。同理,一个二进制数值1010101对应的多项式为x6 + x4 + x2 + 1(x=2)。
生成CRC码的基本原理是:设被校验的数据为K位,校验码为R位,码字长度为N(=K+R),则对于CRC码集中的任一码字,存在且仅存在一个R次多项式g(x),使得
V(x)=A(x)g(x)=xRm(x) + r(x);
其中: m(x)为被校验数据的K-1次多项式
r(x)为校验码的R-1次多项式
g(x)称为生成多项式:g(x)=g0 + g1x1 + g2x2 + ... + g(R-1)x(R-1) + gRxR
发送方通过指定的g(x)计算出CRC校验码,接收方则通过该g(x)来验证收到的CRC校验码。综上所述,一个完整的CRC校验过程是:
发送方:根据要传送的K位原始数据(二进制码序列),以标准指定的多项式计算出一个R位校验码(CRC码),附在原始数据后边,构成一个新的二进制码序列共K + R位,然后发送出去。
接收方:将接收到的数据除以与发送方相同的多项式值,如果能够除尽,则正确,否则证明出错。还有另外一种处理,就是接收方用发送方相同的方法计算出接收到数据的CRC校验值,再与发送方发来的校验值比较,相同则正确,否则证明出错。
2. CRC校验码的计算步骤:
例如:有一个要发送的7位二进制数1011001;对应的m(x)=x6 + x4 + x3 + 1。设CRC校验码取4位并设g(x)=x4 + x + 1,则该多项式对应的值是10011。根据CRC规则,为保证被除数够除,首先需将要发送的数扩大2R 即24倍(左移4位),得到 10110010000,对应的xRm(x)=x10 + x8 + x7 + x4 。
CRC校验码的生成本质其实就是采用模2除法取余数,该除法的简捷计算就是将除数和被除数按位做异或(相同为0,不同为1。0^0=0; 0^1=1; 1^0=1; 1^1=0)运算。需要注意的是,进行异或运算时除数必须和被除数最高有效位对齐。下面是将10110010000除以11001手工计算的演示,得到余数为1010,该值即是数据1011001的CRC校验值。
10110010000 将1011001左移4位
10011 多项式值与被除数最高有效位对齐
=00101010000 第1次异或结果
10011 多项式值与被除数最高有效位对齐
=00001100000 第2次异或结果
10011 多项式值与被除数最高有效位对齐
=00000101100 第3次异或结果
10011 多项式值与被除数最高有效位对齐
=00000001010 第4次异或结果
为了简便计算机程序求解CRC,在实际应用中通常把多项式值的最高位舍掉,并且将参加计算的数据高低位颠倒后再计算。前面的演算数据颠倒后的运算情况如下:
00001001101
11001
00001010100
11001
00000110000
11001
00110100000
11001
01010000000
提请注意:只有真正读懂以上手工演算的步骤和规律,才能理解下节内容。
3. CRC校验的程序设计
下面以最常用的CRC-16-IBM校验标准为例来说明CRC校验码的生成过程。 因为CRC-16码由两个字节构成,所以首先要准备一个16位的CRC寄存器,并将每一位都置1。具体的计算步骤为:
① 设置CRC寄存器,并给其赋值FFFFH。
② 将被校验数据的第一个字节(8Bit)与CRC寄存器的低8位进行异或,结果存CRC寄存器。
③ CRC寄存器的值向右移一位,最高位(MSB)补零,检查移出的最低位(LSB)是否为1。
④ 如果LSB为0,重复第三步;若LSB为1,CRC寄存器与CRC16多项式值A001H相异或。
⑤ 重复第③与第④步,直到该字节的8次移位全部完成。
⑥ 如果被校验数据有多个字节,则重复第②至第⑤步直到所有数据全部处理完。
⑦ 最终CRC寄存器的内容即为CRC值。
3.1 用查表法求CRC函数的程序
查表法省去了将被校验数据移位并与多项式异或计算的步骤,因此比计算法更快捷,但程序编码量偏大。
3.1.1 用VC编写的程序
unsigned short CRC16(puchMsg, usDataLen)
unsigned char * puchMsg ;
unsigned short usDataLen ;
{
unsigned char uchCRCHi = 0xFF ;
unsigned char uchCRCLo = 0xFF ;
unsigned uIndex ;
while (usDataLen--)
{
uIndex = uchCRCLo ^ * puchMsg++ ;
uchCRCLo = uchCRCHi ^ auchCRCLo [uIndex] ;
uchCRCHi = auchCRCHi [uIndex] ;
}
return (uchCRCLo << 8 | uchCRCHi) ;
}
static char auchCRCHi[] = {
0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06,
0x07, 0xC7, 0x05, 0xC5, 0xC4, 0x04, 0xCC, 0x0C, 0x0D, 0xCD,
0x0F, 0xCF, 0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, 0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C,
0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E, 0x5A, 0x9A, 0x9B, 0x5B,
0x99, 0x59, 0x58, 0x98, 0x88, 0x48, 0x49, 0x89, 0x4B, 0x8B,
0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C,
0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42,
0x43, 0x83, 0x41, 0x81, 0x80, 0x40 } ;
static unsigned char auchCRCLo[] = {
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, 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, 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,
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, 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 } ;
3.1.2 用VB编写的程序
Private Function CRC16(data() As Byte) As String
Dim CRC16Hi As Byte
Dim CRC16Lo As Byte
CRC16Hi = &HFF
CRC16Lo = &HFF
Dim i As Integer
Dim iIndex As Long
For i = 0 To UBound(data)
iIndex = CRC16Lo Xor data(i)
CRC16Lo = CRC16Hi Xor GetCRCLo(iIndex) ’低位处理
CRC16Hi = GetCRCHi(iIndex) ’高位处理
Next i
Dim ReturnData(1) As Byte
ReturnData(0) = CRC16Hi ’CRC高位
ReturnData(1) = CRC16Lo ’CRC低位
CRC16 = ReturnData
End Function
Function GetCRCHi(Ind As Long) As Byte ’CRC高位字节值表
GetCRCHi = Choose(Ind + 1,_
&H00, &HC0, &HC1, &H01, &HC3, &H03, &H02, &HC2, &HC6, &H06,_
&H07, &HC7, &H05, &HC5, &HC4, &H04, &HCC, &H0C, &H0D, &HCD,_
&H0F, &HCF, &HCE, &H0E, &H0A, &HCA, &HCB, &H0B, &HC9, &H09,_
&H08, &HC8, &HD8, &H18, &H19, &HD9, &H1B, &HDB, &HDA, &H1A,_
&H1E, &HDE, &HDF, &H1F, &HDD, &H1D, &H1C, &HDC, &H14, &HD4,_
&HD5, &H15, &HD7, &H17, &H16, &HD6, &HD2, &H12, &H13, &HD3,_
&H11, &HD1, &HD0, &H10, &HF0, &H30, &H31, &HF1, &H33, &HF3,_
&HF2, &H32, &H36, &HF6, &HF7, &H37, &HF5, &H35, &H34, &HF4,_
&H3C, &HFC, &HFD, &H3D, &HFF, &H3F, &H3E, &HFE, &HFA, &H3A,_
&H3B, &HFB, &H39, &HF9, &HF8, &H38, &H28, &HE8, &HE9, &H29,_
&HEB, &H2B, &H2A, &HEA, &HEE, &H2E, &H2F, &HEF, &H2D, &HED,_
&HEC, &H2C, &HE4, &H24, &H25, &HE5, &H27, &HE7, &HE6, &H26,_
&H22, &HE2, &HE3, &H23, &HE1, &H21, &H20, &HE0, &HA0, &H60,_
&H61, &HA1, &H63, &HA3, &HA2, &H62, &H66, &HA6, &HA7, &H67,_
&HA5, &H65, &H64, &HA4, &H6C, &HAC, &HAD, &H6D, &HAF, &H6F,_
&H6E, &HAE, &HAA, &H6A, &H6B, &HAB, &H69, &HA9, &HA8, &H68,_
&H78, &HB8, &HB9, &H79, &HBB, &H7B, &H7A, &HBA, &HBE, &H7E,_
&H7F, &HBF, &H7D, &HBD, &HBC, &H7C, &HB4, &H74, &H75, &HB5,_
&H77, &HB7, &HB6, &H76, &H72, &HB2, &HB3, &H73, &HB1, &H71,_
&H70, &HB0, &H50, &H90, &H91, &H51, &H93, &H53, &H52, &H92,_
&H96, &H56, &H57, &H97, &H55, &H95, &H94, &H54, &H9C, &H5C,_
&H5D, &H9D, &H5F, &H9F, &H9E, &H5E, &H5A, &H9A, &H9B, &H5B,_
&H99, &H59, &H58, &H98, &H88, &H48, &H49, &H89, &H4B, &H8B,_
&H8A, &H4A, &H4E, &H8E, &H8F, &H4F, &H8D, &H4D, &H4C, &H8C,_
&H44, &H84, &H85, &H45, &H87, &H47, &H46, &H86, &H82, &H42,_
&H43, &H83, &H41, &H81, &H80, &H40)
End Function
Function GetCRCLo(Ind As Long) As Byte ’CRC低位字节值表
GetCRCLo = Choose(Ind + 1,_
&H00, &HC1, &H81, &H40, &H01, &HC0, &H80, &H41, &H01, &HC0,_
&H80, &H41, &H00, &HC1, &H81, &H40, &H01, &HC0, &H80, &H41,_
&H00, &HC1, &H81, &H40, &H00, &HC1, &H81, &H40, &H01, &HC0,_
&H80, &H41, &H01, &HC0, &H80, &H41, &H00, &HC1, &H81, &H40,_
&H00, &HC1, &H81, &H40, &H01, &HC0, &H80, &H41, &H00, &HC1,_
&H81, &H40, &H01, &HC0, &H80, &H41, &H01, &HC0, &H80, &H41,_
&H00, &HC1, &H81, &H40, &H01, &HC0, &H80, &H41, &H00, &HC1,_
&H81, &H40, &H00, &HC1, &H81, &H40, &H01, &HC0, &H80, &H41,_
&H00, &HC1, &H81, &H40, &H01, &HC0, &H80, &H41, &H01, &HC0,_
&H80, &H41, &H00, &HC1, &H81, &H40, &H00, &HC1, &H81, &H40,_
&H01, &HC0, &H80, &H41, &H01, &HC0, &H80, &H41, &H00, &HC1,_
&H81, &H40, &H01, &HC0, &H80, &H41, &H00, &HC1, &H81, &H40,_
&H00, &HC1, &H81, &H40, &H01, &HC0, &H80, &H41, &H01, &HC0,_
&H80, &H41, &H00, &HC1, &H81, &H40, &H00, &HC1, &H81, &H40,_
&H01, &HC0, &H80, &H41, &H00, &HC1, &H81, &H40, &H01, &HC0,_
&H80, &H41, &H01, &HC0, &H80, &H41, &H00, &HC1, &H81, &H40,_
&H00, &HC1, &H81, &H40, &H01, &HC0, &H80, &H41, &H01, &HC0,_
&H80, &H41, &H00, &HC1, &H81, &H40, &H01, &HC0, &H80, &H41,_
&H00, &HC1, &H81, &H40, &H00, &HC1, &H81, &H40, &H01, &HC0,_
&H80, &H41, &H00, &HC1, &H81, &H40, &H01, &HC0, &H80, &H41,_
&H01, &HC0, &H80, &H41, &H00, &HC1, &H81, &H40, &H01, &HC0,_
&H80, &H41, &H00, &HC1, &H81, &H40, &H00, &HC1, &H81, &H40,_
&H01, &HC0, &H80, &H41, &H01, &HC0, &H80, &H41, &H00, &HC1,_
&H81, &H40, &H00, &HC1, &H81, &H40, &H01, &HC0, &H80, &H41,_
&H00, &HC1, &H81, &H40, &H01, &HC0, &H80, &H41, &H01, &HC0,_
&H80, &H41, &H00, &HC1, &H81, &H40)
End Function
3.2 用计算法求CRC函数的程序
计算法的优点是程序编码量小,虽然比查表法速度偏慢,但当被校验的数据量不大时并不明显。
3.2.1 用VC编写的程序
unsigned short CRC16(unsigned char buf[], int n)
{
unsigned short crc = 0xffff ;
for(i = 0; i < n; i++)
{
crc ^ = buf[i] ;
for(b = 0; b < 8; b++)
{
bool f = crc & 1 ;
crc >> = 1 ;
if(f)
crc ^ = 0xa001 ;
}
}
return(crc) ;
}
3.2.2 用VB编写的程序
Function CRC16(Data() As Byte, No As Integer, CRC16Lo As Byte, CRC16Hi As Byte) As String
Dim CL As Byte, CH As Byte, SaveLo As Byte, SaveHi As Byte
CRC16Hi = &HFF '为16位CRC校验寄存器赋初始值 FFFF
CRC16Lo = &HFF
CH = &HA0 '为16位CRC校验多项式赋初始值 A001
CL = &H1
For i = 1 To No
CRC16Lo = CRC16Lo Xor Data(i - 1) '每一个数据与CRC寄存器进行异或
For j = 1 To 8
SaveHi = CRC16Hi
SaveLo = CRC16Lo
CRC16Hi = CRC16Hi 2 '高位右移一位
CRC16Lo = CRC16Lo 2 '低位右移一位
If ((SaveHi And &H1) = &H1) Then '如果高位字节最右一位为1
CRC16Lo = CRC16Lo Or &H80 '则低位字节最左一位补1,否则自动补0
End If
If ((SaveLo And &H1) = &H1) Then '如低位字节最右一位为1,则与多项式值异或
CRC16Hi = CRC16Hi Xor CH
CRC16Lo = CRC16Lo Xor CL
End If
Next j
Next i
End Function
参考文献:
① GB/T 19582.1-2008《基于Modbus协议的工业自动化网络规范》
②《Modbus协议中文版》 可在互联网上下载
附表: CRC校验标准类型
CRC校验名多 项 式 (标准引用名)对应的16进制值或反值注:已去掉最高位的1
CRC-1x + 1 (most hardware; also known as parity bit)01 或 01 (01)
CRC-4-ITUx4 + x + 1 (ITU G.704, p. 12)03 或 0C (09)
CRC-5-ITUx5 + x4 + x2 + 1 (ITU G.704, p. 9)15 或 15 (1A)
CRC-5-USBx5 + x2 + 1 (USB token packets)05 或 14 (12)
CRC-6-ITUx6 + x + 1 (ITU G.704, p. 3)03 或 30 (21)
CRC-7x7 + x3 + 1 (telecom systems, MMC,SD)09 或 48 (44)
CRC-8-ATMx8 + x2 + x + 1 (ATM HEC)07 或 E0 (83)
CRC-8-CCITTx8 + x7 + x3 + x2 + 1 (1-Wire bus)8D 或 B1 (C6)
CRC-8-Dallas/Maximx8 + x5 + x4 + 1 (1-Wire bus)31 或 8C (98)
CRC-8x8 + x7 + x6 + x4 + x2 + 1D5 或 AB (EA)
CRC-8-SAE J1850x8 + x4 + x3 + x2 + 11D 或 B8 (8E)
CRC-10x10 + x9 + x5 + x4 + x + 1233 或 331 (319)
CRC-11x11 + x9 + x8 + x7 + x + 1 (FlexRay)385 或 50E (5C2)
CRC-12x12 + x11 + x3 + x2 + x + 1 (telecom systems )80F 或 F01 (C07)
CRC-15-CANx15 + x14 + x10 + x8 + x7 + x4 + x3 + 14599 或 4CD1 (62CC)
CRC-16-FletcherNot a CRC; see Fletcher's checksumUsed in Adler-32 A & B CRCs
CRC-16-CCITTx16 + x12 + x5 + 1 (CRC-CCITT, G.hn PHY headers, 802.15.4, X.25, V.41, CDMA, Bluetooth, XMODEM, HDLC,PPP, IrDA, BACnet, MMC,SD)1021 或 8408 (8810)
CRC-16-DNPx16 + x13 + x12 + x11 + x10 + x8 + x6 + x5 + x2+ 1 (DNP, IEC 870, M-Bus)3D65 或 A6BC (9EB2)
CRC-16-IBMx16 + x15 + x2 + 1 (CRC-16, SDLC, USB, many others)8005 或 A001 (C002)
CRC-24-Radix-64x24 + x23 + x18 + x17 + x14 + x11 + x10 + x7 +x6 + x5 + x4 + x3 + x + 1 (FlexRay)864CFB 或 DF3261 (C3267D)
CRC-30x30 + x29 + x21 + x20 + x15 + x13 + x12 + x11 +x8 + x7 + x6 + x2 + x + 1 (CDMA)2030B9C7 或 38E74301 (30185CE3)
CRC-32-AdlerNot a CRC; see Adler-32See Adler-32
CRC-32-IEEE 802.3x32 + x26 + x23 + x22 + x16 + x12 + x11 + x10 +x8 + x7 + x5 + x4 + x2 + x + 1 (V.42, MPEG-2, PNG [10], POSIX cksum)04C11DB7 或 EDB88320 (82608EDB)
CRC-32C (Castagnoli)x32 + x28 + x27 + x26 + x25 + x23 + x22 + x20 + x19 + x18 + x14 + x13 + x11 + x10 + x9 + x8 + x6 + 1 (G.hn payload)1EDC6F41 或 82F63B78 (8F6E37A0)
CRC-32K (Koopman)x32 + x30 + x29 + x28 + x26 + x20 + x19 + x17 +x16 + x15 + x11 + x10 + x7 + x6 + x4 + x2 + x + 1741B8CD7 或 EB31D82E (BA0DC66B)
CRC-64-ISOx64 + x4 + x3 + x + 1 (HDLC-ISO 3309)000000000000001B 或D800000000000000 (800000000000000D)
CRC-64-ECMA-182x64 + x62 + x57 + x55 + x54 + x53 + x52 + x47 + x46 + x45 + x40 + x39 + x38 + x37 + x35 + x33 + x32 + x31 + x29 + x27 + x24 + x23 + x22 + x21 + x19 + x17 + x13 + x12 + x10 + x9 + x7 + x4 + x + 1 (As described in ECMA-182 p.51)