{"id":51514,"date":"2026-08-04T20:26:54","date_gmt":"2026-08-04T12:26:54","guid":{"rendered":"https:\/\/www.modbus.cn\/51514.html"},"modified":"2026-08-04T20:26:54","modified_gmt":"2026-08-04T12:26:54","slug":"visualhmi-%e8%87%aa%e7%94%b1%e4%b8%b2%e5%8f%a3%e5%8d%8f%e8%ae%ae%ef%bc%88%e4%b8%bb%e5%8a%a8%ef%bc%89%ef%bc%9auart_recv-calc_crc16-%e4%b8%8e-xgus-%e6%98%a0%e5%b0%84","status":"publish","type":"post","link":"http:\/\/www.modbus.cn\/en\/51514.html","title":{"rendered":"VisualHMI \u81ea\u7531\u4e32\u53e3\u534f\u8bae\uff08\u4e3b\u52a8\uff09\uff1auart_recv\/calc_crc16 \u4e0e XGUS \u6620\u5c04"},"content":{"rendered":"<p>\u5728\u88ab\u52a8\u6a21\u5f0f\u57fa\u7840\u4e0a\u589e\u52a0\u4e3b\u52a8\u8bfb\u53d6\u80fd\u529b\uff1aHMI \u4e3b\u52a8\u5411\u8bbe\u5907\u53d1\u67e5\u8be2\u5e27\u5e76\u8bfb\u53d6\u54cd\u5e94\u3002\u9002\u5408 HMI \u505a\u4e3b\u8bbe\u5907\u7684\u79c1\u6709\u534f\u8bae\u5bf9\u63a5\u3002<\/p>\n<h2>1. \u65b0\u589e API<\/h2>\n<table border=\"1\" cellpadding=\"6\" style=\"border-collapse:collapse;font-size:14px\">\n<tr>\n<th>\u51fd\u6570<\/th>\n<th>\u8bf4\u660e<\/th>\n<\/tr>\n<tr>\n<td>uart_rxsize(ch)<\/td>\n<td>\u67e5\u8be2\u63a5\u6536\u7f13\u51b2\u5b57\u8282\u6570<\/td>\n<\/tr>\n<tr>\n<td>uart_recv(ch, size)<\/td>\n<td>\u4e3b\u52a8\u8bfb\u53d6\u6307\u5b9a\u5b57\u8282\u6570\uff08size\u22651\uff09\uff0c\u8fd4\u56de\u4e0b\u6807\u4ece 1 \u7684\u5b57\u8282\u8868<\/td>\n<\/tr>\n<tr>\n<td>uart_rxclear(ch)<\/td>\n<td>\u6e05\u7a7a\u63a5\u6536\u7f13\u51b2<\/td>\n<\/tr>\n<tr>\n<td>calc_crc16(data)<\/td>\n<td>Modbus CRC-16 \u8ba1\u7b97\uff08\u4f4e\u5b57\u8282\u5728\u524d\uff09<\/td>\n<\/tr>\n<\/table>\n<p>\u26a0\ufe0f \u4e3b\u52a8\u8bfb\u9700\u8981\u5de5\u7a0b\u5c5e\u6027\u91cc<strong>\u4e32\u53e3\u8bbe\u7f6e \u2192 \u63a5\u6536\u56de\u8c03 \u2192 \u7981\u7528<\/strong>\uff0c\u5426\u5219\u548c on_uart_recv \u51b2\u7a81\u3002<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.modbus.cn\/wp-content\/uploads\/2026\/08\/visualhmi\/lu018_1.png\" style=\"max-width:100%;border:1px solid #eee;margin:8px 0\" loading=\"lazy\" title=\"VisualHMI \u81ea\u7531\u4e32\u53e3\u534f\u8bae\uff08\u4e3b\u52a8\uff09\uff1auart_recv\/calc_crc16 \u4e0e XGUS \u6620\u5c04\u63d2\u56fe\" alt=\"VisualHMI \u81ea\u7531\u4e32\u53e3\u534f\u8bae\uff08\u4e3b\u52a8\uff09\uff1auart_recv\/calc_crc16 \u4e0e XGUS \u6620\u5c04\u63d2\u56fe\" \/><\/p>\n<h2>2. \u6848\u4f8b\uff1aXGUS \u534f\u8bae\u6620\u5c04<\/h2>\n<p><strong>\u5e27\u683c\u5f0f<\/strong>\uff1a5A A5 + \u957f\u5ea6 1B + \u6307\u4ee4 1B + \u6570\u636e + CRC\uff08\u53ef\u9009\uff09\u3002\u6307\u4ee4 0x82 \u5199\u30010x83 \u8bfb\u3002<\/p>\n<p>\u5b98\u65b9\u63d0\u4f9b\u57fa\u4e8e<strong>\u534f\u7a0b + \u5143\u8868<\/strong>\u7684\u975e\u963b\u585e\u6846\u67b6 XGUS.lua\uff1a<\/p>\n<pre style=\"background:#f5f5f5;padding:10px;border-radius:4px\">dofile(\"XGUS.lua\")\nxgus = XGUS:new(0, 0x55AA, true, true)\n\n-- \u8bfb\u4ece\u673a 0x55AA \u7684 0x0000 \u5730\u5740 10 \u4e2a\u5b57\nlocal data = xgus:read(0x0000, 10)\nif data then\n  set_uint16(VT_LW, 0x2000, data[1])\nend\n\n-- \u5199\u5730\u5740 0x0100\nxgus:write(0x0100, {0x12, 0x34})<\/pre>\n<p>\u534f\u7a0b\u6846\u67b6\u7684\u597d\u5904\uff1a\u811a\u672c\u5c42\u5199\u8d77\u6765\u50cf\u540c\u6b65\u8c03\u7528\uff0c\u5e95\u5c42\u81ea\u52a8\u5904\u7406&#8221;\u53d1\u8bf7\u6c42\u2192\u7b49\u54cd\u5e94\u2192\u8d85\u65f6\u91cd\u8bd5&#8221;\uff0c\u4e0d\u963b\u585e on_run \u4e3b\u5faa\u73af\u3002<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.modbus.cn\/wp-content\/uploads\/2026\/08\/visualhmi\/lu018_2.png\" style=\"max-width:100%;border:1px solid #eee;margin:8px 0\" loading=\"lazy\" title=\"VisualHMI \u81ea\u7531\u4e32\u53e3\u534f\u8bae\uff08\u4e3b\u52a8\uff09\uff1auart_recv\/calc_crc16 \u4e0e XGUS \u6620\u5c04\u63d2\u56fe1\" alt=\"VisualHMI \u81ea\u7531\u4e32\u53e3\u534f\u8bae\uff08\u4e3b\u52a8\uff09\uff1auart_recv\/calc_crc16 \u4e0e XGUS \u6620\u5c04\u63d2\u56fe1\" \/><\/p>\n<div class=\"tip\" style=\"border-left:4px solid #f7941e;padding:10px 14px;background:#fff8ef;margin:14px 0\">\u4e3b\u52a8\u6a21\u5f0f\u4e09\u6ce8\u610f\uff1a<strong>\u2460\u5fc5\u987b\u7981\u7528\u63a5\u6536\u56de\u8c03\uff08\u5de5\u7a0b\u4e32\u53e3\u8bbe\u7f6e\u91cc\uff09\uff0c\u5426\u5219 on_uart_recv \u548c uart_recv \u62a2\u6570\u636e\uff1b\u2461calc_crc16 \u662f Modbus CRC-16\uff0c\u4f4e\u5b57\u8282\u5728\u524d\uff1b\u2462\u4e3b\u52a8\u8bfb\u8981\u9632\u8bbe\u5907\u65e0\u54cd\u5e94\u2014\u2014\u6846\u67b6\u5185\u90e8\u6709\u8d85\u65f6\u673a\u5236\uff0c\u81ea\u5df1\u5199\u7684\u8bdd\u52a1\u5fc5\u52a0\u8d85\u65f6<\/strong>\u3002<\/div>\n<p style=\"color:#888;font-size:13px\">\u6574\u7406\u81ea \u5e7f\u5dde\u5927\u5f69\u79d1\u6280 VisualHMI \u5f00\u53d1\u6587\u6863\uff08hmi-doc.gz-dc.com\uff09LUA \u6559\u7a0b\u300c\u81ea\u7531\u4e32\u53e3\u534f\u8bae\uff08\u4e3b\u52a8\uff09\u300d\uff0c\u7248\u6743\u5f52\u5927\u5f69\u79d1\u6280\u6240\u6709\u3002<\/p>\n<span id=\"magicpostMarker\"><\/span>","protected":false},"excerpt":{"rendered":"<p>\u5728\u88ab\u52a8\u6a21\u5f0f\u57fa\u7840\u4e0a\u589e\u52a0\u4e3b\u52a8\u8bfb\u53d6\u80fd\u529b\uff1aHMI \u4e3b\u52a8\u5411\u8bbe\u5907\u53d1\u67e5\u8be2\u5e27&#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":[3131,3126],"tags":[3384,3383,3134,3167,3385,3377],"class_list":["post-51514","post","type-post","status-publish","format-standard","hentry","category-hmi-lua","category-hmi-dev","tag-calc_crc16","tag-uart_recv","tag-visualhmi","tag-xgus","tag-3385","tag-3377"],"_links":{"self":[{"href":"http:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/posts\/51514","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/comments?post=51514"}],"version-history":[{"count":0,"href":"http:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/posts\/51514\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/media?parent=51514"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/categories?post=51514"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/tags?post=51514"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}