微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > CRC校验原理与程序设计——(RS485总线系统应用之1)

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

时间:12-15 来源:互联网 点击:

}

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-1

x + 1 (most hardware; also known as parity bit)

01或01 (01)

CRC-4-ITU

x4+x+ 1 (ITU G.704, p. 12)

03或0C (09)

CRC-5-ITU

x5+x4+x2+ 1 (ITU G.704, p. 9)

15或15 (1A)

CRC-5-USB

x5+x2+ 1 (USB token packets)

05或14 (12)

CRC-6-ITU

x6+x+ 1 (ITU G.704, p. 3)

03或30 (21)

CRC-7

x7+x3+ 1 (telecom systems, MMC,SD)

09或48 (44)

CRC-8-ATM

x8+x2+x+ 1 (ATM HEC)

07或E0 (83)

CRC-8-CCITT

x8+x7+x3+x2+ 1 (1-Wire bus)

8D或B1 (C6)

CRC-8-Dallas/Maxim

x8+x5+x4+ 1 (1-Wire bus)

31或8C (98)

CRC-8

x8+x7+x6+x4+x2+ 1

D5或AB (EA)

CRC-8-SAE J1850

x8+x4+x3+x2+ 1

1D或B8 (8E)

CRC-10

x10+x9+x5+x4+x+ 1

233或331 (319)

CRC-11

x11+x9+x8+x7+x+ 1 (FlexRay)

385或50E (5C2)

CRC-12

x12+x11+x3+x2+x+ 1 (telecom systems )

80F或F01 (C07)

CRC-15-CAN

x15+x14+x10+x8+x7+x4+x3+ 1

4599或4CD1 (62CC)

CRC-16-Fletcher

Not a CRC; see Fletchers checksum

Used in Adler-32 A & B CRCs

CRC-16-CCITT

x16+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-DNP

x16+x13+x12+x11+x10+x8+x6+x5+x2+ 1 (DNP, IEC 870, M-Bus)

3D65或A6BC (9EB2)

CRC-16-IBM

x16+x15+x2+ 1 (CRC-16, SDLC, USB, many others)

8005或A001 (C002)

CRC-24-Radix-64

x24+x23+x18+x17+x14+x11+x10+x7+x6+x5+x4+x3+x+ 1 (FlexRay)

864CFB或DF3261 (C3267D)

CRC-30

x30+x29+x21+x20+x15+x13+x12+x11+x8+x7+x6+x2+x+ 1 (CDMA)

2030B9C7或38E74301 (30185CE3)

CRC-32-Adler

Not a CRC; see Adler-32

See Adler-32

CRC-32-IEEE 802.3

x32+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+ 1

741B8CD7或EB31D82E (BA0DC66B)

CRC-64-ISO

x64+x4+x3+ x + 1 (HDLC-ISO 3309)

000000000000001B或D800000000000000 (800000000000000D)

CRC-64-ECMA-182

x64+ 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)

42F0E1EBA9EA3693或C96C5795D7870F42 (A17870F5D4F51B49)

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top