CRC校验原理与程序设计――(RS485总线系统应用之1)

时间:2018-08-17来源:网络

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)

1 2 3 4

关键词: CRC校验 RS485总线

加入微信
获取电子行业最新资讯
搜索微信公众号:EEPW

或用微信扫描左侧二维码

相关文章

查看电脑版