设为首页收藏本站

新势力科技社区

 找回密码
 申,请,会,员

QQ登录

只需一步,快速开始

搜索
由于某种原因,我们论坛暂时只能转移到美国服务器存放,访问速度将受到一定影响,请大家谅解!
查看: 4114|回复: 2

[资料教程] CRC16 原理算法

[复制链接]

195

主题

0

好友

642

积分

管理员

Rank: 10Rank: 10Rank: 10

金钱
6034 新币
贡献
322 点
威望
129 点
最后登录
2017-3-9
精华
26
积分
642
帖子
320
发表于 2009-3-10 00:47:43 |显示全部楼层
2.5.1.2 CRC 校验
在RTU 模式包含一个对全部报文内容执行的,基于循环冗余校验 (CRC - Cyclical Redundancy
Checking) 算法的错误检验域。CRC 域检验整个报文的内容。不管报文有无奇偶校验,均执行此检
验。
CRC 包含由两个8 位字节组成的一个16 位值。
CRC 域作为报文的最后的域附加在报文之后。计算后,首先附加低字节,然后是高字节。CRC
高字节为报文发送的最后一个子节。
附加在报文后面的CRC 的值由发送设备计算。接收设备在接收报文时重新计算CRC 的值,
并将计算结果于实际接收到的CRC 值相比较。如果两个值不相等,则为错误。
CRC 的计算, 开始对一个16 位寄存器预装全1。然后将报文中的连续的8 位子节对其进行后
续的计算。只有字符中的8个数据位参与生成CRC 的运算,起始位,停止位和校验位不参与CRC
计算。
CRC 的生成过程中, 每个 8–位字符与寄存器中的值异或。然后结果向最低有效位(LSB)方向
移动(Shift) 1位,而最高有效位(MSB)位置充零。然后提取并检查LSB:如果LSB 为1, 则寄存
器中的值与一个固定的预置值异或;如果LSB 为 0, 则不进行异或操作。
这个过程将重复直到执行完8 次移位。完成最后一次(第8 次)移位及相关操作后,下一个8
位字节与寄存器的当前值异或,然后又同上面描述过的一样重复8 次。当所有报文中子节都运算之
后得到的寄存器忠的最终值,就是CRC。
当 CRC 附加在报文之后时,首先附加低字节,然后是高字节。


执行CRC 生成的C 语言的函数在下面示出。所有的可能的CRC 值都被预装在两个数组中,当计算报文内容
时可以简单的索引即可。一个数组含有16 位CRC 域的所有256个可能的高位字节,另一个数组含有地位字节的值。
这种索引访问CRC 的方式提供了比对报文缓冲区的每个新字符都计算新的CRC 更快的方法。
注意: 此函数内部执行高/低CRC 字节的交换。此函数返回的是已经经过交换的CRC 值。
也就是说,从该函数返回的CRC 值可以直接放置于报文用于发送。
函数使用两个参数:
unsigned char *puchMsg; 指向含有用于生成CRC 的二进制数据报文缓冲区的指针
unsigned short usDataLen; 报文缓冲区的字节数.
CRC 生成函数
  1. unsigned short CRC16 ( puchMsg, usDataLen ) /* 函数以unsigned short 类型返回CRC */
  2. unsigned char *puchMsg ; /* 用于计算CRC 的报文*/
  3. unsigned short usDataLen ; /* 报文中的字节数*/
  4. {
  5. unsigned char uchCRCHi = 0xFF ; /* CRC 的高字节初始化*/
  6. unsigned char uchCRCLo = 0xFF ; /* CRC 的低字节初始化*/
  7. unsigned uIndex ; /* CRC 查询表索引*/
  8. while (usDataLen--) /* 完成整个报文缓冲区*/
  9. {
  10. uIndex = uchCRCLo ^ *puchMsgg++ ; /* 计算CRC */
  11. uchCRCLo = uchCRCHi ^ auchCRCHi[uIndex} ;
  12. uchCRCHi = auchCRCLo[uIndex] ;
  13. }
  14. return (uchCRCHi << 8 | uchCRCLo) ;
  15. }
