CRC校驗(yàn)原理與程序設(shè)計(jì)――(RS485總線系統(tǒng)應(yīng)用之1
Dim CL As Byte, CH As Byte, SaveLo As Byte, SaveHi As Byte
CRC16Hi = HFF '為16位CRC校驗(yàn)寄存器賦初始值 FFFF
CRC16Lo = HFF
CH = HA0 '為16位CRC校驗(yàn)多項(xiàng)式賦初始值 A001
CL = H1
For i = 1 To No
CRC16Lo = CRC16Lo Xor Data(i - 1) '每一個(gè)數(shù)據(jù)與CRC寄存器進(jìn)行異或
For j = 1 To 8
SaveHi = CRC16Hi
SaveLo = CRC16Lo
CRC16Hi = CRC16Hi 2 '高位右移一位
CRC16Lo = CRC16Lo 2 '低位右移一位
If ((SaveHi And H1) = H1) Then '如果高位字節(jié)最右一位為1
CRC16Lo = CRC16Lo Or H80 '則低位字節(jié)最左一位補(bǔ)1,否則自動(dòng)補(bǔ)0
End If
If ((SaveLo And H1) = H1) Then '如低位字節(jié)最右一位為1,則與多項(xiàng)式值異或
CRC16Hi = CRC16Hi Xor CH
CRC16Lo = CRC16Lo Xor CL
End If
Next j
Next i
End Function
參考文獻(xiàn):
① GB/T 19582.1-2008《基于Modbus協(xié)議的工業(yè)自動(dòng)化網(wǎng)絡(luò)規(guī)范》
②《Modbus協(xié)議中文版》 可在互聯(lián)網(wǎng)上下載
附表: CRC校驗(yàn)標(biāo)準(zhǔn)類型
CRC校驗(yàn)名多 項(xiàng) 式 (標(biāo)準(zhǔn)引用名)對(duì)應(yīng)的16進(jìn)制值或反值注:已去掉最高位的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)
評(píng)論