关于modbus的小错误!

[复制链接]
4282|11
 楼主| bl20020825 发表于 2007-8-19 17:18 | 显示全部楼层 |阅读模式
我看了网上modbus的资料很多,也很全,但都是一个模板!现在我在做modbus,看了协议,主要对modbus的CRC校验研究了一下,觉得网上的资料有”误导“新手的“嫌疑”,我将我的疑惑写在这,如果是我的错的话,请大家出来指点!<br />网上对modbus协议中的CRC校验的描述如下:<br />生成CRC-16校验字节的步骤如下:&nbsp;<br /><br />①装如一个16位寄存器,所有数位均为1。&nbsp;<br /><br />②该16位寄存器的高位字节与开始8位字节进行“异或”运算。运算结果放入这个16位寄存器。&nbsp;<br /><br />③把这个16寄存器向右移一位。&nbsp;<br /><br />④若向右(标记位)移出的数位是1,则生成多项式1010000000000001和这个寄存器进行“异或”运算;若向右移出的数位是0,则返回③。&nbsp;<br /><br />⑤重复③和④,直至移出8位。&nbsp;<br /><br />⑥另外8位与该十六位寄存器进行“异或”运算。&nbsp;<br /><br />⑦重复③~⑥,直至该报文所有字节均与16位寄存器进行“异或”运算,并移位8次。&nbsp;<br /><br />⑧这个16位寄存器的内容即2字节CRC错误校验,被加到报文的最高有效位。<br /><br />我的意见:<br />看了,整体上是对的,但是第②步就有点问题!应该是8bit数据跟16位寄存器的低位字节进行异或才对!如果是跟高位字节进行异或的话,那根据CRC协议,它应该是进行左移来判断是否进行异或多项式!<br />协议的英文原文是如下:<br />A&nbsp;procedure&nbsp;for&nbsp;generating&nbsp;a&nbsp;CRC&nbsp;is:<br />1,&nbsp;Load&nbsp;a&nbsp;16–bit&nbsp;register&nbsp;with&nbsp;FFFF&nbsp;hex&nbsp;(all&nbsp;1’s).&nbsp;Call&nbsp;this&nbsp;the&nbsp;CRC&nbsp;register.<br />2.&nbsp;Exclusive&nbsp;OR&nbsp;the&nbsp;first&nbsp;8–bit&nbsp;byte&nbsp;of&nbsp;the&nbsp;message&nbsp;with&nbsp;the&nbsp;low–order&nbsp;byte&nbsp;of&nbsp;the&nbsp;16–bit&nbsp;CRC&nbsp;register,&nbsp;putting&nbsp;the&nbsp;result&nbsp;in&nbsp;the&nbsp;CRC&nbsp;register.<br />3.&nbsp;Shift&nbsp;the&nbsp;CRC&nbsp;register&nbsp;one&nbsp;bit&nbsp;to&nbsp;the&nbsp;right&nbsp;(toward&nbsp;the&nbsp;LSB),&nbsp;zero–filling&nbsp;the&nbsp;MSB.&nbsp;Extract&nbsp;and&nbsp;examine&nbsp;the&nbsp;LSB.<br />4.&nbsp;(If&nbsp;the&nbsp;LSB&nbsp;was&nbsp;0):&nbsp;Repeat&nbsp;Step&nbsp;3&nbsp;(another&nbsp;shift).<br />(If&nbsp;the&nbsp;LSB&nbsp;was&nbsp;1):&nbsp;Exclusive&nbsp;OR&nbsp;the&nbsp;CRC&nbsp;register&nbsp;with&nbsp;the&nbsp;polynomial<br />value&nbsp;A001&nbsp;hex&nbsp;(1010&nbsp;0000&nbsp;0000&nbsp;0001).<br />5.&nbsp;Repeat&nbsp;Steps&nbsp;3&nbsp;and&nbsp;4&nbsp;until&nbsp;8&nbsp;shifts&nbsp;have&nbsp;been&nbsp;performed.&nbsp;When&nbsp;this&nbsp;is&nbsp;done,&nbsp;a&nbsp;complete&nbsp;8–bit&nbsp;byte&nbsp;will&nbsp;have&nbsp;been&nbsp;processed.<br />6.&nbsp;Repeat&nbsp;Steps&nbsp;2&nbsp;through&nbsp;5&nbsp;for&nbsp;the&nbsp;next&nbsp;8–bit&nbsp;byte&nbsp;of&nbsp;the&nbsp;message.<br />Continue&nbsp;doing&nbsp;this&nbsp;until&nbsp;all&nbsp;bytes&nbsp;have&nbsp;been&nbsp;processed.<br />7.&nbsp;The&nbsp;final&nbsp;contents&nbsp;of&nbsp;the&nbsp;CRC&nbsp;register&nbsp;is&nbsp;the&nbsp;CRC&nbsp;value.<br />8.&nbsp;When&nbsp;the&nbsp;CRC&nbsp;is&nbsp;placed&nbsp;into&nbsp;the&nbsp;message,&nbsp;its&nbsp;upper&nbsp;and&nbsp;lower&nbsp;bytes<br />must&nbsp;be&nbsp;swapped&nbsp;as&nbsp;described&nbsp;below.<br />大家可以自己看看英文原文和CRC校验定义,如果是我的理解有误忘大家指出来!谢谢浏览,请多多回帖!
chunyang 发表于 2007-8-19 18:25 | 显示全部楼层