复制代码
  1. /* 高位字节的CRC 值*/
  2. static unsigned char auchCRCHi[] = {
  3. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
  4. 0x00, 0xC1, 0x81,
  5. 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81,
  6. 0x40, 0x01, 0xC0,
  7. 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1,
  8. 0x81, 0x40, 0x01,
  9. 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01,
  10. 0xC0, 0x80, 0x41,
  11. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
  12. 0x00, 0xC1, 0x81,
  13. 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80,
  14. 0x41, 0x01, 0xC0,
  15. 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
  16. 0x80, 0x41, 0x01,
  17. 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00,
  18. 0xC1, 0x81, 0x40,
  19. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
  20. 0x00, 0xC1, 0x81,
  21. 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81,
  22. 0x40, 0x01, 0xC0,
  23. 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1,
  24. 0x81, 0x40, 0x01,
  25. 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01,
  26. 0xC0, 0x80, 0x41,
  27. 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
  28. 0x00, 0xC1, 0x81,
  29. 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81,
  30. 0x40, 0x01, 0xC0,
  31. 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
  32. 0x80, 0x41, 0x01,
  33. 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01,
  34. 0xC0, 0x80, 0x41,
  35. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
  36. 0x00, 0xC1, 0x81,
  37. 0x40
  38. } ;
复制代码
  1. /* 低位字节的CRC 值*/
  2. static char auchCRCLo[] = {
  3. 0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06, 0x07, 0xC7,
  4. 0x05, 0xC5, 0xC4,
  5. 0x04, 0xCC, 0x0C, 0x0D, 0xCD, 0x0F, 0xCF, 0xCE, 0x0E, 0x0A, 0xCA, 0xCB,
  6. 0x0B, 0xC9, 0x09,
  7. 0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9, 0x1B, 0xDB, 0xDA, 0x1A, 0x1E, 0xDE,
  8. 0xDF, 0x1F, 0xDD,
  9. 0x1D, 0x1C, 0xDC, 0x14, 0xD4, 0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2,
  10. 0x12, 0x13, 0xD3,
  11. 0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3, 0xF2, 0x32,
  12. 0x36, 0xF6, 0xF7,
  13. 0x37, 0xF5, 0x35, 0x34, 0xF4, 0x3C, 0xFC, 0xFD, 0x3D, 0xFF, 0x3F, 0x3E,
  14. 0xFE, 0xFA, 0x3A,
  15. 0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38, 0x28, 0xE8, 0xE9, 0x29, 0xEB, 0x2B,
  16. 0x2A, 0xEA, 0xEE,
  17. 0x2E, 0x2F, 0xEF, 0x2D, 0xED, 0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xE5, 0x27,
  18. 0xE7, 0xE6, 0x26,
  19. 0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60, 0x61, 0xA1,
  20. 0x63, 0xA3, 0xA2,
  21. 0x62, 0x66, 0xA6, 0xA7, 0x67, 0xA5, 0x65, 0x64, 0xA4, 0x6C, 0xAC, 0xAD,
  22. 0x6D, 0xAF, 0x6F,
  23. 0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB, 0x69, 0xA9, 0xA8, 0x68, 0x78, 0xB8,
  24. 0xB9, 0x79, 0xBB,
  25. 0x7B, 0x7A, 0xBA, 0xBE, 0x7E, 0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4,
  26. 0x74, 0x75, 0xB5,
  27. 0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71, 0x70, 0xB0,
  28. 0x50, 0x90, 0x91,
  29. 0x51, 0x93, 0x53, 0x52, 0x92, 0x96, 0x56, 0x57, 0x97, 0x55, 0x95, 0x94,
  30. 0x54, 0x9C, 0x5C,
  31. 0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E, 0x5A, 0x9A, 0x9B, 0x5B, 0x99, 0x59,
  32. 0x58, 0x98, 0x88,
  33. 0x48, 0x49, 0x89, 0x4B, 0x8B, 0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D,
  34. 0x4D, 0x4C, 0x8C,
  35. 0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42, 0x43, 0x83,
  36. 0x41, 0x81, 0x80,
  37. 0x40
  38. };
