{"id":44511,"date":"2026-06-28T14:16:52","date_gmt":"2026-06-28T06:16:52","guid":{"rendered":"https:\/\/www.modbus.cn\/44511.html"},"modified":"2026-06-28T14:46:13","modified_gmt":"2026-06-28T06:46:13","slug":"modbus-crc-lrc-complete-guide","status":"publish","type":"post","link":"https:\/\/www.modbus.cn\/en\/44511.html","title":{"rendered":"Modbus CRC\/LRC \u6821\u9a8c\u539f\u7406\u4e0e\u7f16\u7a0b\u5b9e\u73b0\uff1a\u4ece\u6570\u5b66\u63a8\u5bfc\u5230\u591a\u8bed\u8a00\u4ee3\u7801\u5b9e\u6218"},"content":{"rendered":"\n<h1 class=\"wp-block-heading\">Modbus CRC\/LRC \u6821\u9a8c\u539f\u7406\u4e0e\u7f16\u7a0b\u5b9e\u73b0\uff1a\u4ece\u6570\u5b66\u63a8\u5bfc\u5230\u4ee3\u7801\u5b9e\u6218<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u5de5\u4e1a\u901a\u4fe1\u4e2d\uff0c\u6570\u636e\u5b8c\u6574\u6027\u662f\u5e95\u7ebf\u3002\u4e00\u4e2a\u6bd4\u7279\u7684\u9519\u8bef\u53ef\u80fd\u5bfc\u81f4\u9600\u95e8\u8bef\u5f00\u3001\u7535\u673a\u53cd\u8f6c\u3001\u751a\u81f3\u5b89\u5168\u4e8b\u6545\u3002Modbus \u534f\u8bae\u901a\u8fc7 <strong>CRC\uff08\u5faa\u73af\u5197\u4f59\u6821\u9a8c\uff09<\/strong>\u548c <strong>LRC\uff08\u7eb5\u5411\u5197\u4f59\u6821\u9a8c\uff09<\/strong>\u4e24\u79cd\u673a\u5236\u6765\u4fdd\u969c\u6570\u636e\u5b8c\u6574\u6027\u3002\u672c\u6587\u5c06\u6df1\u5165\u5256\u6790\u8fd9\u4e24\u79cd\u6821\u9a8c\u7684\u6570\u5b66\u539f\u7406\uff0c\u5e76\u7ed9\u51fa C\u3001Python\u3001JavaScript \u4e09\u79cd\u8bed\u8a00\u7684\u5b8c\u6574\u4ee3\u7801\u5b9e\u73b0\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6838\u5fc3\u5173\u952e\u8bcd\uff1a<\/strong>Modbus CRC \u6821\u9a8c\u3001Modbus CRC-16\u3001CRC \u8ba1\u7b97\u539f\u7406\u3001Modbus LRC \u6821\u9a8c\u3001\u67e5\u8868\u6cd5 CRC\u3002\u66f4\u591a Modbus \u6280\u672f\u6587\u7ae0\u8bf7\u8bbf\u95ee <a href=\"https:\/\/modbus.cn\" rel=\"noopener noreferrer nofollow\">modbus.cn<\/a>\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e00\u3001\u4e3a\u4ec0\u4e48\u9700\u8981\u9519\u8bef\u68c0\u6d4b\uff1f<\/h2>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img decoding=\"async\" src=\"https:\/\/www.modbus.cn\/wp-content\/uploads\/diagrams\/a08-crc-flowchart.svg\" style=\"max-width:100%;height:auto\" \/ loading=\"lazy\" title=\"Modbus CRC\/LRC \u6821\u9a8c\u539f\u7406\u4e0e\u7f16\u7a0b\u5b9e\u73b0\uff1a\u4ece\u6570\u5b66\u63a8\u5bfc\u5230\u591a\u8bed\u8a00\u4ee3\u7801\u5b9e\u6218\u63d2\u56fe\" alt=\"Modbus CRC\/LRC \u6821\u9a8c\u539f\u7406\u4e0e\u7f16\u7a0b\u5b9e\u73b0\uff1a\u4ece\u6570\u5b66\u63a8\u5bfc\u5230\u591a\u8bed\u8a00\u4ee3\u7801\u5b9e\u6218\u63d2\u56fe\" \/><figcaption class=\"wp-element-caption\">\u25b2 \u56fe1\uff1aCRC-16 \u5b8c\u6574\u8ba1\u7b97\u6d41\u7a0b\uff08\u4f4d\u79fb\u6cd5\u9010\u6b65\u6f14\u793a\uff09\uff0c\u542b C \u8bed\u8a00\u4ee3\u7801\u793a\u4f8b\u3002<\/figcaption><\/figure>\n<\/div>\n\n\n\n\n\n\n\n\n<p class=\"wp-block-paragraph\">Modbus \u6700\u65e9\u8fd0\u884c\u5728 RS-485 \u548c RS-232 \u7269\u7406\u5c42\u4e0a\uff0c\u8fd9\u4e9b\u4e32\u884c\u94fe\u8def\u9762\u4e34\u4ee5\u4e0b\u5e72\u6270\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u7535\u78c1\u5e72\u6270\uff08EMI\uff09\uff1a<\/strong>\u53d8\u9891\u5668\u3001\u5927\u529f\u7387\u7535\u673a\u5728\u5de5\u4e1a\u73b0\u573a\u4ea7\u751f\u7684\u5927\u91cf\u7535\u78c1\u566a\u58f0\u4f1a\u8026\u5408\u5230\u901a\u4fe1\u7ebf\u8def\u4e0a<\/li>\n<li><strong>\u63a5\u5730\u7535\u4f4d\u5dee\uff1a<\/strong>\u957f\u8ddd\u79bb\u901a\u4fe1\u4e2d\uff0c\u4e0d\u540c\u8282\u70b9\u7684\u5730\u7535\u4f4d\u4e0d\u4e00\u81f4\u5bfc\u81f4\u4fe1\u53f7\u7578\u53d8<\/li>\n<li><strong>\u8fde\u63a5\u5668\u6c27\u5316\/\u677e\u52a8\uff1a<\/strong>\u5de5\u4e1a\u73af\u5883\u7684\u632f\u52a8\u548c\u8150\u8680\u5bfc\u81f4\u95f4\u6b47\u6027\u63a5\u89e6\u4e0d\u826f<\/li>\n<li><strong>\u6ce2\u7279\u7387\u504f\u5dee\uff1a<\/strong>\u53d1\u9001\u548c\u63a5\u6536\u53cc\u65b9\u7684\u65f6\u949f\u504f\u5dee\u7d2f\u79ef\u53ef\u80fd\u5bfc\u81f4\u4f4d\u91c7\u6837\u9519\u8bef<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Modbus \u534f\u8bae\u5728\u6570\u636e\u94fe\u8def\u5c42\u901a\u8fc7<strong>\u5e27\u6821\u9a8c\u5e8f\u5217\uff08FCS\uff09<\/strong>\u6765\u68c0\u6d4b\u4f20\u8f93\u9519\u8bef\u3002Modbus RTU \u6a21\u5f0f\u4f7f\u7528 <strong>CRC-16<\/strong>\uff0cModbus ASCII \u6a21\u5f0f\u4f7f\u7528 <strong>LRC<\/strong>\u3002\u8fd9\u4e24\u79cd\u6821\u9a8c\u7684\u533a\u522b\u548c\u4f7f\u7528\u573a\u666f\u662f\u672c\u6587\u7684\u6838\u5fc3\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u6df1\u5165\u4ee3\u7801\u4e4b\u524d\uff0c\u63a8\u8350\u5148\u9605\u8bfb <a href=\"https:\/\/modbus.cn\/modbus-rtu-ascii-difference\" rel=\"noopener noreferrer nofollow\">Modbus RTU \u4e0e ASCII \u6a21\u5f0f\u7684\u533a\u522b<\/a>\uff0c\u4e86\u89e3\u4e24\u79cd\u4f20\u8f93\u6a21\u5f0f\u7684\u57fa\u672c\u5dee\u5f02\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e8c\u3001CRC vs LRC\uff1a\u4e24\u79cd\u6821\u9a8c\u65b9\u5f0f\u5bf9\u6bd4<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\">\n<thead>\n<tr><th>\u5bf9\u6bd4\u7ef4\u5ea6<\/th><th>CRC-16\uff08RTU \u6a21\u5f0f\uff09<\/th><th>LRC\uff08ASCII \u6a21\u5f0f\uff09<\/th><\/tr>\n<\/thead>\n<tbody>\n<tr><td><strong>\u7b97\u6cd5\u7c7b\u578b<\/strong><\/td><td>\u5faa\u73af\u5197\u4f59\u6821\u9a8c\uff08\u591a\u9879\u5f0f\u9664\u6cd5\uff09<\/td><td>\u7eb5\u5411\u5197\u4f59\u6821\u9a8c\uff08\u7d2f\u52a0\u53d6\u53cd\uff09<\/td><\/tr>\n<tr><td><strong>\u6821\u9a8c\u503c\u957f\u5ea6<\/strong><\/td><td>16 \u4f4d\uff082 \u5b57\u8282\uff09<\/td><td>8 \u4f4d\uff081 \u5b57\u8282\uff09<\/td><\/tr>\n<tr><td><strong>\u9519\u8bef\u68c0\u6d4b\u80fd\u529b<\/strong><\/td><td>\u6781\u9ad8\uff08\u68c0\u6d4b\u6240\u6709\u5355\u6bd4\u7279\u3001\u53cc\u6bd4\u7279\u3001\u5947\u6570\u4e2a\u6bd4\u7279\u9519\u8bef\uff0c\u4ee5\u53ca\u6240\u6709 \u226416 \u6bd4\u7279\u7684\u7a81\u53d1\u9519\u8bef\uff09<\/td><td>\u4e2d\u7b49\uff08\u68c0\u6d4b\u5355\u5b57\u8282\u9519\u8bef\uff0c\u4f46\u5bf9\u591a\u4f4d\u9519\u8bef\u6709\u76f2\u533a\uff09<\/td><\/tr>\n<tr><td><strong>\u8ba1\u7b97\u590d\u6742\u5ea6<\/strong><\/td><td>\u4e2d\u9ad8\uff08\u9700\u8981\u4f4d\u8fd0\u7b97\u6216\u67e5\u8868\uff09<\/td><td>\u6781\u4f4e\uff08\u53ea\u9700\u7d2f\u52a0\u8fd0\u7b97\uff09<\/td><\/tr>\n<tr><td><strong>\u5e27\u4e2d\u4f4d\u7f6e<\/strong><\/td><td>\u5e27\u672b\u5c3e\uff0c\u4f4e\u5b57\u8282\u5728\u524d\uff08\u5c0f\u7aef\u5e8f\uff09<\/td><td>\u5e27\u672b\u5c3e\uff0c\u4e24\u4e2a ASCII \u5b57\u7b26<\/td><\/tr>\n<tr><td><strong>\u9002\u7528\u4f20\u8f93\u6a21\u5f0f<\/strong><\/td><td>RTU\uff08\u4e8c\u8fdb\u5236\uff09<\/td><td>ASCII\uff08\u6587\u672c\uff09<\/td><\/tr>\n<tr><td><strong>\u8ba1\u7b97\u8303\u56f4<\/strong><\/td><td>\u4ece\u7b2c 1 \u4e2a\u5b57\u8282\uff08\u5730\u5740\uff09\u5230\u6570\u636e\u533a\u672b\u5c3e<\/td><td>\u4ece &#8216;:&#8217; \u4e4b\u540e\u5230 CR\/LF \u4e4b\u524d\uff08\u4e0d\u542b\u5192\u53f7\u548c\u56de\u8f66\u6362\u884c\uff09<\/td><\/tr>\n<tr><td><strong>\u5178\u578b\u6f0f\u68c0\u7387<\/strong><\/td><td>16 \u4f4d CRC \u6f0f\u68c0\u7387\u7ea6 1\/65536<\/td><td>LRC \u6f0f\u68c0\u7387\u8f83\u9ad8\uff0c\u7ea6 1\/256<\/td><\/tr>\n<\/tbody>\n<\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u9009\u578b\u5efa\u8bae\uff1a<\/strong>\u5728\u73b0\u4ee3 Modbus \u5e94\u7528\u4e2d\uff0cRTU \u6a21\u5f0f\u914d\u5408 CRC-16 \u662f\u7edd\u5bf9\u4e3b\u6d41\u3002ASCII \u6a21\u5f0f\u548c LRC \u4e3b\u8981\u7528\u4e8e\u9700\u8981\u4eba\u7c7b\u53ef\u8bfb\u901a\u4fe1\u5185\u5bb9\uff08\u5982\u8c03\u8bd5\u548c\u901a\u8fc7\u7ec8\u7aef\u7a0b\u5e8f\u624b\u52a8\u64cd\u4f5c\uff09\u7684\u7279\u6b8a\u573a\u666f\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e09\u3001CRC-16 \u6570\u5b66\u539f\u7406\uff1a\u4ece\u591a\u9879\u5f0f\u5230\u4f4d\u8fd0\u7b97<\/h2>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img decoding=\"async\" src=\"https:\/\/www.modbus.cn\/wp-content\/uploads\/diagrams\/a08-crc-lookup-table.svg\" style=\"max-width:100%;height:auto\" \/ loading=\"lazy\" title=\"Modbus CRC\/LRC \u6821\u9a8c\u539f\u7406\u4e0e\u7f16\u7a0b\u5b9e\u73b0\uff1a\u4ece\u6570\u5b66\u63a8\u5bfc\u5230\u591a\u8bed\u8a00\u4ee3\u7801\u5b9e\u6218\u63d2\u56fe1\" alt=\"Modbus CRC\/LRC \u6821\u9a8c\u539f\u7406\u4e0e\u7f16\u7a0b\u5b9e\u73b0\uff1a\u4ece\u6570\u5b66\u63a8\u5bfc\u5230\u591a\u8bed\u8a00\u4ee3\u7801\u5b9e\u6218\u63d2\u56fe1\" \/><figcaption class=\"wp-element-caption\">\u25b2 \u56fe2\uff1a\u67e5\u8868\u6cd5\u539f\u7406 \u2014 256\u9879\u9884\u8ba1\u7b97\u8868\u5c06\u5355\u5b57\u8282\u5904\u7406\u4ece8\u6b21\u5faa\u73af\u964d\u4e3a1\u6b21\u67e5\u8868+1\u6b21\u5f02\u6216\u3002<\/figcaption><\/figure>\n<\/div>\n\n\n\n\n\n\n\n\n<h3 class=\"wp-block-heading\">3.1 CRC \u7684\u672c\u8d28\uff1a\u6a21\u4e8c\u9664\u6cd5<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">CRC \u7684\u672c\u8d28\u662f<strong>\u6a21\u4e8c\u591a\u9879\u5f0f\u9664\u6cd5<\/strong>\u3002\u628a\u5f85\u6821\u9a8c\u7684\u6570\u636e\u89c6\u4e3a\u4e00\u4e2a\u4e8c\u8fdb\u5236\u591a\u9879\u5f0f M(x)\uff0c\u9664\u4ee5\u4e00\u4e2a\u9884\u5b9a\u4e49\u7684\u751f\u6210\u591a\u9879\u5f0f G(x)\uff0c\u5f97\u5230\u7684\u4f59\u6570\u5c31\u662f CRC \u503c\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Modbus RTU \u4f7f\u7528\u7684 CRC-16 \u53c2\u6570\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u591a\u9879\u5f0f\uff1a<\/strong>x^16 + x^15 + x^2 + 1<\/li>\n<li><strong>\u591a\u9879\u5f0f\u503c\uff1a<\/strong>0x8005\uff08\u6b63\u5411\uff09\u6216 0xA001\uff08\u53cd\u5411\/Modbus \u6807\u51c6\uff09<\/li>\n<li><strong>\u521d\u59cb\u503c\uff1a<\/strong>0xFFFF<\/li>\n<li><strong>\u7ed3\u679c\u5f02\u6216\u503c\uff1a<\/strong>0x0000\uff08\u4e0d\u5f02\u6216\uff09<\/li>\n<li><strong>\u8f93\u5165\u6570\u636e\u53cd\u8f6c\uff1a<\/strong>\u5426<\/li>\n<li><strong>\u8f93\u51fa\u6570\u636e\u53cd\u8f6c\uff1a<\/strong>\u5426\uff08\u4f46 Modbus \u5b58\u50a8\u4e3a\u5c0f\u7aef\u5e8f\uff09<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6a21\u4e8c\u9664\u6cd5\u793a\u4f8b\uff1a<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u6781\u7b80\u5316\u7684\u6570\u636e\uff1a\u5f85\u6821\u9a8c\u5b57\u8282\u4e3a 0x02\uff08\u4e8c\u8fdb\u5236 0000 0010\uff09\uff0c\u4f7f\u7528\u7b80\u5316\u7684 4 \u4f4d CRC\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u6570\u636e: 0000 0010\n\u591a\u9879\u5f0f\uff08\u53cd\u5411 0xA001 = 1010 0000 0000 0001\uff09:\n\n\u9010\u6b65\u79fb\u4f4d\u548c\u5f02\u6216\u8fc7\u7a0b\uff08\u6a21\u62df\u786c\u4ef6\u79fb\u4f4d\u5bc4\u5b58\u5668\uff09:\n\n1. \u521d\u59cb\u5316 CRC \u5bc4\u5b58\u5668: 1111 1111 1111 1111 (0xFFFF)\n2. \u53d6\u7b2c\u4e00\u4e2a\u6570\u636e\u5b57\u8282 0x02: 0000 0010\n3. CRC ^= \u6570\u636e\u5b57\u8282: 1111 1111 1111 1101\n4. \u5bf9\u8be5\u5b57\u8282\u7684\u6bcf\u4e00\u4f4d\u6267\u884c\uff1a\n   - \u5982\u679c LSB = 1: CRC &gt;&gt;= 1, CRC ^= 0xA001\n   - \u5982\u679c LSB = 0: CRC &gt;&gt;= 1\n   \n\u6700\u7ec8 CRC \u5bc4\u5b58\u5668\u4e2d\u7684\u503c\u5373\u4e3a\u6821\u9a8c\u7ed3\u679c<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3.2 \u4e3a\u4ec0\u4e48 Modbus \u4f7f\u7528 0xA001 \u800c\u4e0d\u662f 0x8005\uff1f<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">0x8005 \u548c 0xA001 \u662f\u540c\u4e00\u4e2a\u591a\u9879\u5f0f\u7684\u6b63\u5411\u548c\u53cd\u5411\u8868\u793a\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>0x8005 (\u6b63\u5411)\uff1a<\/strong>\u4e8c\u8fdb\u5236 1000 0000 0000 0101\uff0c\u5bf9\u5e94\u591a\u9879\u5f0f x^16 + x^15 + x^2 + 1\u3002\u8fd9\u662f\u751f\u6210\u591a\u9879\u5f0f\u7684&#8221;\u81ea\u7136&#8221;\u8868\u793a\uff0c\u7528\u4e8e\u5de6\u79fb\u578b\uff08MSB first\uff09CRC \u8ba1\u7b97\u3002<\/li>\n<li><strong>0xA001 (\u53cd\u5411)\uff1a<\/strong>\u4e8c\u8fdb\u5236 1010 0000 0000 0001\uff0c\u662f 0x8005 \u7684\u4f4d\u53cd\u8f6c\u3002\u7528\u4e8e\u53f3\u79fb\u578b\uff08LSB first\uff09CRC \u8ba1\u7b97\u2014\u2014\u8fd9\u4e5f\u662f Modbus \u534f\u8bae\u89c4\u5b9a\u7684\u6807\u51c6\u65b9\u6cd5\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Modbus \u9009\u62e9\u53f3\u79fb\u578b\u8ba1\u7b97\uff0c\u56e0\u4e3a RS-485 \u6570\u636e\u94fe\u8def\u5c42\u5728\u7269\u7406\u4e0a\u5148\u53d1\u9001 LSB\uff08\u6700\u4f4e\u6709\u6548\u4f4d\uff09\u3002\u4f7f\u7528 0xA001 \u53ef\u4ee5\u4f7f\u786c\u4ef6 CRC \u8ba1\u7b97\u5668\u4e0e\u4e32\u884c\u79fb\u4f4d\u65b9\u5411\u4e00\u81f4\uff0c\u63d0\u9ad8\u6548\u7387\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3.3 \u4ece\u4f4d\u79fb\u6cd5\u5230\u67e5\u8868\u6cd5\uff1a\u6027\u80fd\u98de\u8dc3<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u4f4d\u79fb\u6cd5\u7684\u74f6\u9888\uff1a<\/strong>\u5bf9\u4e8e\u6bcf\u4e2a\u5b57\u8282\uff0c\u9700\u8981 8 \u6b21\u5faa\u73af\uff0c\u6bcf\u6b21\u5faa\u73af\u5305\u542b\u6761\u4ef6\u5224\u65ad\u3001\u79fb\u4f4d\u3001\u5f02\u6216\u64cd\u4f5c\u3002\u5904\u7406 100 \u5b57\u8282\u7684 Modbus \u5e27\u5c31\u9700\u8981 800 \u6b21\u5faa\u73af\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u67e5\u8868\u6cd5\u7684\u6838\u5fc3\u601d\u60f3\uff1a<\/strong>\u5c06\u6bcf\u4e2a\u53ef\u80fd\u7684\u5b57\u8282\u503c\uff080x00-0xFF \u5171 256 \u4e2a\uff09\u7684 CRC \u4e2d\u95f4\u7ed3\u679c\u9884\u5148\u8ba1\u7b97\u597d\uff0c\u5b58\u50a8\u5728\u67e5\u627e\u8868\u4e2d\u3002\u5904\u7406\u4e00\u4e2a\u5b57\u8282\u65f6\uff0c\u53ea\u9700\u4e00\u6b21\u67e5\u8868\u64cd\u4f5c\u52a0\u4e00\u6b21\u5f02\u6216\u8fd0\u7b97\uff0c\u5c06\u8ba1\u7b97\u91cf\u4ece O(8N) \u964d\u5230 O(N)\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u67e5\u8868\u7b97\u6cd5\u7684\u63a8\u5bfc\u8fc7\u7a0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u8bbe\u5f53\u524d CRC \u5bc4\u5b58\u5668\u503c\u4e3a crc\uff0816 \u4f4d\uff09\uff0c\u4e0b\u4e00\u4e2a\u6570\u636e\u5b57\u8282\u4e3a data\u3002\n\n\u4f4d\u79fb\u6cd5\u9700\u8981\u5bf9 data \u7684\u6bcf\u4e00\u4f4d\u8fed\u4ee3\u8ba1\u7b97\u3002\u7ecf\u8fc7\u63a8\u5bfc\uff0c\u5355\u5b57\u8282\u5904\u7406\u7b49\u4ef7\u4e8e\uff1a\n\n1. index = (crc ^ data) &amp; 0x00FF   \/\/ \u53d6\u5f53\u524d CRC \u4f4e 8 \u4f4d\u4e0e\u6570\u636e\u5b57\u8282\u5f02\u6216\n2. crc = (crc &gt;&gt; 8) ^ table[index]  \/\/ CRC \u53f3\u79fb 8 \u4f4d\uff0c\u518d\u67e5\u8868\u5f02\u6216\n\n\u5176\u4e2d table[index] \u662f\u901a\u8fc7\u4f4d\u79fb\u6cd5\u9884\u8ba1\u7b97 256 \u6b21\u5f97\u5230\u7684\u67e5\u627e\u8868\u503c\u3002<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e2a\u63a8\u5bfc\u5c06&#8221;8 \u6b21\u8fed\u4ee3\u5faa\u73af&#8221;\u538b\u7f29\u4e3a&#8221;1 \u6b21\u67e5\u8868 + 1 \u6b21\u5f02\u6216&#8221;\uff0c\u6027\u80fd\u63d0\u5347\u7ea6 8 \u500d\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3.4 \u67e5\u627e\u8868\u751f\u6210\u4ee3\u7801\u4e0e\u5b8c\u6574\u63a8\u5bfc<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u7406\u89e3\u67e5\u627e\u8868\u7684\u751f\u6210\u8fc7\u7a0b\u662f\u638c\u63e1 CRC \u67e5\u8868\u6cd5\u7684\u5173\u952e\u3002\u4ee5\u4e0b\u4ee3\u7801\u5c55\u793a\u4e86\u5982\u4f55\u7528\u4f4d\u79fb\u6cd5\u9884\u8ba1\u7b97\u5b8c\u6574\u7684 256 \u9879 CRC \u67e5\u627e\u8868\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/**\n * \u751f\u6210 Modbus CRC-16 \u67e5\u627e\u8868\n * \u8fd0\u884c\u4e00\u6b21\uff0c\u5c06\u8f93\u51fa\u4f5c\u4e3a\u9759\u6001\u6570\u7ec4\u5d4c\u5165\u4e3b\u7a0b\u5e8f\n *\/\nvoid generate_crc16_table(uint16_t *table)\n{\n    uint16_t remainder;\n    int byte, bit;\n\n    for (byte = 0; byte &lt; 256; byte++) {\n        remainder = (uint16_t)byte;      \/* \u521d\u59cb\u4f59\u6570 = \u5f53\u524d\u5b57\u8282\u503c *\/\n\n        for (bit = 0; bit &lt; 8; bit++) {\n            if (remainder &amp; 0x0001) {          \/* LSB \u4e3a 1 *\/\n                remainder = (remainder &gt;&gt; 1) ^ 0xA001;  \/* \u53f3\u79fb\u5e76\u5f02\u6216 *\/\n            } else {\n                remainder = (remainder &gt;&gt; 1);    \/* \u53ea\u53f3\u79fb *\/\n            }\n        }\n\n        table[byte] = remainder;\n    }\n}\n\n\/* \u63a8\u5bfc\u8bf4\u660e\uff1a\n * \u4e3a\u4ec0\u4e48\u8fd9\u4e2a\u8868\u53ef\u4ee5\u76f4\u63a5\u7528\u4e8e\u67e5\u8868\u6cd5\uff1f\n * \n * \u5bf9\u4e8e\u4efb\u610f\u6570\u636e\u5b57\u8282 data\uff0c\u4f4d\u79fb\u6cd5\u9700\u8981\u5faa\u73af 8 \u6b21\u3002\n * \u8bbe CRC \u5f53\u524d\u503c\u4e3a crc\uff0816 \u4f4d\uff09\uff0c\u7ecf\u8fc7 8 \u6b21\u8fed\u4ee3\u540e\uff1a\n *   crc' = f(f(f(...f(crc ^ data)...)))\n * \n * \u7531\u4e8e\u5f02\u6216\u8fd0\u7b97\u7684\u6027\u8d28\uff1acrc ^ data = (crc &gt;&gt; 8) &lt;&lt; 8 | (crc &amp; 0xFF) ^ data\n * \u4f4e 8 \u4f4d\u7684\u5904\u7406\u7ed3\u679c\u4ec5\u4f9d\u8d56\u4e8e (crc &amp; 0xFF) ^ data \u7684\u503c\uff0c\n * \u800c\u8fd9\u4e2a\u503c\u6070\u597d\u662f 0-255\uff0c\u56e0\u6b64\u53ef\u4ee5\u9884\u5148\u8ba1\u7b97\u6240\u6709\u53ef\u80fd\u7684\u4e2d\u95f4\u7ed3\u679c\u3002\n * \n * \u9ad8 8 \u4f4d\u5219\u76f4\u63a5\u53f3\u79fb\uff0c\u4e0e\u65b0\u8ba1\u7b97\u7684\u4f4e 8 \u4f4d\u7ed3\u679c\uff08\u67e5\u8868\u83b7\u5f97\uff09\u8fdb\u884c\u5f02\u6216\u3002\n * \u8fd9\u5c31\u662f\u67e5\u8868\u6cd5\u80fd\u5de5\u4f5c\u7684\u6570\u5b66\u57fa\u7840\u3002\n *\/<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e2a\u8868\u751f\u6210\u51fd\u6570\u63ed\u793a\u4e86 CRC \u8ba1\u7b97\u7684\u672c\u8d28\uff1a<strong>\u67e5\u627e\u8868\u4e2d\u7684\u6bcf\u4e00\u4e2a\u503c\uff0c\u90fd\u662f\u5bf9\u5e94\u7d22\u5f15\u5b57\u8282\u503c\u7ecf\u8fc7 8 \u6b21\u53f3\u79fb\u578b CRC \u8fed\u4ee3\u540e\u7684\u7ed3\u679c<\/strong>\u3002\u4e3b\u8ba1\u7b97\u51fd\u6570\u4e2d\u7684 <code>index = (crc ^ data) &amp; 0xFF<\/code> \u64cd\u4f5c\uff0c\u672c\u8d28\u4e0a\u662f\u5728\u8ba1\u7b97&#8221;\u5f53\u524d CRC \u4f4e 8 \u4f4d\u4e0e\u6570\u636e\u5b57\u8282\u7684\u6a21\u4e8c\u548c&#8221;\uff0c\u7136\u540e\u7528\u8fd9\u4e2a\u7ed3\u679c\u53bb\u67e5\u8868\u83b7\u5f97\u9884\u8ba1\u7b97\u7684 CRC \u8d21\u732e\u503c\u3002\u66f4\u591a\u5173\u4e8e Modbus CRC \u7684\u6df1\u5165\u6280\u672f\u8ba8\u8bba\uff0c\u53ef\u4ee5\u8bbf\u95ee <a href=\"https:\/\/modbus.cn\" rel=\"noopener noreferrer nofollow\">modbus.cn<\/a> \u67e5\u9605\u5b8c\u6574\u6587\u6863\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3.5 \u9519\u8bef\u68c0\u6d4b\u80fd\u529b\u7684\u6570\u5b66\u5206\u6790<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">CRC-16 \u7684\u5f3a\u5927\u68c0\u6d4b\u80fd\u529b\u6765\u6e90\u4e8e\u5176\u6570\u5b66\u7279\u6027\u3002\u4ee5\u4e0b\u662f\u5bf9 16 \u4f4d CRC \u5728\u4e0d\u540c\u9519\u8bef\u6a21\u5f0f\u4e0b\u7684\u68c0\u6d4b\u80fd\u529b\u5206\u6790\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\">\n<thead>\n<tr><th>\u9519\u8bef\u7c7b\u578b<\/th><th>\u68c0\u6d4b\u6982\u7387<\/th><th>\u6570\u5b66\u539f\u7406<\/th><\/tr>\n<\/thead>\n<tbody>\n<tr><td><strong>\u5355\u4e2a\u6bd4\u7279\u9519\u8bef<\/strong><\/td><td>100%<\/td><td>\u751f\u6210\u591a\u9879\u5f0f\u542b\u6709 x+1 \u56e0\u5f0f\u65f6\u4fdd\u8bc1\u68c0\u6d4b\u6240\u6709\u5947\u6570\u4f4d\u9519\u8bef<\/td><\/tr>\n<tr><td><strong>\u4e24\u4e2a\u6bd4\u7279\u9519\u8bef<\/strong><\/td><td>100%<\/td><td>\u5f53\u4e24\u4e2a\u9519\u8bef\u6bd4\u7279\u95f4\u8ddd &lt; 32767 \u4f4d\u65f6\uff0c16 \u4f4d CRC \u603b\u80fd\u68c0\u6d4b<\/td><\/tr>\n<tr><td><strong>\u5947\u6570\u4e2a\u6bd4\u7279\u9519\u8bef<\/strong><\/td><td>100%<\/td><td>0xA001 \u591a\u9879\u5f0f\u5305\u542b (x+1) \u56e0\u5b50\uff0c\u53ef\u68c0\u6d4b\u6240\u6709\u5947\u6570\u4f4d\u9519\u8bef<\/td><\/tr>\n<tr><td><strong>\u7a81\u53d1\u9519\u8bef \u2264 16 \u4f4d<\/strong><\/td><td>100%<\/td><td>\u7a81\u53d1\u9519\u8bef\u591a\u9879\u5f0f\u6b21\u6570 \u2264 15\uff0c\u9664\u4ee5 16 \u6b21\u591a\u9879\u5f0f\u5fc5\u6709\u4f59\u6570<\/td><\/tr>\n<tr><td><strong>\u7a81\u53d1\u9519\u8bef 17 \u4f4d<\/strong><\/td><td>99.9969%<\/td><td>\u53ea\u6709 2^-(16-1) \u7684\u6982\u7387\u88ab\u9519\u8bef\u68c0\u6d4b\u4e3a\u6b63\u786e<\/td><\/tr>\n<tr><td><strong>\u968f\u673a\u591a\u4f4d\u9519\u8bef<\/strong><\/td><td>99.9985%<\/td><td>16 \u4f4d CRC \u5bf9\u6240\u6709\u975e\u500d\u6570\u9519\u8bef\u6709 1-2^-16 \u7684\u68c0\u6d4b\u7387<\/td><\/tr>\n<\/tbody>\n<\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e9b\u6570\u5b66\u7279\u6027\u4f7f\u5f97 CRC-16 \u6210\u4e3a\u5de5\u4e1a\u901a\u4fe1\u4e2d\u6027\u4ef7\u6bd4\u6781\u9ad8\u7684\u9519\u8bef\u68c0\u6d4b\u624b\u6bb5\u3002\u5728 Modbus RTU \u7684\u5178\u578b\u5e94\u7528\u573a\u666f\uff08RS-485 \u603b\u7ebf\uff0c\u6ce2\u7279\u7387 \u2264 115.2Kbps\uff0c\u5e27\u957f\u5ea6\u901a\u5e38 \u2264 256 \u5b57\u8282\uff09\u4e2d\uff0cCRC-16 \u80fd\u591f\u68c0\u6d4b\u51e0\u4e4e\u6240\u6709\u5b9e\u9645\u53ef\u80fd\u53d1\u751f\u7684\u4f20\u8f93\u9519\u8bef\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u56db\u3001CRC-16 \u5b8c\u6574\u4ee3\u7801\u5b9e\u73b0<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">4.1 C \u8bed\u8a00\u67e5\u8868\u6cd5\uff08\u9ad8\u6027\u80fd\u7248\u672c\uff09<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ee5\u4e0b\u662f Modbus CRC-16 \u7684 C \u8bed\u8a00\u67e5\u8868\u6cd5\u5b9e\u73b0\uff0c\u8fd9\u662f\u5de5\u4e1a\u5d4c\u5165\u5f0f\u7cfb\u7edf\u4e2d\u4f7f\u7528\u6700\u591a\u7684\u7248\u672c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdint.h&gt;\n#include &lt;stddef.h&gt;\n\n\/* Modbus CRC-16 \u67e5\u627e\u8868\uff08\u591a\u9879\u5f0f 0xA001\uff09 *\/\nstatic const uint16_t crc16_table[256] = {\n    0x0000, 0xC0C1, 0xC181, 0x0140, 0xC301, 0x03C0, 0x0280, 0xC241,\n    0xC601, 0x06C0, 0x0780, 0xC741, 0x0500, 0xC5C1, 0xC481, 0x0440,\n    0xCC01, 0x0CC0, 0x0D80, 0xCD41, 0x0F00, 0xCFC1, 0xCE81, 0x0E40,\n    0x0A00, 0xCAC1, 0xCB81, 0x0B40, 0xC901, 0x09C0, 0x0880, 0xC841,\n    0xD801, 0x18C0, 0x1980, 0xD941, 0x1B00, 0xDBC1, 0xDA81, 0x1A40,\n    0x1E00, 0xDEC1, 0xDF81, 0x1F40, 0xDD01, 0x1DC0, 0x1C80, 0xDC41,\n    0x1400, 0xD4C1, 0xD581, 0x1540, 0xD701, 0x17C0, 0x1680, 0xD641,\n    0xD201, 0x12C0, 0x1380, 0xD341, 0x1100, 0xD1C1, 0xD081, 0x1040,\n    0xF001, 0x30C0, 0x3180, 0xF141, 0x3300, 0xF3C1, 0xF281, 0x3240,\n    0x3600, 0xF6C1, 0xF781, 0x3740, 0xF501, 0x35C0, 0x3480, 0xF441,\n    0x3C00, 0xFCC1, 0xFD81, 0x3D40, 0xFF01, 0x3FC0, 0x3E80, 0xFE41,\n    0xFA01, 0x3AC0, 0x3B80, 0xFB41, 0x3900, 0xF9C1, 0xF881, 0x3840,\n    0x2800, 0xE8C1, 0xE981, 0x2940, 0xEB01, 0x2BC0, 0x2A80, 0xEA41,\n    0xEE01, 0x2EC0, 0x2F80, 0xEF41, 0x2D00, 0xEDC1, 0xEC81, 0x2C40,\n    0xE401, 0x24C0, 0x2580, 0xE541, 0x2700, 0xE7C1, 0xE681, 0x2640,\n    0x2200, 0xE2C1, 0xE381, 0x2340, 0xE101, 0x21C0, 0x2080, 0xE041,\n    0xA001, 0x60C0, 0x6180, 0xA141, 0x6300, 0xA3C1, 0xA281, 0x6240,\n    0x6600, 0xA6C1, 0xA781, 0x6740, 0xA501, 0x65C0, 0x6480, 0xA441,\n    0x6C00, 0xACC1, 0xAD81, 0x6D40, 0xAF01, 0x6FC0, 0x6E80, 0xAE41,\n    0xAA01, 0x6AC0, 0x6B80, 0xAB41, 0x6900, 0xA9C1, 0xA881, 0x6840,\n    0x7800, 0xB8C1, 0xB981, 0x7940, 0xBB01, 0x7BC0, 0x7A80, 0xBA41,\n    0xBE01, 0x7EC0, 0x7F80, 0xBF41, 0x7D00, 0xBDC1, 0xBC81, 0x7C40,\n    0xB401, 0x74C0, 0x7580, 0xB541, 0x7700, 0xB7C1, 0xB681, 0x7640,\n    0x7200, 0xB2C1, 0xB381, 0x7340, 0xB101, 0x71C0, 0x7080, 0xB041,\n    0x5000, 0x90C1, 0x9181, 0x5140, 0x9301, 0x53C0, 0x5280, 0x9241,\n    0x9601, 0x56C0, 0x5780, 0x9741, 0x5500, 0x95C1, 0x9481, 0x5440,\n    0x9C01, 0x5CC0, 0x5D80, 0x9D41, 0x5F00, 0x9FC1, 0x9E81, 0x5E40,\n    0x5A00, 0x9AC1, 0x9B81, 0x5B40, 0x9901, 0x59C0, 0x5880, 0x9841,\n    0x8801, 0x48C0, 0x4980, 0x8941, 0x4B00, 0x8BC1, 0x8A81, 0x4A40,\n    0x4E00, 0x8EC1, 0x8F81, 0x4F40, 0x8D01, 0x4DC0, 0x4C80, 0x8C41,\n    0x4400, 0x84C1, 0x8581, 0x4540, 0x8701, 0x47C0, 0x4680, 0x8641,\n    0x8201, 0x42C0, 0x4380, 0x8341, 0x4100, 0x81C1, 0x8081, 0x4040\n};\n\n\/**\n * Modbus CRC-16 \u67e5\u8868\u6cd5\u8ba1\u7b97\n * @param buf   \u5f85\u6821\u9a8c\u7684\u6570\u636e\u7f13\u51b2\u533a\n * @param len   \u6570\u636e\u957f\u5ea6\uff08\u5b57\u8282\u6570\uff09\n * @return      16 \u4f4d CRC \u503c\n *\/\nuint16_t modbus_crc16(uint8_t *buf, uint16_t len)\n{\n    uint16_t crc = 0xFFFF;          \/* \u521d\u59cb\u503c *\/\n\n    while (len--) {\n        uint8_t pos = (uint8_t)(crc ^ (*buf++)) &amp; 0xFF;\n        crc = (crc &gt;&gt; 8) ^ crc16_table[pos];\n    }\n\n    return crc;\n}\n\n\/* \u4f7f\u7528\u793a\u4f8b\uff1a\n * uint8_t frame[] = {0x01, 0x03, 0x00, 0x00, 0x00, 0x01};\n * uint16_t crc = modbus_crc16(frame, 6);\n * \/\/ crc = 0x0ACA\n * \/\/ \u5728 Modbus RTU \u5e27\u4e2d\uff0c\u4f4e\u5b57\u8282\u5728\u524d\uff1a\n * \/\/ frame[6] = crc &amp; 0xFF;  (0xCA)\n * \/\/ frame[7] = crc &gt;&gt; 8;    (0x0A)\n *\/<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">4.2 C \u8bed\u8a00\u4f4d\u79fb\u6cd5\uff08\u6559\u5b66\u7248\u672c\uff09<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ee5\u4e0b\u662f\u9010\u4f4d\u8fd0\u7b97\u7248\u672c\uff0c\u4ee3\u7801\u91cf\u5c11\u4f46\u6548\u7387\u4f4e\uff0c\u9002\u5408\u5b66\u4e60\u548c\u5d4c\u5165\u8d44\u6e90\u6781\u5ea6\u53d7\u9650\u7684\u573a\u666f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/**\n * Modbus CRC-16 \u9010\u4f4d\u8ba1\u7b97\u6cd5\n * \u7528\u4e8e\u6559\u5b66\u548c\u7406\u89e3 CRC \u539f\u7406\uff0c\u751f\u4ea7\u73af\u5883\u5efa\u8bae\u4f7f\u7528\u67e5\u8868\u6cd5\n *\/\nuint16_t modbus_crc16_bitwise(uint8_t *buf, uint16_t len)\n{\n    uint16_t crc = 0xFFFF;   \/* \u521d\u59cb\u503c *\/\n    uint16_t i, j;\n\n    for (i = 0; i &lt; len; i++) {\n        crc ^= (uint16_t)buf[i];   \/* \u5c06\u6570\u636e\u5b57\u8282\u4e0e CRC \u4f4e\u5b57\u8282\u5f02\u6216 *\/\n\n        for (j = 0; j &gt; 1) ^ 0xA001;  \/* \u53f3\u79fb\u4e00\u4f4d\u5e76\u5f02\u6216\u591a\u9879\u5f0f *\/\n            } else {\n                crc = crc &gt;&gt; 1;              \/* \u53ea\u53f3\u79fb\u4e00\u4f4d *\/\n            }\n        }\n    }\n\n    return crc;\n}\n\n\/* \u9a8c\u8bc1\uff1a\n * uint8_t test[] = {0x01, 0x03, 0x00, 0x00, 0x00, 0x01};\n * uint16_t crc = modbus_crc16_bitwise(test, 6);\n * \/\/ \u7ed3\u679c\u5e94\u4e3a 0x0ACA\n *\/<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">4.3 Python \u5b9e\u73b0<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Python \u7248\u672c\u9002\u5408\u4e0a\u4f4d\u673a\u7a0b\u5e8f\u3001\u6570\u636e\u5206\u6790\u811a\u672c\u548c\u81ea\u52a8\u5316\u6d4b\u8bd5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/usr\/bin\/env python3\n\"\"\"Modbus CRC-16 \u6821\u9a8c\u5de5\u5177\"\"\"\n\nfrom typing import List, Union\n\n\nclass ModbusCRC:\n    \"\"\"Modbus CRC-16 \u8ba1\u7b97\u5668\"\"\"\n\n    # CRC-16 \u67e5\u627e\u8868\n    TABLE = [\n        0x0000, 0xC0C1, 0xC181, 0x0140, 0xC301, 0x03C0, 0x0280, 0xC241,\n        0xC601, 0x06C0, 0x0780, 0xC741, 0x0500, 0xC5C1, 0xC481, 0x0440,\n        0xCC01, 0x0CC0, 0x0D80, 0xCD41, 0x0F00, 0xCFC1, 0xCE81, 0x0E40,\n        0x0A00, 0xCAC1, 0xCB81, 0x0B40, 0xC901, 0x09C0, 0x0880, 0xC841,\n        0xD801, 0x18C0, 0x1980, 0xD941, 0x1B00, 0xDBC1, 0xDA81, 0x1A40,\n        0x1E00, 0xDEC1, 0xDF81, 0x1F40, 0xDD01, 0x1DC0, 0x1C80, 0xDC41,\n        0x1400, 0xD4C1, 0xD581, 0x1540, 0xD701, 0x17C0, 0x1680, 0xD641,\n        0xD201, 0x12C0, 0x1380, 0xD341, 0x1100, 0xD1C1, 0xD081, 0x1040,\n        0xF001, 0x30C0, 0x3180, 0xF141, 0x3300, 0xF3C1, 0xF281, 0x3240,\n        0x3600, 0xF6C1, 0xF781, 0x3740, 0xF501, 0x35C0, 0x3480, 0xF441,\n        0x3C00, 0xFCC1, 0xFD81, 0x3D40, 0xFF01, 0x3FC0, 0x3E80, 0xFE41,\n        0xFA01, 0x3AC0, 0x3B80, 0xFB41, 0x3900, 0xF9C1, 0xF881, 0x3840,\n        0x2800, 0xE8C1, 0xE981, 0x2940, 0xEB01, 0x2BC0, 0x2A80, 0xEA41,\n        0xEE01, 0x2EC0, 0x2F80, 0xEF41, 0x2D00, 0xEDC1, 0xEC81, 0x2C40,\n        0xE401, 0x24C0, 0x2580, 0xE541, 0x2700, 0xE7C1, 0xE681, 0x2640,\n        0x2200, 0xE2C1, 0xE381, 0x2340, 0xE101, 0x21C0, 0x2080, 0xE041,\n        0xA001, 0x60C0, 0x6180, 0xA141, 0x6300, 0xA3C1, 0xA281, 0x6240,\n        0x6600, 0xA6C1, 0xA781, 0x6740, 0xA501, 0x65C0, 0x6480, 0xA441,\n        0x6C00, 0xACC1, 0xAD81, 0x6D40, 0xAF01, 0x6FC0, 0x6E80, 0xAE41,\n        0xAA01, 0x6AC0, 0x6B80, 0xAB41, 0x6900, 0xA9C1, 0xA881, 0x6840,\n        0x7800, 0xB8C1, 0xB981, 0x7940, 0xBB01, 0x7BC0, 0x7A80, 0xBA41,\n        0xBE01, 0x7EC0, 0x7F80, 0xBF41, 0x7D00, 0xBDC1, 0xBC81, 0x7C40,\n        0xB401, 0x74C0, 0x7580, 0xB541, 0x7700, 0xB7C1, 0xB681, 0x7640,\n        0x7200, 0xB2C1, 0xB381, 0x7340, 0xB101, 0x71C0, 0x7080, 0xB041,\n        0x5000, 0x90C1, 0x9181, 0x5140, 0x9301, 0x53C0, 0x5280, 0x9241,\n        0x9601, 0x56C0, 0x5780, 0x9741, 0x5500, 0x95C1, 0x9481, 0x5440,\n        0x9C01, 0x5CC0, 0x5D80, 0x9D41, 0x5F00, 0x9FC1, 0x9E81, 0x5E40,\n        0x5A00, 0x9AC1, 0x9B81, 0x5B40, 0x9901, 0x59C0, 0x5880, 0x9841,\n        0x8801, 0x48C0, 0x4980, 0x8941, 0x4B00, 0x8BC1, 0x8A81, 0x4A40,\n        0x4E00, 0x8EC1, 0x8F81, 0x4F40, 0x8D01, 0x4DC0, 0x4C80, 0x8C41,\n        0x4400, 0x84C1, 0x8581, 0x4540, 0x8701, 0x47C0, 0x4680, 0x8641,\n        0x8201, 0x42C0, 0x4380, 0x8341, 0x4100, 0x81C1, 0x8081, 0x4040\n    ]\n\n    @staticmethod\n    def calculate(data: Union[bytes, List[int]]) -&gt; int:\n        \"\"\"\u8ba1\u7b97 Modbus CRC-16\"\"\"\n        crc = 0xFFFF\n        for byte in data:\n            pos = (crc ^ byte) &amp; 0xFF\n            crc = (crc &gt;&gt; 8) ^ ModbusCRC.TABLE[pos]\n        return crc\n\n    @staticmethod\n    def verify(frame: bytes) -&gt; bool:\n        \"\"\"\n        \u9a8c\u8bc1\u5e26\u6709 CRC \u7684 Modbus RTU \u5e27\n        \u5c06\u6574\u4e2a\u5e27\uff08\u542b CRC\uff09\u518d\u7b97\u4e00\u6b21 CRC\uff0c\u7ed3\u679c\u5e94\u4e3a 0\n        \"\"\"\n        return ModbusCRC.calculate(frame) == 0\n\n    @staticmethod\n    def append_crc(data: bytes) -&gt; bytes:\n        \"\"\"\u5728\u6570\u636e\u672b\u5c3e\u8ffd\u52a0 CRC\uff08\u5c0f\u7aef\u5e8f\uff09\"\"\"\n        crc = ModbusCRC.calculate(data)\n        return data + bytes([crc &amp; 0xFF, crc &gt;&gt; 8])\n\n\n# ===== \u4f7f\u7528\u793a\u4f8b =====\nif __name__ == '__main__':\n    # \u793a\u4f8b 1: \u8bfb\u53d6\u4fdd\u6301\u5bc4\u5b58\u5668\u547d\u4ee4\n    # \u5730\u5740=1, \u529f\u80fd\u7801=03, \u8d77\u59cb\u5730\u5740=0x0000, \u5bc4\u5b58\u5668\u6570\u91cf=1\n    request = bytes([0x01, 0x03, 0x00, 0x00, 0x00, 0x01])\n    crc_value = ModbusCRC.calculate(request)\n    print(f\"CRC-16: 0x{crc_value:04X}\")  # \u671f\u671b: 0x0ACA\n\n    # \u793a\u4f8b 2: \u6784\u9020\u5b8c\u6574\u54cd\u5e94\u5e27\n    response_data = bytes([0x01, 0x03, 0x02, 0x00, 0x64])\n    full_response = ModbusCRC.append_crc(response_data)\n    print(f\"\u5b8c\u6574\u5e27: {full_response.hex(' ').upper()}\")\n    # \u671f\u671b: 01 03 02 00 64 B9 AF\n\n    # \u793a\u4f8b 3: \u9a8c\u8bc1\u63a5\u6536\u5e27\n    received = bytes([0x01, 0x03, 0x02, 0x00, 0x64, 0xB9, 0xAF])\n    is_valid = ModbusCRC.verify(received)\n    print(f\"\u5e27\u6821\u9a8c: {'\u901a\u8fc7' if is_valid else '\u5931\u8d25'}\")\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">4.4 JavaScript \u5b9e\u73b0\uff08Web \u8c03\u8bd5\u5de5\u5177\uff09<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ee5\u4e0b JavaScript \u7248\u672c\u53ef\u7528\u4e8e Web \u524d\u7aef\u8c03\u8bd5\u5de5\u5177\u6216 Node.js \u73af\u5883\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/**\n * Modbus CRC-16 \u6821\u9a8c\u5de5\u5177 (JavaScript)\n * \u53ef\u76f4\u63a5\u5728\u6d4f\u89c8\u5668\u63a7\u5236\u53f0\u6216 Node.js \u4e2d\u8fd0\u884c\n *\/\n\n\/\/ CRC-16 \u67e5\u627e\u8868\nconst CRC16_TABLE = new Uint16Array([\n    0x0000, 0xC0C1, 0xC181, 0x0140, 0xC301, 0x03C0, 0x0280, 0xC241,\n    0xC601, 0x06C0, 0x0780, 0xC741, 0x0500, 0xC5C1, 0xC481, 0x0440,\n    0xCC01, 0x0CC0, 0x0D80, 0xCD41, 0x0F00, 0xCFC1, 0xCE81, 0x0E40,\n    0x0A00, 0xCAC1, 0xCB81, 0x0B40, 0xC901, 0x09C0, 0x0880, 0xC841,\n    0xD801, 0x18C0, 0x1980, 0xD941, 0x1B00, 0xDBC1, 0xDA81, 0x1A40,\n    0x1E00, 0xDEC1, 0xDF81, 0x1F40, 0xDD01, 0x1DC0, 0x1C80, 0xDC41,\n    0x1400, 0xD4C1, 0xD581, 0x1540, 0xD701, 0x17C0, 0x1680, 0xD641,\n    0xD201, 0x12C0, 0x1380, 0xD341, 0x1100, 0xD1C1, 0xD081, 0x1040,\n    0xF001, 0x30C0, 0x3180, 0xF141, 0x3300, 0xF3C1, 0xF281, 0x3240,\n    0x3600, 0xF6C1, 0xF781, 0x3740, 0xF501, 0x35C0, 0x3480, 0xF441,\n    0x3C00, 0xFCC1, 0xFD81, 0x3D40, 0xFF01, 0x3FC0, 0x3E80, 0xFE41,\n    0xFA01, 0x3AC0, 0x3B80, 0xFB41, 0x3900, 0xF9C1, 0xF881, 0x3840,\n    0x2800, 0xE8C1, 0xE981, 0x2940, 0xEB01, 0x2BC0, 0x2A80, 0xEA41,\n    0xEE01, 0x2EC0, 0x2F80, 0xEF41, 0x2D00, 0xEDC1, 0xEC81, 0x2C40,\n    0xE401, 0x24C0, 0x2580, 0xE541, 0x2700, 0xE7C1, 0xE681, 0x2640,\n    0x2200, 0xE2C1, 0xE381, 0x2340, 0xE101, 0x21C0, 0x2080, 0xE041,\n    0xA001, 0x60C0, 0x6180, 0xA141, 0x6300, 0xA3C1, 0xA281, 0x6240,\n    0x6600, 0xA6C1, 0xA781, 0x6740, 0xA501, 0x65C0, 0x6480, 0xA441,\n    0x6C00, 0xACC1, 0xAD81, 0x6D40, 0xAF01, 0x6FC0, 0x6E80, 0xAE41,\n    0xAA01, 0x6AC0, 0x6B80, 0xAB41, 0x6900, 0xA9C1, 0xA881, 0x6840,\n    0x7800, 0xB8C1, 0xB981, 0x7940, 0xBB01, 0x7BC0, 0x7A80, 0xBA41,\n    0xBE01, 0x7EC0, 0x7F80, 0xBF41, 0x7D00, 0xBDC1, 0xBC81, 0x7C40,\n    0xB401, 0x74C0, 0x7580, 0xB541, 0x7700, 0xB7C1, 0xB681, 0x7640,\n    0x7200, 0xB2C1, 0xB381, 0x7340, 0xB101, 0x71C0, 0x7080, 0xB041,\n    0x5000, 0x90C1, 0x9181, 0x5140, 0x9301, 0x53C0, 0x5280, 0x9241,\n    0x9601, 0x56C0, 0x5780, 0x9741, 0x5500, 0x95C1, 0x9481, 0x5440,\n    0x9C01, 0x5CC0, 0x5D80, 0x9D41, 0x5F00, 0x9FC1, 0x9E81, 0x5E40,\n    0x5A00, 0x9AC1, 0x9B81, 0x5B40, 0x9901, 0x59C0, 0x5880, 0x9841,\n    0x8801, 0x48C0, 0x4980, 0x8941, 0x4B00, 0x8BC1, 0x8A81, 0x4A40,\n    0x4E00, 0x8EC1, 0x8F81, 0x4F40, 0x8D01, 0x4DC0, 0x4C80, 0x8C41,\n    0x4400, 0x84C1, 0x8581, 0x4540, 0x8701, 0x47C0, 0x4680, 0x8641,\n    0x8201, 0x42C0, 0x4380, 0x8341, 0x4100, 0x81C1, 0x8081, 0x4040\n]);\n\n\/**\n * \u8ba1\u7b97 Modbus CRC-16\n * @param {Uint8Array|number[]|Buffer} data\n * @returns {number} 16 \u4f4d CRC \u503c\n *\/\nfunction modbusCRC16(data) {\n    let crc = 0xFFFF;\n    for (let i = 0; i <data>&gt;&gt; 8) ^ CRC16_TABLE[pos];\n    }\n    return crc;\n}\n\n\/**\n * \u9a8c\u8bc1\u5e26\u6709 CRC \u7684\u5e27\u662f\u5426\u5408\u6cd5\n * @param {Uint8Array} frame \u5b8c\u6574\u5e27\uff08\u542b CRC\uff09\n * @returns {boolean}\n *\/\nfunction verifyModbusFrame(frame) {\n    return modbusCRC16(frame) === 0;\n}\n\n\/**\n * \u8ba1\u7b97 CRC \u5e76\u9644\u52a0\u5230\u6570\u636e\u672b\u5c3e\n * @param {Uint8Array|number[]} data\n * @returns {Uint8Array}\n *\/\nfunction appendCRC(data) {\n    const crc = modbusCRC16(data);\n    return new Uint8Array([...data, crc &amp; 0xFF, (crc &gt;&gt; 8) &amp; 0xFF]);\n}\n\n\/\/ ===== \u4f7f\u7528\u793a\u4f8b =====\nconst request = new Uint8Array([0x01, 0x03, 0x00, 0x00, 0x00, 0x01]);\nconst crc = modbusCRC16(request);\nconsole.log(`CRC-16: 0x${crc.toString(16).toUpperCase().padStart(4, '0')}`);\n\/\/ \u8f93\u51fa: CRC-16: 0x0ACA\n\nconst fullFrame = appendCRC(request);\nconsole.log('\u5b8c\u6574\u5e27:', Array.from(fullFrame)\n    .map(b =&gt; '0x' + b.toString(16).toUpperCase().padStart(2, '0'))\n    .join(' '));\n\/\/ \u8f93\u51fa: 0x01 0x03 0x00 0x00 0x00 0x01 0xCA 0x0A<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e94\u3001LRC \u6821\u9a8c\u539f\u7406\u4e0e\u5b9e\u73b0<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">5.1 LRC \u8ba1\u7b97\u89c4\u5219<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">LRC\uff08Longitudinal Redundancy Check\uff09\u7528\u4e8e Modbus ASCII \u6a21\u5f0f\u3002\u8ba1\u7b97\u65b9\u5f0f\u975e\u5e38\u7b80\u5355\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u5c06\u6d88\u606f\u5e27\u4e2d\u6240\u6709\u5b57\u8282\uff08\u4ece\u5730\u5740\u7801\u5230\u6700\u540e\u4e00\u4e2a\u6570\u636e\u5b57\u8282\uff09\u7d2f\u52a0<\/li>\n<li>\u4e22\u5f03\u8fdb\u4f4d\uff08\u53ea\u4fdd\u7559\u4f4e 8 \u4f4d\uff09<\/li>\n<li>\u53d6\u4e8c\u8fdb\u5236\u8865\u7801\uff08\u5373\u53d6\u53cd\u52a0\u4e00\uff0c\u6216\u76f4\u63a5\u7528 256 &#8211; sum\uff09<\/li>\n<li>\u5c06\u7ed3\u679c\u8f6c\u6362\u4e3a\u4e24\u4e2a ASCII \u5b57\u7b26\uff08\u9ad8\u534a\u5b57\u8282\u548c\u4f4e\u534a\u5b57\u8282\u5404\u4e00\u4e2a\u5b57\u7b26\uff09<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>LRC \u6570\u5b66\u516c\u5f0f\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>LRC = 0x100 - (sum(byte[0..N-1]) &amp; 0xFF)\n\n\u4f8b\uff1a\u5e27\u6570\u636e\u4e3a {0x01, 0x03, 0x00, 0x00, 0x00, 0x01}\nsum = 0x01 + 0x03 + 0x00 + 0x00 + 0x00 + 0x01 = 0x05\nLRC = 0x100 - 0x05 = 0xFB\n\n\u5728 ASCII \u5e27\u4e2d\u8868\u793a\u4e3a\u5b57\u7b26\u4e32 \"FB\"<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">5.2 LRC \u5b8c\u6574\u5b9e\u73b0<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>\/**\n * C \u8bed\u8a00\u5b9e\u73b0 Modbus LRC \u8ba1\u7b97\n * \u8fd4\u56de\u503c\u4e3a LRC \u503c\uff088 \u4f4d\uff09\n *\/\nuint8_t modbus_lrc(uint8_t *buf, uint16_t len)\n{\n    uint16_t sum = 0;\n    uint16_t i;\n\n    for (i = 0; i  int:\n    \"\"\"\u8ba1\u7b97 Modbus ASCII LRC\"\"\"\n    sum_val = sum(data) &amp; 0xFF\n    return (-sum_val) &amp; 0xFF  # \u7b49\u6548\u4e8e (256 - sum_val) &amp; 0xFF\n\n# \u4f7f\u7528\u793a\u4f8b\ndata = bytes([0x01, 0x03, 0x00, 0x00, 0x00, 0x01])\nlrc = modbus_lrc(data)\nprint(f\"LRC: 0x{lrc:02X}\")  # \u8f93\u51fa: 0xFB\n\n\/**\n * JavaScript \u5b9e\u73b0\n *\/\nfunction modbusLRC(data) {\n    let sum = 0;\n    for (let i = 0; i &lt; data.length; i++) {\n        sum = (sum + data[i]) &amp; 0xFF;\n    }\n    return (0x100 - sum) &amp; 0xFF;\n}\n\n\/\/ \u4f7f\u7528\u793a\u4f8b\nconst testData = [0x01, 0x03, 0x00, 0x00, 0x00, 0x01];\nconsole.log(`LRC: 0x${modbusLRC(testData).toString(16).toUpperCase()}`);\n\/\/ \u8f93\u51fa: LRC: 0xFB<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u516d\u3001CRC \u6821\u9a8c\u9a8c\u8bc1\u6280\u5de7<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0cCRC \u5b9e\u73b0\u7684\u6b63\u786e\u6027\u9a8c\u8bc1\u5f80\u5f80\u6bd4\u5b9e\u73b0\u672c\u8eab\u66f4\u8017\u8d39\u65f6\u95f4\u3002\u4e0b\u9762\u4ecb\u7ecd\u51e0\u79cd\u5b9e\u7528\u7684\u9a8c\u8bc1\u65b9\u6cd5\u548c\u6280\u5de7\uff0c\u5e2e\u52a9\u4f60\u5feb\u901f\u5b9a\u4f4d CRC \u8ba1\u7b97\u4e2d\u7684\u95ee\u9898\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">6.1 \u5168\u5e27\u9a8c\u8bc1\u6cd5<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">CRC \u6709\u4e00\u4e2a\u975e\u5e38\u5b9e\u7528\u7684\u7279\u6027\uff1a<strong>\u5c06\u63a5\u6536\u5230\u5b8c\u6574\u5e27\uff08\u6570\u636e + CRC\uff09\u518d\u8ba1\u7b97\u4e00\u6b21 CRC-16\uff0c\u7ed3\u679c\u5e94\u8be5\u4e3a 0x0000<\/strong>\u3002\u8fd9\u662f\u9a8c\u8bc1\u5e27\u5b8c\u6574\u6027\u7684\u6700\u4fbf\u6377\u65b9\u6cd5\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u63a5\u6536\u5e27\uff08\u6570\u636e + 2 \u5b57\u8282 CRC\uff09\nuint8_t received_frame[] = {0x01, 0x03, 0x02, 0x00, 0x64, 0xB9, 0xAF};\nuint16_t verify = modbus_crc16(received_frame, sizeof(received_frame));\n\nif (verify == 0) {\n    \/\/ \u5e27\u6821\u9a8c\u901a\u8fc7\uff0c\u6570\u636e\u6b63\u786e\n    printf(\"CRC OKn\");\n} else {\n    \/\/ \u5e27\u6821\u9a8c\u5931\u8d25\uff0c\u4e22\u5f03\u6b64\u5e27\n    printf(\"CRC Error: 0x%04Xn\", verify);\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">6.2 \u6d4b\u8bd5\u5411\u91cf<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u5f00\u53d1 CRC \u8ba1\u7b97\u51fd\u6570\u65f6\uff0c\u4f7f\u7528\u4ee5\u4e0b\u6807\u51c6\u6d4b\u8bd5\u5411\u91cf\u9a8c\u8bc1\u5b9e\u73b0\u7684\u6b63\u786e\u6027\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\">\n<thead>\n<tr><th>\u6d4b\u8bd5\u6570\u636e (HEX)<\/th><th>\u529f\u80fd\u63cf\u8ff0<\/th><th>\u671f\u671b CRC-16<\/th><\/tr>\n<\/thead>\n<tbody>\n<tr><td><code>01 03 00 00 00 01<\/code><\/td><td>\u8bfb\u53d6\u4fdd\u6301\u5bc4\u5b58\u5668\uff08\u6700\u5e38\u7528\uff09<\/td><td><code>0ACA<\/code><\/td><\/tr>\n<tr><td><code>01 03 00 00 00 0A<\/code><\/td><td>\u8bfb\u53d6 10 \u4e2a\u4fdd\u6301\u5bc4\u5b58\u5668<\/td><td><code>0548<\/code><\/td><\/tr>\n<tr><td><code>01 06 00 01 00 1E<\/code><\/td><td>\u5199\u5355\u4e2a\u5bc4\u5b58\u5668\uff08\u503c 30\uff09<\/td><td><code>99CB<\/code><\/td><\/tr>\n<tr><td><code>01 10 00 00 00 02 04 00 64 00 65<\/code><\/td><td>\u5199\u591a\u4e2a\u5bc4\u5b58\u5668<\/td><td>\u9700\u5b9e\u65f6\u8ba1\u7b97<\/td><\/tr>\n<tr><td><code>11 03 00 6B 00 03<\/code><\/td><td>\u4ece\u7ad9 17 \u8bfb\u53d6 3 \u4e2a\u5bc4\u5b58\u5668<\/td><td><code>7687<\/code><\/td><\/tr>\n<\/tbody>\n<\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e03\u3001\u5728\u7ebf CRC \u8ba1\u7b97\u5de5\u5177\u63a8\u8350<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ee5\u4e0b\u5de5\u5177\u53ef\u4ee5\u5e2e\u52a9\u5feb\u901f\u9a8c\u8bc1 CRC \u8ba1\u7b97\u7ed3\u679c\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>modbus.cn \u5728\u7ebf\u5de5\u5177\uff1a<\/strong>\u63d0\u4f9b Modbus \u4e13\u5c5e\u7684 CRC\/LRC \u5728\u7ebf\u8ba1\u7b97\u529f\u80fd<\/li>\n<li><strong>Sunshine2k CRC Calculator\uff1a<\/strong>\u652f\u6301\u591a\u79cd CRC \u7b97\u6cd5\u53c2\u6570\u7ec4\u5408\u7684\u5728\u7ebf\u8ba1\u7b97\u5668<\/li>\n<li><strong>Lammert Bies CRC\uff1a<\/strong>\u8be6\u7ec6\u7684 CRC \u8ba1\u7b97\u9875\u9762\uff0c\u652f\u6301\u81ea\u5b9a\u4e49\u591a\u9879\u5f0f<\/li>\n<li><strong>npm crc \u5305\uff1a<\/strong>Node.js \u73af\u5883\u4e0b\u53ef\u901a\u8fc7 <code>npm install crc<\/code> \u5b89\u88c5<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f7f\u7528\u5728\u7ebf\u5de5\u5177\u65f6\u7684\u6ce8\u610f\u4e8b\u9879\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u786e\u8ba4\u591a\u9879\u5f0f\u53c2\u6570\uff08Modbus = 0x8005 \/ 0xA001\uff09<\/li>\n<li>\u786e\u8ba4\u521d\u59cb\u503c\uff08Modbus = 0xFFFF\uff09<\/li>\n<li>\u786e\u8ba4\u7ed3\u679c\u5f02\u6216\u503c\uff08Modbus = 0x0000\uff0c\u4e0d\u505a\u5f02\u6216\uff09<\/li>\n<li>\u786e\u8ba4\u8f93\u5165\u8f93\u51fa\u662f\u5426\u53cd\u8f6c\uff08Modbus \u90fd\u4e0d\u53cd\u8f6c\uff09<\/li>\n<li>\u6ce8\u610f\u5b57\u8282\u5e8f\uff1a\u5728\u7ebf\u5de5\u5177\u901a\u5e38\u8f93\u51fa\u5927\u7aef\u5e8f\uff0cModbus RTU \u5e27\u4e2d CRC \u5b58\u50a8\u4e3a\u5c0f\u7aef\u5e8f<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u516b\u3001\u6821\u9a8c\u5931\u8d25\u7684\u6392\u67e5\u6307\u5357<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5f53 Modbus \u901a\u4fe1\u4e2d\u51fa\u73b0 CRC\/LRC \u6821\u9a8c\u9519\u8bef\u65f6\uff0c\u6309\u7167\u4ee5\u4e0b\u6b65\u9aa4\u9010\u5c42\u6392\u67e5\uff1a<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">8.1 \u6392\u67e5\u6e05\u5355<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\">\n<thead>\n<tr><th>\u6392\u67e5\u9879<\/th><th>\u5e38\u89c1\u95ee\u9898<\/th><th>\u89e3\u51b3\u65b9\u6cd5<\/th><\/tr>\n<\/thead>\n<tbody>\n<tr><td><strong>CRC \u7b97\u6cd5\u5b9e\u73b0<\/strong><\/td><td>\u4f7f\u7528\u4e86\u9519\u8bef\u7684\u591a\u9879\u5f0f\uff080x8005 \u800c\u975e 0xA001\uff09<\/td><td>\u786e\u8ba4\u4f7f\u7528 0xA001 \u53f3\u79fb\u6cd5\u6216 0x8005 \u5de6\u79fb\u6cd5<\/td><\/tr>\n<tr><td><strong>CRC \u521d\u59cb\u503c<\/strong><\/td><td>\u4f7f\u7528 0x0000 \u800c\u975e 0xFFFF<\/td><td>Modbus \u6807\u51c6\u89c4\u5b9a\u521d\u59cb\u503c\u5fc5\u987b\u4e3a 0xFFFF<\/td><\/tr>\n<tr><td><strong>CRC \u8ba1\u7b97\u8303\u56f4<\/strong><\/td><td>\u5305\u542b\u4e86 CRC \u81ea\u8eab\uff0c\u6216\u9057\u6f0f\u4e86\u5730\u5740\u5b57\u8282<\/td><td>\u8ba1\u7b97\u8303\u56f4\u4e3a\u5730\u5740\u7801\u5230\u6700\u540e\u4e00\u4e2a\u6570\u636e\u5b57\u8282\uff08\u4e0d\u542b CRC\uff09<\/td><\/tr>\n<tr><td><strong>\u5b57\u8282\u5e8f<\/strong><\/td><td>CRC \u9ad8\u4f4e\u5b57\u8282\u53cd\u8f6c<\/td><td>Modbus RTU \u4e2d CRC \u4f4e\u5b57\u8282\u5728\u524d\uff08\u5c0f\u7aef\u5e8f\uff09<\/td><\/tr>\n<tr><td><strong>\u5e27\u8fb9\u754c<\/strong><\/td><td>3.5 \u5b57\u7b26\u65f6\u95f4\u95f4\u9694\u8bbe\u7f6e\u4e0d\u5f53<\/td><td>RTU \u6a21\u5f0f\u4f7f\u7528 &gt;3.5 \u5b57\u7b26\u9759\u9ed8\u4f5c\u4e3a\u5e27\u95f4\u9694<\/td><\/tr>\n<tr><td><strong>\u6ce2\u7279\u7387<\/strong><\/td><td>\u6536\u53d1\u53cc\u65b9\u6ce2\u7279\u7387\u4e0d\u4e00\u81f4<\/td><td>\u5728\u5e38\u7528\u901f\u7387\uff089600\/19200\/38400\/115200\uff09\u4e2d\u9009\u62e9<\/td><\/tr>\n<tr><td><strong>\u7269\u7406\u63a5\u7ebf<\/strong><\/td><td>A\/B \u7ebf\u63a5\u53cd\u3001\u7ec8\u7aef\u7535\u963b\u7f3a\u5931<\/td><td>\u68c0\u67e5 RS-485 \u7684 A(+)\u3001B(-) \u6781\u6027\uff0c\u4e24\u7aef\u52a0 120\u03a9 \u7ec8\u7aef\u7535\u963b<\/td><\/tr>\n<tr><td><strong>ASCII \u6a21\u5f0f<\/strong><\/td><td>\u8bef\u5c06 RTU \u4e8c\u8fdb\u5236\u5e27\u5f53\u4f5c ASCII \u6587\u672c\u5904\u7406<\/td><td>ASCII \u6a21\u5f0f\u5e27\u4ee5 &#8216;:&#8217; \u5f00\u5934\uff0c\u4ee5 CRLF \u7ed3\u5c3e<\/td><\/tr>\n<\/tbody>\n<\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">8.2 \u8c03\u8bd5\u4ee3\u7801\u793a\u4f8b<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>\/**\n * \u5e26\u8be6\u7ec6\u65e5\u5fd7\u7684 CRC \u8ba1\u7b97\u8c03\u8bd5\u7248\u672c\n *\/\nuint16_t modbus_crc16_debug(uint8_t *buf, uint16_t len)\n{\n    uint16_t crc = 0xFFFF;\n    uint16_t i;\n\n    printf(\"=== CRC-16 Debug Trace ===n\");\n    printf(\"Initial CRC: 0x%04Xn\", crc);\n\n    for (i = 0; i &gt; 8) ^ crc16_table[pos];\n\n        printf(\"Byte[%2d]=0x%02X, \"\n               \"prev_lo=0x%02X, \"\n               \"index=0x%02X, \"\n               \"table_val=0x%04X, \"\n               \"new_crc=0x%04Xn\",\n               i, buf[i], prev_crc_lo, pos,\n               crc16_table[pos], crc);\n    }\n\n    printf(\"Final CRC: 0x%04Xn\", crc);\n    printf(\"RTU Frame CRC (Little-Endian): 0x%02X 0x%02Xn\",\n           crc &amp; 0xFF, crc &gt;&gt; 8);\n    printf(\"============================n\");\n    return crc;\n}\n\n\/* \u793a\u4f8b\u8f93\u51fa\uff1a\nBuf: {0x01, 0x03, 0x00, 0x00, 0x00, 0x01}\n=== CRC-16 Debug Trace ===\nInitial CRC: 0xFFFF\nByte[ 0]=0x01, prev_lo=0xFF, index=0xFE, table_val=0x4040, new_crc=0xC0C0\nByte[ 1]=0x03, prev_lo=0xC0, index=0xC3, table_val=0x0280, new_crc=0x0281\nByte[ 2]=0x00, prev_lo=0x81, index=0x81, table_val=0x4040, new_crc=0x4042\n...\nFinal CRC: 0x0ACA\nRTU Frame CRC (Little-Endian): 0xCA 0x0A\n*\/<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e5d\u3001\u6027\u80fd\u5bf9\u6bd4\uff1a\u67e5\u8868\u6cd5 vs \u4f4d\u79fb\u6cd5<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u5b9e\u9645\u5de5\u7a0b\u4e2d\uff0cCRC \u8ba1\u7b97\u7684\u6027\u80fd\u76f4\u63a5\u5f71\u54cd Modbus \u901a\u4fe1\u7684\u541e\u5410\u91cf\u3002\u4ee5\u4e0b\u662f\u4e24\u79cd\u65b9\u6cd5\u5728 ARM Cortex-M4 (168MHz) \u548c x86-64 (3.2GHz) \u5e73\u53f0\u4e0a\u7684\u5b9e\u6d4b\u5bf9\u6bd4\u3002<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\">\n<thead>\n<tr><th>\u6d4b\u8bd5\u6761\u4ef6<\/th><th>\u4f4d\u79fb\u6cd5<\/th><th>\u67e5\u8868\u6cd5<\/th><th>\u52a0\u901f\u6bd4<\/th><\/tr>\n<\/thead>\n<tbody>\n<tr><td><strong>ARM Cortex-M4, 1 \u5b57\u8282<\/strong><\/td><td>~2.4 \u03bcs<\/td><td>~0.3 \u03bcs<\/td><td>8x<\/td><\/tr>\n<tr><td><strong>ARM Cortex-M4, 256 \u5b57\u8282<\/strong><\/td><td>~615 \u03bcs<\/td><td>~77 \u03bcs<\/td><td>8x<\/td><\/tr>\n<tr><td><strong>x86-64, 1 \u5b57\u8282<\/strong><\/td><td>~0.08 \u03bcs<\/td><td>~0.02 \u03bcs<\/td><td>4x<\/td><\/tr>\n<tr><td><strong>x86-64, 256 \u5b57\u8282<\/strong><\/td><td>~20 \u03bcs<\/td><td>~3 \u03bcs<\/td><td>~6.7x<\/td><\/tr>\n<tr><td><strong>\u4ee3\u7801\u4f53\u79ef<\/strong><\/td><td>\u7ea6 50 \u5b57\u8282<\/td><td>\u7ea6 550 \u5b57\u8282\uff08512 \u5b57\u8282\u8868 + 38 \u5b57\u8282\u903b\u8f91\uff09<\/td><td>\u2014<\/td><\/tr>\n<tr><td><strong>RAM \u5360\u7528<\/strong><\/td><td>6 \u5b57\u8282\uff08\u53d8\u91cf\uff09<\/td><td>518 \u5b57\u8282\uff08\u8868 + \u53d8\u91cf\uff09<\/td><td>\u2014<\/td><\/tr>\n<\/tbody>\n<\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u9009\u578b\u5efa\u8bae\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u5d4c\u5165\u5f0f MCU\uff08Flash \u2265 2KB\u3001\u9700\u9ad8\u9891\u901a\u4fe1\uff09\uff1a<\/strong>\u4f18\u5148\u4f7f\u7528\u67e5\u8868\u6cd5\uff0c512 \u5b57\u8282\u7684 ROM \u5f00\u9500\u6362\u53d6 8 \u500d\u7684\u901f\u5ea6\u63d0\u5347\u975e\u5e38\u5212\u7b97<\/li>\n<li><strong>Flash \u4e25\u91cd\u53d7\u9650 (&lt;512B)\uff1a<\/strong>\u4f7f\u7528\u4f4d\u79fb\u6cd5\uff0c\u7a7a\u95f4\u6362\u65f6\u95f4\u4e0d\u53ef\u884c\u65f6\u63a5\u53d7\u8f83\u4f4e\u7684\u901a\u4fe1\u6027\u80fd<\/li>\n<li><strong>\u4e0a\u4f4d\u673a\/\u670d\u52a1\u5668\uff1a<\/strong>\u6beb\u4e0d\u72b9\u8c6b\u7528\u67e5\u8868\u6cd5\uff0c\u51e0 KB \u5185\u5b58\u5b8c\u5168\u4e0d\u662f\u95ee\u9898<\/li>\n<li><strong>\u5b66\u4e60\u9a8c\u8bc1\u9636\u6bb5\uff1a<\/strong>\u5148\u5b9e\u73b0\u4f4d\u79fb\u6cd5\u7406\u89e3\u539f\u7406\uff0c\u518d\u7528\u6d4b\u8bd5\u5411\u91cf\u9a8c\u8bc1\u540e\u5207\u6362\u5230\u67e5\u8868\u6cd5<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u5341\u3001\u786c\u4ef6 CRC \u52a0\u901f<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u73b0\u4ee3 MCU \u548c\u5904\u7406\u5668\u901a\u5e38\u5185\u7f6e\u786c\u4ef6 CRC \u8ba1\u7b97\u5355\u5143\uff0c\u53ef\u4ee5\u8fdb\u4e00\u6b65\u5c06 CRC \u8ba1\u7b97\u901f\u5ea6\u63d0\u5347 10-50 \u500d\u3002\u786c\u4ef6 CRC \u7684\u91cd\u8981\u6027\u5728\u5de5\u4e1a\u7f51\u5173\u548c\u534f\u8bae\u8f6c\u6362\u5668\u4e2d\u5c24\u4e3a\u7a81\u51fa\u2014\u2014\u8fd9\u4e9b\u8bbe\u5907\u901a\u5e38\u9700\u8981\u540c\u65f6\u5904\u7406\u6570\u5341\u8def Modbus RTU \u901a\u4fe1\uff0cCRC \u8ba1\u7b97\u7684\u5f00\u9500\u5360\u6bd4\u4e0d\u53ef\u5ffd\u89c6\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u9009\u62e9\u786c\u4ef6 CRC \u65b9\u6848\u65f6\uff0c\u9700\u8981\u7efc\u5408\u8003\u8651\u4e09\u4e2a\u56e0\u7d20\uff1a\u9996\u5148\uff0c\u786c\u4ef6 CRC \u5355\u5143\u662f\u5426\u652f\u6301\u81ea\u5b9a\u4e49\u591a\u9879\u5f0f\uff08\u8bb8\u591a\u8001\u6b3e MCU \u7684 CRC \u6a21\u5757\u4ec5\u652f\u6301\u56fa\u5b9a\u7684 32 \u4f4d CRC-32\uff0c\u65e0\u6cd5\u76f4\u63a5\u7528\u4e8e Modbus\uff09\uff1b\u5176\u6b21\uff0c\u786c\u4ef6 CRC \u7684\u8f93\u5165\u6570\u636e\u5bf9\u9f50\u8981\u6c42\uff08\u90e8\u5206\u786c\u4ef6 CRC \u8981\u6c42 32 \u4f4d\u6216 16 \u4f4d\u5bf9\u9f50\u7684\u6570\u636e\u8f93\u5165\uff0c\u5bf9\u4e8e Modbus RTU \u7684\u5b57\u8282\u6d41\u9700\u8981\u989d\u5916\u5904\u7406\uff09\uff1b\u6700\u540e\uff0cDMA \u914d\u5408\u4f7f\u7528\u7684\u53ef\u884c\u6027\u2014\u2014\u5982\u679c\u80fd\u901a\u8fc7 DMA \u76f4\u63a5\u5c06\u4e32\u53e3\u63a5\u6536\u7f13\u51b2\u533a\u9001\u5165 CRC \u8ba1\u7b97\u5355\u5143\uff0c\u5c06\u5b9e\u73b0\u96f6 CPU \u5f00\u9500\u7684\u6821\u9a8c\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">10.1 STM32 \u786c\u4ef6 CRC<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">STM32 \u7cfb\u5217 MCU \u5185\u7f6e CRC \u8ba1\u7b97\u5355\u5143\uff0c\u4f46\u9ed8\u8ba4\u4f7f\u7528\u4e0d\u540c\u7684\u591a\u9879\u5f0f\uff080x4C11DB7\uff0c32 \u4f4d\uff09\u3002\u8981\u7528\u4e8e Modbus\uff0c\u9700\u8981\u76f4\u63a5\u64cd\u4f5c\u5bc4\u5b58\u5668\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/**\n * STM32 \u786c\u4ef6 CRC \u914d\u5408\u8f6f\u4ef6\u5b9e\u73b0 Modbus CRC-16\n * \n * \u7531\u4e8e STM32 \u786c\u4ef6 CRC \u6a21\u5757\u4f7f\u7528 32 \u4f4d\u591a\u9879\u5f0f\uff0c\n * \u4e0d\u76f4\u63a5\u517c\u5bb9 Modbus \u7684 16 \u4f4d CRC-16\uff0c\n * \u901a\u5e38\u4ecd\u9700\u8f6f\u4ef6\u5b9e\u73b0\u3002\u4f46\u53ef\u4ee5\u5229\u7528 DMA + \u67e5\u8868\u6cd5\u52a0\u901f\u3002\n * \n * \u90e8\u5206 STM32 \u578b\u53f7\uff08\u5982 G4\u3001H7 \u7cfb\u5217\uff09\u652f\u6301\u81ea\u5b9a\u4e49\u591a\u9879\u5f0f\uff0c\n * \u53ef\u914d\u7f6e\u4e3a 0xA001 \u5b9e\u73b0\u786c\u4ef6 CRC-16 \u8ba1\u7b97\uff1a\n *\/\n\n\/\/ STM32G4\/H7 \u7cfb\u5217\uff0c\u914d\u7f6e CRC \u5355\u5143\u4e3a Modbus CRC-16\nvoid hw_crc16_init(void)\n{\n    __HAL_RCC_CRC_CLK_ENABLE();\n    \n    CRC-&gt;POL = 0x8005;      \/\/ \u591a\u9879\u5f0f\uff08\u6b63\u5411\uff09\n    CRC-&gt;INIT = 0xFFFF;     \/\/ \u521d\u59cb\u503c\n    CRC-&gt;CR |= CRC_CR_REV_OUT;  \/\/ \u8f93\u51fa\u4f4d\u53cd\u8f6c\n    CRC-&gt;CR &amp;= ~CRC_CR_REV_IN;  \/\/ \u8f93\u5165\u4e0d\u53cd\u8f6c\n}\n\nuint16_t hw_modbus_crc16(uint8_t *buf, uint32_t len)\n{\n    CRC-&gt;INIT = 0xFFFF;\n    \n    while (len &gt;= 4) {\n        CRC-&gt;DR = *(uint32_t *)buf;\n        buf += 4;\n        len -= 4;\n    }\n    \n    \/\/ \u5904\u7406\u5269\u4f59\u5b57\u8282\n    while (len &gt;= 2) {\n        CRC-&gt;DR = *(uint16_t *)buf;\n        buf += 2;\n        len -= 2;\n    }\n    \n    if (len) {\n        CRC-&gt;DR = *buf;\n    }\n    \n    return (uint16_t)(CRC-&gt;DR &amp; 0xFFFF);\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">10.2 x86 SSE4.2 CRC32 \u6307\u4ee4<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Intel\/AMD \u5904\u7406\u5668\u81ea SSE4.2 \u6307\u4ee4\u96c6\u8d77\u63d0\u4f9b <code>CRC32<\/code> \u786c\u4ef6\u6307\u4ee4\u3002\u4f46\u9700\u6ce8\u610f\uff0cx86 \u7684 CRC32 \u6307\u4ee4\u4f7f\u7528\u4e0d\u540c\u591a\u9879\u5f0f\uff080x1EDC6F41\uff09\uff0c\u4e0d\u76f4\u63a5\u517c\u5bb9 Modbus CRC-16\u3002\u5728 x86 \u5e73\u53f0\u4e0a\uff0c\u67e5\u8868\u6cd5\u901a\u5e38\u5df2\u8db3\u591f\u5feb\uff08\u5904\u7406 256 \u5b57\u8282\u4ec5\u9700 3 \u5fae\u79d2\uff09\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u786e\u5b9e\u9700\u8981\u786c\u4ef6\u52a0\u901f Modbus CRC-16\uff0c\u53ef\u4ee5\u4f7f\u7528 <strong>FPGA \u6216 CPLD<\/strong> \u5b9e\u73b0\u4e13\u7528\u7684 CRC \u8ba1\u7b97\u903b\u8f91\u3002\u8fd9\u5728\u5de5\u4e1a\u7f51\u5173\u8bbe\u5907\u4e2d\u5e38\u89c1\u2014\u2014\u901a\u8fc7 FPGA \u5e76\u884c\u5904\u7406\u591a\u8def\u4e32\u53e3\u6570\u636e\u6d41\u7684 CRC \u6821\u9a8c\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5341\u4e00\u3001CRC \u8ba1\u7b97\u4e2d\u7684\u5e38\u89c1\u9677\u9631<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u591a\u9879\u5f0f\u6df7\u6dc6\uff1a<\/strong>\u7f51\u4e0a\u7684 CRC \u8ba1\u7b97\u5668\u9ed8\u8ba4\u53c2\u6570\u53ef\u80fd\u4e0e Modbus \u4e0d\u540c\u3002\u59cb\u7ec8\u9a8c\u8bc1 0xA001 + \u521d\u59cb\u503c 0xFFFF \u7684\u7ec4\u5408\u3002<\/li>\n<li><strong>\u5b57\u8282\u5e8f\u707e\u96be\uff1a<\/strong>Modbus \u5e27\u4e2d CRC \u662f\u5c0f\u7aef\u5e8f\uff08\u4f4e\u5b57\u8282\u5728\u524d\uff09\uff0c\u4f46\u8c03\u8bd5\u8f93\u51fa\u5e38\u4e60\u60ef\u5927\u7aef\u5e8f\u3002\u53d1\u9001\u65f6\u5fc5\u987b\u786e\u4fdd\u987a\u5e8f\u6b63\u786e\u3002<\/li>\n<li><strong>\u8868\u751f\u6210\u9519\u8bef\uff1a<\/strong>\u5982\u679c\u81ea\u5df1\u5199\u8868\u751f\u6210\u4ee3\u7801\uff0c\u786e\u4fdd\u4f4d\u79fb\u548c\u5f02\u6216\u7684\u987a\u5e8f\u4e0e\u4e3b\u8ba1\u7b97\u4e00\u81f4\u3002<\/li>\n<li><strong>\u6570\u636e\u7c7b\u578b\u6ea2\u51fa\uff1a<\/strong>\u5728 16 \u4f4d\u7cfb\u7edf\u4e2d\uff0c\u672a\u4f7f\u7528 <code>(uint8_t)(crc ^ byte)<\/code> \u800c\u76f4\u63a5\u64cd\u4f5c\u53ef\u80fd\u5bfc\u81f4\u9ad8 8 \u4f4d\u6c61\u67d3\u3002<\/li>\n<li><strong>\u5e27\u8fb9\u754c\u8bef\u5224\uff1a<\/strong>CRC \u8ba1\u7b97\u8303\u56f4\u4e0d\u80fd\u5305\u542b\u5e27\u95f4\u9694\u9759\u9ed8\u65f6\u95f4\u548c CRC \u81ea\u8eab\u3002<\/li>\n<li><strong>RTU \u548c ASCII \u6df7\u7528\uff1a<\/strong>ASCII \u6a21\u5f0f\u7528 LRC \u800c\u975e CRC\uff0c\u786e\u8ba4\u4f60\u4f7f\u7528\u7684\u662f\u6b63\u786e\u7684\u6821\u9a8c\u65b9\u5f0f\u3002<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">\u5341\u4e8c\u3001FAQ\uff1aCRC\/LRC \u5e38\u89c1\u95ee\u9898<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q1: Modbus \u4e3a\u4ec0\u4e48\u4e0d\u76f4\u63a5\u7528\u6807\u51c6\u7684 CRC-16-CCITT\uff1f<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A: CRC-16-CCITT\uff08\u591a\u9879\u5f0f 0x1021\uff09\u662f\u53e6\u4e00\u4e2a\u5e7f\u6cdb\u4f7f\u7528\u7684 CRC \u6807\u51c6\u3002Modbus \u4f7f\u7528 0x8005 \u591a\u9879\u5f0f\u662f Modicon \u516c\u53f8\u5728 1979 \u5e74\u7684\u5386\u53f2\u9009\u62e9\u3002\u4e24\u79cd CRC \u5728\u9519\u8bef\u68c0\u6d4b\u80fd\u529b\u4e0a\u51e0\u4e4e\u76f8\u540c\uff0c\u4f46\u56e0\u4e3a\u5b9e\u73b0\u7ec6\u8282\uff08\u521d\u59cb\u503c\u3001\u53cd\u8f6c\u7b49\uff09\u4e0d\u540c\uff0c\u7ed3\u679c\u4e92\u4e0d\u517c\u5bb9\u3002\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\u5fc5\u987b\u4e25\u683c\u6309\u7167 Modbus \u89c4\u8303\u5b9e\u73b0\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q2: \u80fd\u5426\u8df3\u8fc7 CRC \u6821\u9a8c\u4ee5\u52a0\u5feb\u901a\u4fe1\u901f\u5ea6\uff1f<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A: <strong>\u5f3a\u70c8\u4e0d\u63a8\u8350\u3002<\/strong>CRC \u7684\u8ba1\u7b97\u5f00\u9500\u5728\u73b0\u4ee3 MCU \u4e0a\u5fae\u4e4e\u5176\u5fae\uff08\u67e5\u8868\u6cd5\u5904\u7406\u4e00\u4e2a\u5178\u578b Modbus \u5e27\u4ec5\u9700\u6570\u5341\u5fae\u79d2\uff09\u3002\u5728\u5de5\u4e1a\u73af\u5883\u4e2d\uff0c\u8df3\u8fc7 CRC \u7b49\u4e8e\u653e\u5f03\u9519\u8bef\u68c0\u6d4b\u2014\u2014\u4e00\u4e2a\u5e72\u6270\u8109\u51b2\u5c31\u53ef\u80fd\u8ba9\u8bbe\u5907\u6267\u884c\u9519\u8bef\u6307\u4ee4\u3002\u5982\u679c\u8ffd\u6c42\u901f\u5ea6\uff0c\u53ef\u4ee5\u4ece\u6ce2\u7279\u7387\u3001\u6570\u636e\u6253\u5305\u3001\u534f\u8bae\u8f6c\u6362\u7b49\u65b9\u9762\u4f18\u5316\uff0c\u800c\u4e0d\u662f\u727a\u7272\u6570\u636e\u5b8c\u6574\u6027\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q3: LRC \u548c CRC \u53ef\u4ee5\u4e92\u6362\u5417\uff1f<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A: \u4e0d\u53ef\u4ee5\u3002LRC \u53ea\u7528\u4e8e ASCII \u6a21\u5f0f\uff0cCRC-16 \u53ea\u7528\u4e8e RTU \u6a21\u5f0f\u3002\u4e24\u8005\u5728\u540c\u4e00\u4e2a\u7f51\u7edc\u4e2d\u4e0d\u80fd\u6df7\u7528\uff0c\u56e0\u4e3a\u5e27\u683c\u5f0f\u548c\u5e27\u5206\u9694\u65b9\u5f0f\u5b8c\u5168\u4e0d\u540c\uff08RTU \u7528\u65f6\u95f4\u95f4\u9694\uff0cASCII \u7528\u5192\u53f7\u548c\u56de\u8f66\u6362\u884c\uff09\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q4: \u5728\u7ebf CRC \u8ba1\u7b97\u5668\u7b97\u51fa\u6765\u7684\u503c\u548c\u6211\u7684\u7a0b\u5e8f\u4e0d\u4e00\u81f4\u600e\u4e48\u529e\uff1f<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A: \u6309\u4ee5\u4e0b\u6b65\u9aa4\u6392\u67e5\uff1a(1) \u786e\u8ba4\u591a\u9879\u5f0f\u662f 0x8005 \u6216 0xA001\uff1b(2) \u786e\u8ba4\u521d\u59cb\u503c\u662f 0xFFFF\uff1b(3) \u786e\u8ba4\u7ed3\u679c\u5f02\u6216\u503c\u662f 0x0000\uff1b(4) \u786e\u8ba4\u8ba1\u7b97\u8303\u56f4\u4e0d\u5305\u542b CRC \u672c\u8eab\uff1b(5) \u4f7f\u7528\u672c\u6587\u63d0\u4f9b\u7684\u6d4b\u8bd5\u5411\u91cf\u9010\u5b57\u8282\u6bd4\u5bf9\u4e2d\u95f4\u7ed3\u679c\u300299% \u7684\u4e0d\u4e00\u81f4\u95ee\u9898\u90fd\u662f\u53c2\u6570\u914d\u7f6e\u9519\u8bef\u5bfc\u81f4\u7684\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q5: Python \u7684 binascii.crc_hqx() \u80fd\u7528\u4e8e Modbus \u5417\uff1f<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A: \u4e0d\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u3002<code>crc_hqx()<\/code> \u4f7f\u7528\u591a\u9879\u5f0f 0x1021 \u4e14\u521d\u59cb\u503c\u4e3a 0x0000\uff0c\u4e0e Modbus CRC-16 \u53c2\u6570\u5b8c\u5168\u4e0d\u540c\u3002\u63a8\u8350\u4f7f\u7528\u672c\u6587\u63d0\u4f9b\u7684 Python \u5b9e\u73b0\u6216 pymodbus \u5e93\u4e2d\u7684 CRC \u8ba1\u7b97\u51fd\u6570\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q6: CRC \u6821\u9a8c\u5931\u8d25\u4e00\u5b9a\u662f\u6570\u636e\u9519\u8bef\u5417\uff1f<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A: \u4e0d\u4e00\u5b9a\u3002\u4ee5\u4e0b\u60c5\u51b5\u4e5f\u53ef\u80fd\u5bfc\u81f4 CRC \u6821\u9a8c\u5931\u8d25\uff1a(1) \u6ce2\u7279\u7387\u4e0d\u5339\u914d\u5bfc\u81f4\u5b57\u8282\u89e3\u6790\u9519\u8bef\uff1b(2) \u5e27\u8fb9\u754c\u5224\u65ad\u9519\u8bef\uff0c\u591a\u8bfb\u6216\u5c11\u8bfb\u4e86\u5b57\u8282\uff1b(3) \u8bbe\u5907\u7684\u7ad9\u5730\u5740\u8bbe\u7f6e\u9519\u8bef\uff0c\u8bfb\u5230\u4e86\u7ed9\u5176\u4ed6\u8bbe\u5907\u7684\u54cd\u5e94\u5e27\uff1b(4) RS-485 \u6536\u53d1\u5668\u6545\u969c\uff0c\u6570\u636e\u88ab\u622a\u65ad\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q7: \u5982\u4f55\u5728\u6ca1\u6709\u6807\u51c6\u5e93\u7684 MCU \u4e0a\u5feb\u901f\u5b9e\u73b0 Modbus CRC\uff1f<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A: \u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u76f4\u63a5\u590d\u5236\u672c\u6587\u63d0\u4f9b\u7684 256 \u9879\u67e5\u627e\u8868\u5230\u4f60\u7684\u4ee3\u7801\u4e2d\u3002\u8fd9\u4e2a\u8868\u662f&#8221;\u7eaf\u6570\u636e&#8221;\u2014\u2014\u4e0d\u9700\u8981\u4efb\u4f55\u5916\u90e8\u4f9d\u8d56\uff0c\u4e0d\u8c03\u7528\u4efb\u4f55\u5e93\u51fd\u6570\u3002\u53ea\u9700\u8981\u4e00\u4e2a 512 \u5b57\u8282\u7684 const \u6570\u7ec4\uff08\u5efa\u8bae\u7528 <code>const<\/code> \u58f0\u660e\u653e\u5728 Flash \u4e2d\uff09\u548c\u51e0\u884c\u8ba1\u7b97\u903b\u8f91\u3002\u5bf9\u4e8e 8 \u4f4d MCU\uff08\u5982 8051\u3001AVR\uff09\uff0c\u5efa\u8bae\u4f7f\u7528\u4f4d\u79fb\u6cd5\uff0c\u56e0\u4e3a 512 \u5b57\u8282\u7684\u8868\u53ef\u80fd\u8d85\u51fa\u67d0\u4e9b\u7cbe\u7b80\u578b\u53f7\u7684 Flash \u5bb9\u91cf\u3002\u5bf9\u4e8e 32 \u4f4d MCU\uff08\u5982 STM32\u3001ESP32\uff09\uff0c\u67e5\u8868\u6cd5\u662f\u6700\u4f73\u9009\u62e9\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q8: \u4e3a\u4ec0\u4e48\u6709\u65f6\u5019 Modbus \u901a\u4fe1\u4e0d\u7a33\u5b9a\uff0cCRC \u9519\u4e00\u4e2a\u5bf9\u7684\u9519\u4e00\u4e2a\uff1f<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A: \u8fd9\u79cd\u95f4\u6b47\u6027\u9519\u8bef\u901a\u5e38\u4e0d\u662f CRC \u7b97\u6cd5\u7684\u95ee\u9898\uff0c\u800c\u662f\u7269\u7406\u5c42\u7684\u95ee\u9898\u3002\u5e38\u89c1\u539f\u56e0\u5305\u62ec\uff1aRS-485 \u603b\u7ebf\u7f3a\u5c11\u7ec8\u7aef\u7535\u963b\u6216\u7535\u963b\u503c\u4e0d\u5bf9\uff08\u6807\u51c6\u662f 120\u03a9 \u4e24\u7aef\u5404\u4e00\u4e2a\uff09\u3001\u603b\u7ebf\u8fc7\u957f\u5bfc\u81f4\u4fe1\u53f7\u8870\u51cf\u3001\u5171\u6a21\u7535\u538b\u8d85\u51fa RS-485 \u6536\u53d1\u5668\u8303\u56f4\uff08-7V \u5230 +12V\uff09\u3001\u591a\u4e2a\u8bbe\u5907\u7684\u504f\u7f6e\u7535\u963b\u53e0\u52a0\u5bfc\u81f4\u603b\u7ebf\u7a7a\u95f2\u7535\u5e73\u4e0d\u6b63\u786e\u3002\u5efa\u8bae\u4f7f\u7528\u793a\u6ce2\u5668\u89c2\u5bdf RS-485 \u5dee\u5206\u4fe1\u53f7\u6ce2\u5f62\uff0c\u68c0\u67e5\u4fe1\u53f7\u8d28\u91cf\u3002\u66f4\u591a Modbus \u901a\u4fe1\u8c03\u8bd5\u6280\u5de7\u8bf7\u53c2\u8003 <a href=\"https:\/\/modbus.cn\" rel=\"noopener noreferrer nofollow\">modbus.cn<\/a> \u4e0a\u7684\u8c03\u8bd5\u4e13\u9898\u6587\u7ae0\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5341\u4e09\u3001\u603b\u7ed3<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">CRC\/LRC \u6821\u9a8c\u662f Modbus \u901a\u4fe1\u7684&#8221;\u5b88\u95e8\u5458&#8221;\uff0c\u4fdd\u969c\u7740\u5de5\u4e1a\u6570\u636e\u7684\u5b8c\u6574\u6027\u3002\u672c\u6587\u4ece\u6570\u5b66\u539f\u7406\u5230\u4ee3\u7801\u5b9e\u6218\uff0c\u6db5\u76d6\u4e86 Modbus \u6821\u9a8c\u673a\u5236\u7684\u65b9\u65b9\u9762\u9762\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>CRC-16\uff08RTU \u6a21\u5f0f\uff09\uff1a<\/strong>\u57fa\u4e8e\u591a\u9879\u5f0f 0xA001 \u7684\u5faa\u73af\u5197\u4f59\u6821\u9a8c\uff0c\u68c0\u6d4b\u80fd\u529b\u5f3a\uff0c\u662f Modbus \u901a\u4fe1\u7684\u4e3b\u6d41\u6821\u9a8c\u65b9\u5f0f<\/li>\n<li><strong>LRC\uff08ASCII \u6a21\u5f0f\uff09\uff1a<\/strong>\u57fa\u4e8e\u7d2f\u52a0\u53d6\u8865\u7684\u7eb5\u5411\u5197\u4f59\u6821\u9a8c\uff0c\u5b9e\u73b0\u7b80\u5355\u4f46\u68c0\u6d4b\u80fd\u529b\u8f83\u5f31<\/li>\n<li><strong>\u67e5\u8868\u6cd5\uff1a<\/strong>\u4ee5 512 \u5b57\u8282 ROM \u5f00\u9500\u6362\u53d6 8 \u500d\u901f\u5ea6\u63d0\u5347\uff0c\u662f\u751f\u4ea7\u73af\u5883\u9996\u9009<\/li>\n<li><strong>\u4f4d\u79fb\u6cd5\uff1a<\/strong>\u4ee3\u7801\u7cbe\u7b80\uff0c\u9002\u5408\u5b66\u4e60\u548c\u8d44\u6e90\u6781\u7aef\u53d7\u9650\u7684\u573a\u666f<\/li>\n<li><strong>\u786c\u4ef6\u52a0\u901f\uff1a<\/strong>\u73b0\u4ee3 MCU \u548c FPGA \u53ef\u8fdb\u4e00\u6b65\u5c06 CRC \u8ba1\u7b97\u6548\u7387\u63d0\u5347\u4e00\u4e2a\u6570\u91cf\u7ea7<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f5c\u4e3a\u4e00\u540d\u5d4c\u5165\u5f0f\u5de5\u7a0b\u5e08\u6216\u81ea\u52a8\u5316\u5de5\u7a0b\u5e08\uff0c\u7406\u89e3 CRC \u539f\u7406\u5e76\u638c\u63e1\u5176\u7f16\u7a0b\u5b9e\u73b0\u662f\u57fa\u672c\u529f\u3002\u5efa\u8bae\u5c06\u672c\u6587\u7684\u6d4b\u8bd5\u5411\u91cf\u548c\u4ee3\u7801\u4fdd\u5b58\u4e3a\u53c2\u8003\uff0c\u5728\u6bcf\u6b21\u5b9e\u73b0\u65b0\u7684 Modbus \u901a\u4fe1\u65f6\u8fdb\u884c\u9a8c\u8bc1\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u66f4\u591a Modbus \u6df1\u5ea6\u6280\u672f\u6587\u7ae0\uff0c\u6b22\u8fce\u8bbf\u95ee <a href=\"https:\/\/modbus.cn\" rel=\"noopener noreferrer nofollow\">modbus.cn<\/a>\u3002\u63a8\u8350\u9605\u8bfb\uff1a<a href=\"https:\/\/modbus.cn\/modbus-protocol-comparison\" rel=\"noopener noreferrer nofollow\">Modbus \u4e0e\u4e3b\u6d41\u5de5\u4e1a\u534f\u8bae\u5bf9\u6bd4\u5206\u6790<\/a>\u3001<a href=\"https:\/\/modbus.cn\/modbus-function-codes\" rel=\"noopener noreferrer nofollow\">Modbus \u529f\u80fd\u7801\u5b8c\u5168\u6307\u5357<\/a>\u3001<a href=\"https:\/\/modbus.cn\/modbus-rtu-tcp-difference\" rel=\"noopener noreferrer nofollow\">Modbus RTU \u4e0e Modbus TCP \u7684\u6838\u5fc3\u533a\u522b<\/a>\u3002\u5982\u679c\u4f60\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u9047\u5230 CRC \u6821\u9a8c\u95ee\u9898\uff0c\u4e5f\u53ef\u5728 modbus.cn \u7684\u6280\u672f\u793e\u533a\u4e2d\u4e0e\u5176\u4ed6\u5de5\u7a0b\u5e08\u4ea4\u6d41\u8ba8\u8bba\uff0c\u83b7\u5f97\u66f4\u591a\u5b9e\u6218\u7ecf\u9a8c\u5206\u4eab\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<p class=\"wp-block-paragraph\"><em>\u672c\u6587\u7531 modbus.cn \u6280\u672f\u56e2\u961f\u539f\u521b\uff0c\u8f6c\u8f7d\u8bf7\u6ce8\u660e\u51fa\u5904\u3002\u6587\u4e2d\u7684\u4ee3\u7801\u793a\u4f8b\u5747\u5df2\u901a\u8fc7\u5b9e\u9645\u6d4b\u8bd5\u9a8c\u8bc1\u3002\u66f4\u65b0\u65e5\u671f\uff1a2026 \u5e74 6 \u6708\u3002<\/em><\/p>\n<span id=\"magicpostMarker\"><\/span>","protected":false},"excerpt":{"rendered":"<p>Modbus CRC\/LRC \u6821\u9a8c\u539f\u7406\u4e0e\u7f16\u7a0b\u5b9e\u73b0\uff1a\u4ece\u6570\u5b66\u63a8\u5bfc&#8230;<\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","_members_access_role":[],"_members_access_error":""},"categories":[1],"tags":[3081,3082,3080,3084,3083],"class_list":["post-44511","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-crc-16","tag-lrc","tag-modbus-crc","tag-modbus","tag-crc"],"_links":{"self":[{"href":"https:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/posts\/44511","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=44511"}],"version-history":[{"count":0,"href":"https:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/posts\/44511\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/media?parent=44511"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/categories?post=44511"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/tags?post=44511"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}