看我写的老帖

里面有正确的CRC生成方法,亦可用于Modbus。不过CRC的计算方法很多,数学上的步骤不是唯一的,需要验证或推导后才好说对错。
 楼主| bl20020825 发表于 2007-8-19 19:58 | 显示全部楼层

我的疑问是否存在呢??不应该跟高字节相异或

但请问,我的疑问是否对呢??第②步的应该是8bit数据跟16位寄存器的低位字节进行异或才对?
 楼主| bl20020825 发表于 2007-8-19 20:07 | 显示全部楼层

chunyang先生出来说说啊!

在modbus里,我个人从英文原版来说,应该是来的字节数据应该跟16位的寄存器直接异或,即跟低字节异或!但我用程序写就出现问题了,就是用资料提供的查表法跟我用一位一位移动然后异或的方法计算出来的值不一样啊!<br />就比如字节数据是1,用查表法算出来的CRC值是32384,而如果是按着上面(跟低位异或)的方法来运行的话,算出来的是32894!不知到这是何原因???
chunyang 发表于 2007-8-19 20:17 | 显示全部楼层

用我写的方法去做就行,然后对比结果

你有兴趣也可以在纸上推演验证一番,你不会让我来替你做吧。严格说,运算高位或运算低位都行,但过程不同,结果相同。当然,你找到的算法是否正确,我不能在未推演的前提下判断,推演的工作你该自己进行。
 楼主| bl20020825 发表于 2007-8-19 20:32 | 显示全部楼层

这样我将我的程序贴出来!

