{"id":27940,"date":"2024-04-29T00:40:34","date_gmt":"2024-04-28T16:40:34","guid":{"rendered":"https:\/\/www.modbus.cn\/?p=27940"},"modified":"2026-06-30T23:54:43","modified_gmt":"2026-06-30T15:54:43","slug":"modbus-cha-cuo-jiao-yan","status":"publish","type":"post","link":"https:\/\/www.modbus.cn\/en\/27940.html","title":{"rendered":"\u6df1\u5165\u7406\u89e3Modbus\u7684LRC\u548cCRC\u6821\u9a8c"},"content":{"rendered":"<p class=\"wp-block-paragraph\">\u57fa\u4e8e\u4e32\u884c\u94fe\u8def\u7684 Modbus \u901a\u4fe1\u9700\u8981\u8fdb\u884c\u5dee\u9519\u6821\u9a8c\uff0c\u6839\u636e\u4f20\u8f93\u6a21\u5f0f\uff08ASCII \u6216 RTU\uff09\u7684\u4e0d\u540c\uff0c\u5dee\u9519\u6821\u9a8c\u57df\u91c7\u7528\u4e0d\u540c\u7684\u6821\u9a8c\u65b9\u6cd5\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>ASCII \u6a21\u5f0f<\/strong>\u00a0\u2014\u2014 \u6821\u9a8c\u5b57\u6bb5\u7531\u4e24\u4e2a\u5b57\u7b26\u7ec4\u6210\uff0c\u5176\u503c\u662f\u57fa\u4e8e\u5bf9\u5168\u90e8\u62a5\u6587\u5185\u5bb9\u6267\u884c\u00a0<strong>LRC<\/strong>\u00a0\u6821\u9a8c\uff08Longitudinal Redundancy Check\uff0c\u7eb5\u5411\u5197\u4f59\u6821\u9a8c\uff09\u7684\u8ba1\u7b97\u7ed3\u679c\u3002\u8ba1\u7b97\u5bf9\u8c61\u4e0d\u5305\u62ec\u8d77\u59cb\u7684\u5192\u53f7\uff08<code>:<\/code>\uff09\u548c\u56de\u8f66\u6362\u884c\u7b26\u53f7\uff08<code>CRLF<\/code>\uff09\u3002<\/li>\n\n\n\n<li><strong>RTU \u6a21\u5f0f<\/strong>\u00a0\u2014\u2014 \u6821\u9a8c\u5b57\u6bb5\u7531 16 \u4e2a\u6bd4\u7279\u4f4d\uff08\u4e24\u4e2a\u5b57\u8282\uff09\u7ec4\u6210\uff0c\u5176\u503c\u662f\u57fa\u4e8e\u5bf9\u5168\u90e8\u62a5\u6587\u5185\u5bb9\u6267\u884c\u00a0<strong>CRC<\/strong>\u00a0\u6821\u9a8c\uff08Cyclical Redundancy Check\uff0c\u5faa\u73af\u5197\u4f59\u6821\u9a8c\uff09\u7684\u8ba1\u7b97\u7ed3\u679c\u3002\u8ba1\u7b97\u5bf9\u8c61\u5305\u62ec\u6821\u9a8c\u57df\u4e4b\u524d\u7684\u6240\u6709\u5b57\u8282\u3002<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"lrc-\u6821\u9a8c\">LRC \u6821\u9a8c<a href=\"https:\/\/getiot.tech\/zh\/modbus\/modbus-checksum#lrc-%E6%A0%A1%E9%AA%8C\" rel=\"noopener noreferrer nofollow\">\u200b<\/a><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">LRC \u6821\u9a8c\u6bd4\u8f83\u7b80\u5355\uff0c\u5b83\u5728 ASCII \u534f\u8bae\u4e2d\u4f7f\u7528\uff0c\u68c0\u6d4b\u4e86\u6d88\u606f\u57df\u4e2d\u9664\u5f00\u59cb\u7684\u5192\u53f7\u53ca\u7ed3\u675f\u7684\u56de\u8f66\u6362\u884c\u53f7\u5916\u7684\u5185\u5bb9\u3002\u5b83\u4ec5\u4ec5\u662f\u628a\u6bcf\u4e00\u4e2a\u9700\u8981\u4f20\u8f93\u7684\u6570\u636e\u6309\u5b57\u8282\u53e0\u52a0\uff08\u4e22\u5f03\u6240\u6709\u8fdb\u4f4d\uff09\uff0c\u7136\u540e\u5bf9\u7ed3\u679c\u8fdb\u884c\u4e8c\u8fdb\u5236\u8865\u7801\u64cd\u4f5c\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0b\u9762\u662f LRC \u6821\u9a8c\u7684\u5177\u4f53\u4ee3\u7801\uff1a<\/p>\n\n\n\n<div class=\"wp-block-wpcom-hljs\"><pre class=\"\"><code>unsigned char LRC(unsigned char *auchMsg, unsigned short usDataLen)\n{\n    unsigned char uchLRC = 0;\n    while (usDataLen--)\n        uchLRC += *auchMsg++;\n    return ((unsigned char)(-((char)uchLRC)));\n}<\/code><\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ece\u7b97\u6cd5\u672c\u8d28\u6765\u8bf4\uff0cLRC \u57df\u672c\u8eab\u4ec5\u5360 1 \u4e2a\u5b57\u8282\uff0c\u4f46\u5728 ASCII \u6a21\u5f0f\u4f20\u9012\u6d88\u606f\u5e27\u65f6\uff0cLRC \u503c\u88ab\u7f16\u7801\u4e3a 2 \u4e2a\u5b57\u8282\u7684 ASCII \u5b57\u7b26\u3002\u4f8b\u5982\uff0c\u8ba1\u7b97\u5f97\u5230\u7684 LRC \u503c\u4e3a 0xF3\uff0c\u90a3\u4e48\u5728 ASCII \u6d88\u606f\u5e27\u4e2d\u8868\u793a\u4e3a &#8216;F&#8217; \u548c &#8216;3&#8217; \u4e24\u4e2a\u5b57\u7b26\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"crc-\u6821\u9a8c\">CRC Check<a href=\"https:\/\/getiot.tech\/zh\/modbus\/modbus-checksum#crc-%E6%A0%A1%E9%AA%8C\" rel=\"noopener noreferrer nofollow\">\u200b<\/a><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">CRC \u57df\u662f\u4e24\u4e2a\u5b57\u8282\uff0c\u5305\u542b\u4e00\u4e2a 16 \u4f4d\u7684\u4e8c\u8fdb\u5236\u503c\u3002\u5b83\u7531\u4f20\u8f93\u8bbe\u5907\u8ba1\u7b97\u540e\u52a0\u5165\u5230\u6d88\u606f\u4e2d\u3002\u63a5\u6536\u8bbe\u5907\u91cd\u65b0\u8ba1\u7b97\u6536\u5230\u6d88\u606f\u7684 CRC\uff0c\u5e76\u4e0e\u63a5\u6536\u5230\u7684 CRC \u57df\u4e2d\u7684\u503c\u6bd4\u8f83\uff0c\u5982\u679c\u4e24\u503c\u4e0d\u540c\uff0c\u5219\u6709\u8bef\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">CRC \u662f\u5148\u8c03\u5165\u4e00\u503c\u662f\u5168\u201c1\u201d\u7684 16 \u4f4d\u5bc4\u5b58\u5668\uff0c\u7136\u540e\u8c03\u7528\u4e00\u8fc7\u7a0b\u5c06\u6d88\u606f\u4e2d\u8fde\u7eed\u7684 8 \u4f4d\u5b57\u8282\u5404\u5f53\u524d\u5bc4\u5b58\u5668\u4e2d\u7684\u503c\u8fdb\u884c\u5904\u7406\u3002\u4ec5\u6bcf\u4e2a\u5b57\u7b26\u4e2d\u7684 8 \u4f4d\u6570\u636e\u5bf9 CRC \u6709\u6548\uff0c\u8d77\u59cb\u4f4d\u548c\u505c\u6b62\u4f4d\u4ee5\u53ca\u5947\u5076\u6821\u9a8c\u4f4d\u5747\u65e0\u6548\u3002 CRC \u4ea7\u751f\u8fc7\u7a0b\u4e2d\uff0c\u6bcf\u4e2a 8 \u4f4d\u5b57\u7b26\u90fd\u5355\u72ec\u548c\u5bc4\u5b58\u5668\u5185\u5bb9\u76f8\u6216\uff08OR\uff09\uff0c\u7ed3\u679c\u5411\u6700\u4f4e\u6709\u6548\u4f4d\u65b9\u5411\u79fb\u52a8\uff0c\u6700\u9ad8\u6709\u6548\u4f4d\u4ee5 0 \u586b\u5145\u3002LSB \u88ab\u63d0\u53d6\u51fa\u6765\u68c0\u6d4b\uff0c\u5982\u679c LSB \u4e3a 1\uff0c\u5bc4\u5b58\u5668\u5355\u72ec\u548c\u9884\u7f6e\u7684\u503c\u6216\u4e00\u4e0b\uff0c\u5982\u679c LSB \u4e3a 0\uff0c\u5219\u4e0d\u8fdb\u884c\u3002\u6574\u4e2a\u8fc7\u7a0b\u8981\u91cd\u590d 8 \u6b21\u3002\u5728\u6700\u540e\u4e00\u4f4d\uff08\u7b2c 8 \u4f4d\uff09\u5b8c\u6210\u540e\uff0c\u4e0b\u4e00\u4e2a 8 \u4f4d\u5b57\u8282\u53c8\u5355\u72ec\u548c\u5bc4\u5b58\u5668\u7684\u5f53\u524d\u503c\u76f8\u6216\u3002\u6700\u7ec8\u5bc4\u5b58\u5668\u4e2d\u7684\u503c\uff0c\u662f\u6d88\u606f\u4e2d\u6240\u6709\u7684\u5b57\u8282\u90fd\u6267\u884c\u4e4b\u540e\u7684 CRC \u503c\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0b\u9762\u662f CRC \u6821\u9a8c\u7684\u5177\u4f53\u4ee3\u7801\uff1a<\/p>\n\n\n\n<div class=\"wp-block-wpcom-hljs\"><pre class=\"\"><code>unsigned short CRC16(unsigned char *puchMsg, unsigned short usDataLen)\n{\n    int i, j;\n    unsigned short usRegCRC = 0xFFFF;\n    \n    for (i=0; i&lt;usDataLen; i++)\n    {\n        usRegCRC ^= *puchMsg++;\n        for (j=0; j&lt;8; j++)\n        {\n            if (usRegCRC &amp; 0x0001)\n                usRegCRC = usRegCRC >> 1 ^ 0xA001;\n            else\n                usRegCRC >>= 1;\n        }\n    }\n    \n    return usRegCRC;\n}<\/code><\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 Modbus RTU \u6a21\u5f0f\u4e2d\uff0c\u89c4\u5b9a\u4e86\u5728\u6d88\u606f\u5e27\u4f20\u9012\u65f6 CRC \u6821\u9a8c\u503c\u5fc5\u987b\u6309\u4f4e\u5b57\u8282\u5728\u524d\uff0c\u9ad8\u5b57\u8282\u5728\u540e\u7684\u987a\u5e8f\u3002\u4f8b\u5982\uff0c\u8ba1\u7b97\u5f97\u5230\u7684 CRC \u503c\u4e3a 0xCA31\uff0c\u90a3\u4e48\u53d1\u9001\u987a\u5e8f\u4e3a 0x31\u30010xCA\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0a\u9762\u7684 CRC16 \u8ba1\u7b97\u65b9\u6cd5\u5e76\u4e0d\u662f\u6700\u4f18\u7684\uff0c\u56e0\u4e3a\u5b83\u9700\u8981\u5927\u91cf\u7684\u8fd0\u7b97\uff0c\u53ea\u9002\u7528\u4e8e\u5bf9\u901f\u5ea6\u4e0d\u654f\u611f\u7684\u573a\u5408\u3002\u5bf9\u8fd0\u7b97\u901f\u5ea6\u6709\u8981\u6c42\u7684\uff0c\u53ef\u4ee5\u91c7\u7528\u67e5\u8868\u6cd5\u5f97\u5230\u6821\u9a8c\u503c\uff0c\u662f\u4e00\u79cd\u4ee5\u7a7a\u95f4\u6362\u65f6\u95f4\u7684\u7b56\u7565\u3002<\/p>\n<span id=\"magicpostMarker\"><\/span>","protected":false},"excerpt":{"rendered":"<p>\u57fa\u4e8e\u4e32\u884c\u94fe\u8def\u7684 Modbus \u901a\u4fe1\u9700\u8981\u8fdb\u884c\u5dee\u9519\u6821\u9a8c\uff0c\u6839\u636e\u4f20\u8f93&#8230;<\/p>","protected":false},"author":1,"featured_media":27977,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","_members_access_role":[],"_members_access_error":""},"categories":[1394],"tags":[905,630,629,632,689],"class_list":["post-27940","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-modbus-protocol","tag-char","tag-crc","tag-lrc","tag-unsigned","tag-689"],"_links":{"self":[{"href":"https:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/posts\/27940","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/comments?post=27940"}],"version-history":[{"count":0,"href":"https:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/posts\/27940\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/media\/27977"}],"wp:attachment":[{"href":"https:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/media?parent=27940"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/categories?post=27940"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/tags?post=27940"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}