复制代码
新势力将为您助上一臂之力!

0

主题

0

好友

469

积分

中级会员

Rank: 3Rank: 3

金钱
539 新币
贡献
0 点
威望
0 点
最后登录
2018-7-21
精华
0
积分
469
帖子
469
发表于 2018-7-10 18:26:01 |显示全部楼层
20180710 junda
adidas outlet
chrome hearts outlet
longchamp handbags
michael kors outlet
ray ban sunglasses
jack wolfskin
cheap nhl jerseys
five fingers shoes
chrome hearts outlet
moncler outlet
reebok outlet store
ugg outlet
swarovski outlet
ugg outlet
canada goose outlet
ugg outlet
tag heuer watches
new balance shoes
prada shoes
merrell shoes
columbia sportswear
supreme clothing
moncler outlet
ray ban sunglasses
malone souliers
kd 10 elite
lakers jerseys
canada goose outlet
mizuno running shoes
camel shoes
air max 90
true religion jeans
kevin durant jerseys
nba jerseys
dansko shoes
air max 90
longchamp outlet
tory burch outlet
swarovski outlet
nike factory outlet
michael kors
ray ban sunglasses
oakley sunglasses wholesale
birkenstock sandals
air max 90
kings jerseys
asics shoes
coach outlet store online
ferragamo outlet
ray ban sunglasses
fitflops shoes
ferragamo outlet
wellensteyn jackets
burberry outlet
dolce and gabbana
polo ralph lauren
tory burch outlet
michael kors outlet
ralph lauren outlet
rockets jerseys
coach outlet online
warriors jerseys
swarovski outlet
michael kors outlet
michael kors outlet
coach outlet
soccer jerseys
world cup jerseys
michael kors outlet
nike store uk
cheap jordans
nhl jerseys
polo ralph lauren
canada goose outlet
uggs outlet
supra shoes
stussy clothing
ray ban sunglasses
coach outlet
uggs outlet
miu miu handbags
kate spade outlet
huf clothing
harry winston jewelry
bape clothing
coach outlet online
moncler jackets
kevin durant shoes
ferragamo outlet
oakley sunglasses wholesale
ralph lauren polo shirts
cheap mlb jerseys
ugg boots
michael kors outlet
cleveland cavaliers jersey
michael kors outlet
pandora charms
michael kors outlet
houston texans jerseys
回复

使用道具 举报

2

主题

0

好友

172

积分

初级会员

Rank: 2