我将我的程序贴出来!还有你的贴子真是好多,找不到哪个才是我需要的!<br />还有个问题,敬请回答,就是我的疑问是不是对的啊!从英文资料来说,它的CRC形成步骤应该是跟16位寄存器的低字节相异或而不是高字节,这样才会进行右移判断是否异或!!???<br />我的代码,都是对数据d=1&nbsp;,进行CRC编码的!<br />首先是查表法:这跟modbus&nbsp;guide文档有的:<br />/*&nbsp;CRC&nbsp;高位字节值表&nbsp;*/&nbsp;<br />unsigned&nbsp;char&nbsp;auchCRCHi[]&nbsp;=&nbsp;{&nbsp;<br />0x00,&nbsp;0xC1,&nbsp;0x81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bsp;<br />0x01,&nbsp;0xC0,&nbsp;0x80,&nbsp;0x41,&nbsp;0x01,&nbsp;0xC0,&nbsp;0x80,&nbsp;0x41,&nbsp;0x00,&nbsp;0xC1,&nbsp;<br />0x81,&nbsp;0x40,&nbsp;0x01,&nbsp;0xC0,&nbsp;0x80,&nbsp;0x41,&nbsp;0x00,&nbsp;0xC1,&nbsp;0x81,&nbsp;0x40,&nbsp;<br />0x00,&nbsp;0xC1,&nbsp;0x81,&nbsp;0x40,&nbsp;0x01,&nbsp;0xC0,&nbsp;0x80,&nbsp;0x41,&nbsp;0x01,&nbsp;0xC0,&nbsp;<br />0x80,&nbsp;0x41,&nbsp;0x00,&nbsp;0xC1,&nbsp;0x81,&nbsp;0x40,&nbsp;0x00,&nbsp;0xC1,&nbsp;0x81,&nbsp;0x40,&nbsp;<br />0x01,&nbsp;0xC0,&nbsp;0x80,&nbsp;0x41,&nbsp;0x00,&nbsp;0xC1,&nbsp;0x81,&nbsp;0x40,&nbsp;0x01,&nbsp;0xC0,&nbsp;<br />0x80,&nbsp;0x41,&nbsp;0x01,&nbsp;0xC0,&nbsp;0x80,&nbsp;0x41,&nbsp;0x00,&nbsp;0xC1,&nbsp;0x81,&nbsp;0x40,&nbsp;<br />0x00,&nbsp;0xC1,&nbsp;0x81,&nbsp;0x40,&nbsp;0x01,&nbsp;0xC0,&nbsp;0x80,&nbsp;0x41,&nbsp;0x01,&nbsp;0xC0,&nbsp;<br />0x80,&nbsp;0x41,&nbsp;0x00,&nbsp;0xC1,&nbsp;0x81,&nbsp;0x40,&nbsp;0x01,&nbsp;0xC0,&nbsp;0x80,&nbsp;0x41,&nbsp;<br />0x00,&nbsp;0xC1,&nbsp;0x81,&nbsp;0x40,&nbsp;0x00,&nbsp;0xC1,&nbsp;0x81,&nbsp;0x40,&nbsp;0x01,&nbsp;0xC0,&nbsp;<br />0x80,&nbsp;0x41,&nbsp;0x00,&nbsp;0xC1,&nbsp;0x81,&nbsp;0x40,&nbsp;0x01,&nbsp;0xC0,&nbsp;0x80,&nbsp;0x41,&nbsp;<br />0x01,&nbsp;0xC0,&nbsp;0x80,&nbsp;0x41,&nbsp;0x00,&nbsp;0xC1,&nbsp;0x81,&nbsp;0x40,&nbsp;0x01,&nbsp;0xC0,&nbsp;<br />0x80,&nbsp;0x41,&nbsp;0x00,&nbsp;0xC1,&nbsp;0x81,&nbsp;0x40,&nbsp;0x00,&nbsp;0xC1,&nbsp;0x81,&nbsp;0x40,&nbsp;<br />0x01,&nbsp;0xC0,&nbsp;0x80,&nbsp;0x41,&nbsp;0x01,&nbsp;0xC0,&nbsp;0x80,&nbsp;0x41,&nbsp;0x00,&nbsp;0xC1,&nbsp;<br />0x81,&nbsp;0x40,&nbsp;0x00,&nbsp;0xC1,&nbsp;0x81,&nbsp;0x40,&nbsp;0x01,&nbsp;0xC0,&nbsp;0x80,&nbsp;0x41,&nbsp;<br />0x00,&nbsp;0xC1,&nbsp;0x81,&nbsp;0x40,&nbsp;0x01,&nbsp;0xC0,&nbsp;0x80,&nbsp;0x41,&nbsp;0x01,&nbsp;0xC0,&nbsp;<br />0x80,&nbsp;0x41,&nbsp;0x00,&nbsp;0xC1,&nbsp;0x81,&nbsp;0x40&nbsp;<br />}&nbsp;;&nbsp;<br />/*&nbsp;CRC低位字节值表*/&nbsp;<br />unsigned&nbsp;char&nbsp;auchCRCLo[]&nbsp;=&nbsp;{&nbsp;<br />0x00,&nbsp;0xC0,&nbsp;0xC1,&nbsp;0x01,&nbsp;0xC3,&nbsp;0x03,&nbsp;0x02,&nbsp;0xC2,&nbsp;0xC6,&nbsp;0x06,&nbsp;<br />0x07,&nbsp;0xC7,&nbsp;0x05,&nbsp;0xC5,&nbsp;0xC4,&nbsp;0x04,&nbsp;0xCC,&nbsp;0x0C,&nbsp;0x0D,&nbsp;0xCD,&nbsp;<br />0x0F,&nbsp;0xCF,&nbsp;0xCE,&nbsp;0x0E,&nbsp;0x0A,&nbsp;0xCA,&nbsp;0xCB,&nbsp;0x0B,&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p;0x5E,&nbsp;0x5A,&nbsp;0x9A,&nbsp;0x9B,&nbsp;0x5B,&nbsp;<br />0x99,&nbsp;0x59,&nbsp;0x58,&nbsp;0x98,&nbsp;0x88,&nbsp;0x48,&nbsp;0x49,&nbsp;0x89,&nbsp;0x4B,&nbsp;0x8B,&nbsp;<br />0x8A,&nbsp;0x4A,&nbsp;0x4E,&nbsp;0x8E,&nbsp;0x8F,&nbsp;0x4F,&nbsp;0x8D,&nbsp;0x4D,&nbsp;0x4C,&nbsp;0x8C,&nbsp;<br />0x44,&nbsp;0x84,&nbsp;0x85,&nbsp;0x45,&nbsp;0x87,&nbsp;0x47,&nbsp;0x46,&nbsp;0x86,&nbsp;0x82,&nbsp;0x42,&nbsp;<br />0x43,&nbsp;0x83,&nbsp;0x41,&nbsp;0x81,&nbsp;0x80,&nbsp;0x40&nbsp;<br />}&nbsp;;<br />unsigned&nbsp;char&nbsp;uchCRCHi&nbsp;=&nbsp;0xFF&nbsp;;&nbsp;/*&nbsp;高CRC字节初始化&nbsp;*/&nbsp;<br />unsigned&nbsp;char&nbsp;uchCRCLo&nbsp;=&nbsp;0xFF&nbsp;;&nbsp;/*&nbsp;低CRC&nbsp;字节初始化&nbsp;*/&nbsp;<br />unsigned&nbsp;char&nbsp;d;<br />Uint16&nbsp;e;<br />Uint32&nbsp;uIndex&nbsp;;&nbsp;/*&nbsp;CRC循环中的索引&nbsp;*/&nbsp;<br />main()<br />{&nbsp;&nbsp;&nbsp;&nbsp;<br />d&nbsp;=&nbsp;1;<br />uIndex&nbsp;=&nbsp;uchCRCHi&nbsp;^&nbsp;d&nbsp;;&nbsp;/*&nbsp;计算CRC&nbsp;*/&nbsp;<br />uchCRCHi&nbsp;=&nbsp;uchCRCLo&nbsp;^&nbsp;auchCRCHi[uIndex]&nbsp;;&nbsp;<br />uchCRCLo&nbsp;=&nbsp;auchCRCLo[uIndex]&nbsp;;&nbsp;<br />e=(uchCRCHi&nbsp;&lt&lt&nbsp;8&nbsp;|&nbsp;uchCRCLo)&nbsp;;<br />}<br /><br />下面就是根据我对modbus的crc生成步骤的理解,来实现CRC的:也是对d=1进行crc编码<br />Uint16&nbsp;c=&nbsp;0xffff,e=0;<br />unsigned&nbsp;char&nbsp;d,f;<br />main()<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;d&nbsp;=&nbsp;1!<br />&nbsp;&nbsp;&nbsp;&nbsp;e&nbsp;=&nbsp;c^(unsigned&nbsp;int)d;//跟低位字节进行异或运算<br />&nbsp;&nbsp;&nbsp;&nbsp;for(f=0;f&lt8;f++)<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(e&1)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e&nbsp;&gt&gt=&nbsp;1;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e&nbsp;^=&nbsp;0xa001;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else&nbsp;e&nbsp;&gt&gt=&nbsp;1;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br />如果是照你的理解的话,跟高位运算只要改一下e&nbsp;=&nbsp;c^(unsigned&nbsp;int)d;就可以拉,但运行的结果,三个代码都是不一样的,这是为何啊???敬请chunyang前辈解答一下!!!
chunyang 发表于 2007-8-19 21:40 | 显示全部楼层

“我的理解”你根本就没有体会到

针对高位或针对低位的算法过程是不同的,不是改一下就行,先好好理解运算过程吧。
 楼主| bl20020825 发表于 2007-8-19 23:26 | 显示全部楼层

我对于你的回答,不是很理解!

我个人认为我的理解没有错!我看了你的blog里面的关于modbus的协议!你这样辛苦的翻译,我深表敬佩!但我也要指出的是,你的中文协议对CRC校验翻译有点问题!我要说明的一点,我对CRC校验还是理解的,虽然没您理解得透彻!在CRC校验实现过程中针对高位和低位的操作其实只是数据模2除法的变通,机制是一样的!如果是CRC进行的是向低位移出的话,必然要将字节数据异或放在低位字节!一般来说,CRC校验码的生成,都是采用高位移出,因为这样是标准模2运算!但其实低位移出,跟模2运算机制也一样,效果一样!虽然刚开始引入低位运算只是为了数据流在传输过程中反向了(高位和低位对调了),其实不管反不反向,对数据的低位操作生成的CRC编码,检错率是一样的,只要通信双方采用一样的移位算法就可以拉!看一下modbus的英文协议就可以知道,modbus里采用的就是低位移出算法!如果我的知识有限的话,请chun先生多多指点!还有我上面贴出来的程序,两个都是正确的!第一个查表法,刚开始时,我理解出错,其实uchCRCLo是最后crc校验码的高字节
chunyang 发表于 2007-8-20 00:44 | 显示全部楼层

一堆感叹号

如果正确,结果将一样,我给出的算法步骤应是比较简单的。至于你找到的算法和你的理解,我前面已经讲过,我不能在未推演的前提下判断正误,而推演的工作你该自己进行。
erhui_cn 发表于 2007-8-20 13:33 | 显示全部楼层

re:

  
erhui_cn 发表于 2007-8-20 13:36 | 显示全部楼层

re:

你先用网上的查表法测试一组数,得出结果,然后与你那些移法得出的结果比较,哪个对就是哪个移法对吧<br /><br />说到底,你知道移来移去的意思吗?
 楼主| bl20020825 发表于 2007-8-20 18:32 | 显示全部楼层

知道移位的意思啊!

只要你理解了CRC的定义得话,掌握模2除法的精髓,结果应该是一样的!其实我发这个帖是为了让网上的资料清楚点,只是想更正一下,有益于我们新手的了理解!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

30

主题

57

帖子

1

粉丝
快速回复 在线客服 返回列表 返回顶部