金钱
230 新币
贡献
0 点
威望
0 点
最后登录
2018-7-19
精华
0
积分
172
帖子
172
发表于 3 天前 |显示全部楼层
vibram fivefingers
nhl jerseys
adidas eqt
chrome hearts outlet
james harden shoes
nike x off white
nmd
michael kors factory outlet
jordan 12
adidas gazelle sale
fitflops sale
adidas superstar
crazy explosive
hermes bag
nba jerseys
oakley
gucci belt
air jordan 6
cheap oakley sunglasses
tom ford glasses
oakley
adidas shoes
bape clothing
yeezy boost
kobe byrant shoes
nike air force
kd shoes
hermes
golden goose
nike air zoom
lebron soldier 11
outlet golden goose
moncler outlet
adidas yeezy
converse shoes
hermes belt
tom ford glasses
jordan retro
yeezy shoes
adidas ultra boost
golden goose
fila outlet
lacoste outlet
air max 2018
nike dunks
adidas flux
custom baseball jerseys
kobe shoes
yeezy 500 blush
converse shoes
timberland outlet
vans shoes
nike free run
christian louboutin shoes
adidas superstar
yeezy 500
nike prestos
foamposites
links of london
hogan outlet
curry shoes
golden goose
tom ford sunglasses
jordan 6
yeezys
nmd
louboutin shoes
nike air force 1
goyard bags
lacoste polo
burberry outlet online
nike dunk low
ray ban sunglasses outlet
huaraches
adidas nmd
nike epic react flyknit
hermes belts
cheap jordans
nike air max 90
nike air vapormax
balenciaga
adidas yeezy
yeezy boost
nike roshe
golden goose sneakers
golden goose slide
curry 4
golden goose
michael kors factory outlet
nike hyperdunk
off white clothing
kobe shoes
nike flyknit racer
curry 4
christian louboutin
lebron james shoes
vans shoes
oakley sunglasses
supreme clothing
true religion
moncler jackets
hermes birkin
cheap nfl jerseys
golden goose starter
nike air max
ferragamo belt
nike huarache
kevin durant shoes
hermes outlet online
birkin bag
air jordan 11
adidas yeezy boost
longchamp
yeezy boost 350
nike air presto
nike outlet store online shopping
nike react
fila shoes
lebron 15
adidas stan smith
nike air zoom structure 19
john wall shoes
goyard handbags
nike outlet store
yeezy wave runner 700
lebron 13
yeezy
adidas yeezy
golden goose shoes
adidas tubular x
bape hoodie
moncler jackets
yeezy 700
asics shoes
golden goose
fila
patriots jersey
michael kors outlet store
golden goose outlet
lebron 15
air jordan 12
off-white
yeezy shoes
longchamp
nike polo
yeezy
michael kors handbags
air jordan
nike cortez women
kyrie 4
adidas nmd
michael kors outlet store
louboutin shoes
yeezy boost 350
oakley sale
nike huarache
huarache shoes
golden goose francy
pandora jewelry
russell westbrook shoes
yeezy
nike air force 1 low
kyrie 4
salvatore ferragamo belt
cheap mlb jerseys
puma fenty
adidas ultra boost 3.0
golden goose sneakers
adidas eqt support adv
nike air max
yeezy shoes
goyard handbags
caterpillar boots
nike hyperdunk
adidas stan smith women
lebron shoes
hermes birkin
jordan shoes
jordan retro 13
hermes birkin
yeezy
longchamp bags
off-white
lebron 15 shoes
michael kors
golden goose sale
michael kors purses
golden goose sneakers
timberland shoes
yeezy boost
pure boost
adidas iniki
oakley sunglasses
yeezy
nike zoom
moncler jackets
air max 2017
nike flyknit
balenciaga sneakers
bape hoodie
michael kors outlet
moncler sale
ray ban
air force ones
ray ban sunglasses
yeezy sneakers
westbrook shoes
hermes belts
new england patriots jersey
ray ban aviator sunglasses
nike air force
paul george shoes
links of london
nike react flyknit
yeezy boost 350
michael kors outlet
yeezy 500
burberry outlet
golden goose sneakers
basketball shoes
michael kors
longchamp handbags
nfl jerseys
off white
adidas tubular
off white jordan 1
adidas ultra boost
jordan 13
ysl handbags
moncler
michael kors outlet
yeezy boost 350 v2
kobe basketball shoes
zx flux
off white hoodie
longchamp bags
michael kors outlet online
paul george shoes
adidas superstar UK
nike air force 1 high
kyrie irving shoes
yeezy boost 350 v2
tom ford sunglasses
air max 270
oakleys sunglasses
iniki
curry 5
ray ban
vans outlet
red bottom shoes for women
supreme hoodie
nike lebron 15
kate spade handbags
cheap nhl jerseys
kayno
nike huarache
yeezy boost 350
ray ban
yeezy shoes
pandora charms
converse outlet
michael kors outlet online
rayban sunglasses
kobe sneakers
nike air max 2017
adidas pure boost
kevin durant shoes
chrome hearts
fitflops sale clearance
cheap oakley sunglasses
yeezy boost 350
goyard handbags
kobe byrant shoes
goyard wallet
nike flyknit
cheap nba jerseys
off white jordan 1
michael kors outlet
longchamp handbags
kobe basketball shoes
michael kors outlet
jordan shoes
chrome hearts online
adidas yeezy
yeezy boost 350 v2
adidas yeezy boost
adidas stan smith
ray ban sunglasses
true religion outlet
nike air max 90
harden shoes
nike sneakers
nike air force 1
nmd r1
golden goose superstar
nike shoes for women
air max 90
adidas gazelle
jordan retro
kd 10
air jordans
michael kors outlet online
michael kors handbags
ray ban sunglasses
nike basketball shoes
air max
birkin bag
adidas yeezy
yeezy shoes
kobe 11
nike shoes for men
caterpillar shoes
michael kors handbags sale
prada eyeglasses
michael kors handbags
nike epic react flyknit
yeezy boost 350
adidas iniki runner
dior sunglasses
michael kors handbags
fila shoes
balenciaga sneakers
longchamp handbags
nike shoes
goyard tote
moncler jackets
nfl store
tory burch shoes
longchamp longchamps
pg 1
nike huarache
yeezy boost 350 v2
off white nike
vans store
fenty puma
yeezy shoes
prada sunglasses
retro jordans
longchamp bags
adidas nmd
adidas ultra boost 3.0
air max
pandora charms
asics gel kayano
off white shoes
kobe 9
off white hoodie
jordan 4
kate spade outlet
links of london sale
kobe 11
ultra boost
yeezy boost 350 v2
adidas eqt
nike running shoes
chrome hearts
reebok outlet
air max 90
ray ban sunglasses online
lebron soldier 10
pandora jewelry
cheap jordan shoes
damian lillard shoes
supreme clothing
yeezy boost
mens basketball shoes
adidas nmd
real jordans for sale cheap
adidas ultra boost
vapormax
adidas stan smith men
nike shox outlet
russell westbrook shoes
nike hyperdunk 2017
nike lebron 11
hermes handbags
michael kors sale
nike air max 2018
ray ban sunglasses
christian louboutin shoes
balenciaga
yeezy boost 350 v2
nike air max
moncler
goyard
hermes belt
calvin klein outlet
jordan 11
jordan 1 off white
true religion jeans
hermes belt
cheap nfl jerseys china
basketball shoes
stephen curry shoes
nike off white
adidas nmd
nike shox
ferragamo sale
pureboost
d rose shoes
adidas outlet
nike mercurial
adidas stan smith uk
golden goose mid star
kyrie 3
adidas store
fila online shop
michael kors outlet
nike mercurial vapor
derrick rose shoes
ray ban store
michael kors
nike roshe one
adidas eqt support adv
chrome hearts outlet
michael kors outlet handbags
adidas nmd r1
pandora bracelet
yeezy
converse outlet store
pandora bracelet
nike air max shoes
yeezys
irving shoes
off white jordan 1
lacoste online shop
calvin klein underwear
gucci belts
longchamp
longchamp
balenciaga sneakers
adidas tubular
michael kors outlet
nike react
hyperdunks
cheap jordans
nike air max 270
hogan outlet online
asics sneakers
air max 270
michael kors
nike shox for women
jordan retro
mlb jerseys
yeezy shoes
golden goose
nike cortez
tory burch shoes
kobe 9
lebron 14
oakley
hermes handbags
michael kors outlet
ultra boost 3.0
curry 5
adidas yeezy
balenciaga shoes
nfl jerseys
nike flyknit trainer
moncler
oakley sunglasses
oakleys sunglasses
yeezy boost 350
nike air zoom pegasus 32
ferragamo belt
100% real jordans for cheap
jordan shoes
red bottom shoes
moncler outlet
balenciaga speed
huarache shoes
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 申,请,会,员

手机版|Archiver|新势力科技 ( 鲁ICP备05002753号  

GMT+8, 2018-7-22 09:14

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部