{"id":51495,"date":"2026-08-04T20:16:13","date_gmt":"2026-08-04T12:16:13","guid":{"rendered":"https:\/\/www.modbus.cn\/51495.html"},"modified":"2026-08-04T20:16:13","modified_gmt":"2026-08-04T12:16:13","slug":"visualhmi-%e5%af%84%e5%ad%98%e5%99%a8%e6%93%8d%e4%bd%9c%ef%bc%9aget-set-%e5%85%a8%e7%b3%bb%e5%88%97-api-%e4%b8%8e%e7%bc%93%e5%ad%98%e6%9c%ba%e5%88%b6%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"http:\/\/www.modbus.cn\/en\/51495.html","title":{"rendered":"VisualHMI \u5bc4\u5b58\u5668\u64cd\u4f5c\uff1aget\/set \u5168\u7cfb\u5217 API \u4e0e\u7f13\u5b58\u673a\u5236\u8be6\u89e3"},"content":{"rendered":"<p>\u5bc4\u5b58\u5668\u8bfb\u5199 API \u662f LUA \u548c\u6570\u636e\u6253\u4ea4\u9053\u7684\u6838\u5fc3\u3002\u4ee5 DCBUS \u4e3a\u4f8b\uff0cHMI \u6570\u636e\u7c7b\u578b\u5305\u62ec bit\u3001int16\/uint16\u3001int32\/uint32\u3001int64\/uint64\u3001float\u3001double\u3001string\uff0c\u8fd8\u6709\u6279\u91cf\u5199\u77ed\u6574\u578b\u3002\u8fd9\u7bc7\u628a\u8bfb\u5199 API \u5168\u90e8\u8bb2\u900f\u3002<\/p>\n<h2>1. \u5148\u641e\u61c2\uff1aget_xxx \u4e0d\u4ea7\u751f\u4e32\u53e3\u5e27<\/h2>\n<p>\u6240\u6709 <code>get_xxx()<\/code> \u51fd\u6570<strong>\u90fd\u4ece HMI \u672c\u5730\u7f13\u5b58\uff08\u5185\u5b58\u955c\u50cf\uff09\u8bfb\u53d6<\/strong>\uff0c\u4e0d\u53d1\u8d77\u4e32\u53e3\u8bf7\u6c42\u3002HMI \u540e\u53f0\u4efb\u52a1\u6309\u5de5\u7a0b\u914d\u7f6e\u7684\u8f6e\u8be2\u5468\u671f\uff0c\u81ea\u52a8\u4ece PLC \u8bfb\u53d6 VT_0x\/VT_1x\/VT_3x\/VT_4x \u7b49\u901a\u4fe1\u53d8\u91cf\u5e76\u7f13\u5b58\uff1b\u811a\u672c\u8c03\u7528 get_xxx \u76f4\u63a5\u8fd4\u56de\u7f13\u5b58\u503c\u3002<\/p>\n<p>\u8fd9\u610f\u5473\u7740<strong>\u811a\u672c\u5c42\u7684\u8bfb\u53d6\u662f\u96f6\u901a\u4fe1\u5f00\u9500\u7684<\/strong>\u2014\u2014\u4e0d\u4f1a\u56e0\u4e3a\u811a\u672c\u903b\u8f91\u5bfc\u81f4\u901a\u4fe1\u98ce\u66b4\u3002\u597d\u5904\u662f\u7cfb\u7edf\u5b9e\u65f6\u6027\u6709\u4fdd\u969c\uff0c\u4ee3\u4ef7\u662f\u8bfb\u5230\u7684\u662f&#8221;\u6700\u8fd1\u4e00\u6b21\u8f6e\u8be2&#8221;\u7684\u503c\uff0c\u4e0d\u662f\u7edd\u5bf9\u5b9e\u65f6\u3002<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.modbus.cn\/wp-content\/uploads\/2026\/08\/visualhmi\/lu002_1.gif\" style=\"max-width:100%;border:1px solid #eee;margin:8px 0\" loading=\"lazy\" title=\"VisualHMI \u5bc4\u5b58\u5668\u64cd\u4f5c\uff1aget\/set \u5168\u7cfb\u5217 API \u4e0e\u7f13\u5b58\u673a\u5236\u8be6\u89e3\u63d2\u56fe\" alt=\"VisualHMI \u5bc4\u5b58\u5668\u64cd\u4f5c\uff1aget\/set \u5168\u7cfb\u5217 API \u4e0e\u7f13\u5b58\u673a\u5236\u8be6\u89e3\u63d2\u56fe\" \/><\/p>\n<h2>2. \u4f4d\u64cd\u4f5c\uff1aset_bit \/ get_bit<\/h2>\n<pre style=\"background:#f5f5f5;padding:10px;border-radius:4px\">set_bit(vtype, addr, value, count)  -- \u6279\u91cf\u5199\u4f4d, count=1~16\nget_bit(vtype, addr)                -- \u8bfb\u5355\u4e00\u4f4d, \u8fd4\u56de0\/1<\/pre>\n<p><code>set_bit<\/code> \u7684 value \u662f 16 \u4f4d\u65e0\u7b26\u53f7\u503c\uff0c\u5176 bit0 \u5bf9\u5e94 addr\u3001bit1 \u5bf9\u5e94 addr+1\u2026\u2026count \u6307\u5b9a\u5199\u591a\u5c11\u4f4d\uff081~16\uff09\u3002\u9002\u5408 Modbus \u7ebf\u5708\u3001FX3U \u7684 X\/Y\/S\/M \u4f4d\u533a\u3002<\/p>\n<p>\u793a\u4f8b\uff1a\u70b9\u51fb\u7ebf\u5708 0x0000\uff0c\u81ea\u52a8\u628a\u72b6\u6001\u590d\u5236\u5230\u7ebf\u5708 0x0001\uff1a<\/p>\n<pre style=\"background:#f5f5f5;padding:10px;border-radius:4px\">function on_update(slave, vtype, addr)\n  if vtype == VT_0x then\n    if addr == 0x0000 then\n      set_bit(VT_0x, 0x0001, get_bit(VT_0x, 0x0000))\n    end\n  end\nend<\/pre>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.modbus.cn\/wp-content\/uploads\/2026\/08\/visualhmi\/lu002_2.gif\" style=\"max-width:100%;border:1px solid #eee;margin:8px 0\" loading=\"lazy\" title=\"VisualHMI \u5bc4\u5b58\u5668\u64cd\u4f5c\uff1aget\/set \u5168\u7cfb\u5217 API \u4e0e\u7f13\u5b58\u673a\u5236\u8be6\u89e3\u63d2\u56fe1\" alt=\"VisualHMI \u5bc4\u5b58\u5668\u64cd\u4f5c\uff1aget\/set \u5168\u7cfb\u5217 API \u4e0e\u7f13\u5b58\u673a\u5236\u8be6\u89e3\u63d2\u56fe1\" \/><\/p>\n<h2>3. 16 \u4f4d\u6574\u578b\uff1aset\/get_uint16\u3001set\/get_int16<\/h2>\n<pre style=\"background:#f5f5f5;padding:10px;border-radius:4px\">set_uint16(vtype, addr, value)  -- 0~65535, \u8d85\u51fa\u81ea\u52a8 value &amp; 0xFFFF\nget_uint16(vtype, addr)         -- \u8bfb16\u4f4d\u65e0\u7b26\u53f7\nset_int16(vtype, addr, value)   -- -32768~+32767, \u8d85\u51fa\u81ea\u52a8 value &amp; 0xFFFF\nget_int16(vtype, addr)          -- \u8bfb16\u4f4d\u6709\u7b26\u53f7<\/pre>\n<p>uint16 \u5c31\u662f Modbus \u4fdd\u6301\u5bc4\u5b58\u5668\u3001FX3U D \u5bc4\u5b58\u5668\u3001HMI \u5185\u90e8\u53d8\u91cf\uff08LW\uff09\u7684\u57fa\u672c\u5355\u4f4d\u3002\u6ce8\u610f\uff1a<strong>int16 \u5199\u5165\u8d1f\u503c\u65f6\u5b58\u7684\u662f\u8865\u7801<\/strong>\uff0c\u8bfb\u56de\u7528 get_int16 \u624d\u8fd8\u539f\u6210\u8d1f\u6570\u3002<\/p>\n<h2>4. 32 \u4f4d\uff1aset\/get_uint32\u3001set\/get_int32<\/h2>\n<pre style=\"background:#f5f5f5;padding:10px;border-radius:4px\">set_uint32(vtype, addr, value)  -- 0~4294967295, \u8d85\u51fa &amp; 0xFFFFFFFF, \u5730\u5740\u8de8\u5ea6+2\nget_uint32(vtype, addr)\nset_int32(vtype, addr, value)   -- -2^31 ~ 2^31-1, \u5730\u5740\u8de8\u5ea6+2\nget_int32(vtype, addr)<\/pre>\n<p>32 \u4f4d\u5360<strong>\u8fde\u7eed 2 \u4e2a\u5bc4\u5b58\u5668<\/strong>\u3002\u4f4e\u5730\u5740\u662f\u4f4e\u4f4d\u8fd8\u662f\u9ad8\u4f4d\u53d6\u51b3\u4e8e <code>set_endian()<\/code> \u8bbe\u7f6e\uff08\u9ed8\u8ba4\u5c0f\u7aef\uff0c\u5373\u4f4e\u5730\u5740\u5b58\u4f4e\u5b57\u8282\uff09\u3002<\/p>\n<h2>5. 64 \u4f4d\uff1aset\/get_uint64\u3001set\/get_int64<\/h2>\n<pre style=\"background:#f5f5f5;padding:10px;border-radius:4px\">set_uint64(vtype, addr, value)  -- 0~2^64-1, \u5730\u5740\u8de8\u5ea6+4\nget_uint64(vtype, addr)\nset_int64(vtype, addr, value)   -- -2^63 ~ 2^63-1, \u5730\u5740\u8de8\u5ea6+4\nget_int64(vtype, addr)<\/pre>\n<p>64 \u4f4d\u5360\u8fde\u7eed 4 \u4e2a\u5bc4\u5b58\u5668\u3002\u7535\u8868\u3001\u6d41\u91cf\u8ba1\u8fd9\u7c7b\u5927\u6570\u573a\u666f\u7528\u3002<\/p>\n<h2>6. \u6d6e\u70b9\uff1aset\/get_float\u3001set\/get_double<\/h2>\n<pre style=\"background:#f5f5f5;padding:10px;border-radius:4px\">set_float(vtype, addr, value)   -- IEEE 754 \u5355\u7cbe\u5ea6, \u5730\u5740\u8de8\u5ea6+2\nget_float(vtype, addr)\nset_double(vtype, addr, value)  -- IEEE 754 \u53cc\u7cbe\u5ea6, \u5730\u5740\u8de8\u5ea6+4\nget_double(vtype, addr)<\/pre>\n<p>\u6d6e\u70b9\u6309 <strong>IEEE 754<\/strong> \u5b58\u50a8\u3002\u4f8b\uff1aget_float(VT_4x, 0x0064) \u8bfb\u4fdd\u6301\u5bc4\u5b58\u5668 0x0064~0x0065 \u7ec4\u6210\u7684\u5355\u7cbe\u5ea6\u503c\u3002<\/p>\n<h2>7. \u5b57\u7b26\u4e32\uff1aset\/get_string<\/h2>\n<pre style=\"background:#f5f5f5;padding:10px;border-radius:4px\">set_string(vtype, addr, str)          -- \u6700\u59272KB, UTF-8\u6216GBK\nget_string(vtype, addr, len)          -- len\u9ed8\u8ba4128, \u6700\u59272048, \u9047\u622a\u65ad<\/pre>\n<p>\u5b57\u7b26\u4e32\u5360\u8fde\u7eed\u5bc4\u5b58\u5668\uff08\u6bcf\u5bc4\u5b58\u5668 2 \u5b57\u8282\uff09\uff0c\u5730\u5740\u8de8\u5ea6 = \u5b57\u8282\u6570\/2 \u5411\u4e0a\u53d6\u6574\u3002\u5b58\u4e2d\u6587\u6ce8\u610f\u7f16\u7801\u8981\u548c PLC \u7ea6\u5b9a\u4e00\u81f4\u3002<\/p>\n<h2>8. \u6279\u91cf\u5199\uff1aset_uint16_ex \u4e0e set_array<\/h2>\n<pre style=\"background:#f5f5f5;padding:10px;border-radius:4px\">set_uint16_ex(vtype, addr, v1, v2, ..., vN)  -- \u6700\u591a120\u4e2a\u5bc4\u5b58\u5668, \u53ef\u53d8\u53c2\u6570\nset_array(vtype, addr, buff)                 -- buff\u662fLua table, \u6700\u591a120\u5b57<\/pre>\n<p>\u4e24\u8005\u90fd\u80fd\u6279\u91cf\u5199\uff0c\u533a\u522b\uff1a<code>set_uint16_ex<\/code> \u53c2\u6570\u76f4\u63a5\u5217\u5728\u51fd\u6570\u91cc\uff0c\u9002\u5408\u56fa\u5b9a\u4e2a\u6570\uff1b<code>set_array<\/code> \u4f20 Lua \u8868\uff0c\u9002\u5408\u7a0b\u5e8f\u5316\u751f\u6210\u3001\u52a8\u6001\u6570\u636e\u3002\u6279\u91cf\u5199\u80fd\u663e\u8457\u51cf\u5c11\u901a\u4fe1\u5e27\u6570\u3002<\/p>\n<h2>9. \u5b8c\u6574\u793a\u4f8b\uff1a12 \u7ec4\u6570\u636e\u7c7b\u578b\u590d\u5236<\/h2>\n<p>\u5b98\u65b9\u4f8b\u7a0b\u5728 on_update \u91cc\u6309\u7c7b\u578b\u628a\u6e90\u5730\u5740\u590d\u5236\u5230\u76ee\u6807\u5730\u5740\uff08vtype \u5224\u65ad + addr \u7cbe\u786e\u5339\u914d\uff09\uff1a<\/p>\n<pre style=\"background:#f5f5f5;padding:10px;border-radius:4px\">function on_update(slave, vtype, addr)\n  if vtype ~= VT_LW then return end\n  if addr == 0x1000 then set_uint16(VT_LW, 0x1001, get_uint16(VT_LW, 0x1000)) end\n  if addr == 0x1002 then set_int16(VT_LW, 0x1003, get_int16(VT_LW, 0x1002)) end\n  if addr == 0x1004 then set_uint32(VT_LW, 0x1006, get_uint32(VT_LW, 0x1004)) end\n  if addr == 0x1008 then set_int32(VT_LW, 0x100A, get_int32(VT_LW, 0x1008)) end\n  if addr == 0x100C then set_uint64(VT_LW, 0x1010, get_uint64(VT_LW, 0x100C)) end\n  if addr == 0x1014 then set_int64(VT_LW, 0x1018, get_int64(VT_LW, 0x1014)) end\n  if addr == 0x101C then set_float(VT_LW, 0x101E, get_float(VT_LW, 0x101C)) end\n  if addr == 0x1020 then set_double(VT_LW, 0x1024, get_double(VT_LW, 0x1020)) end\n  if addr == 0x1030 then set_string(VT_LW, 0x1040, get_string(VT_LW, 0x1030)) end\n  -- \u6279\u91cf\u5199: 0x1050\u4e3a\u5076\u6570\u65f6, \u519910\u4e2a\u5bc4\u5b58\u5668\n  if addr == 0x1050 then\n    local v = get_uint16(VT_LW, 0x1050)\n    if v % 2 == 0 then\n      set_uint16_ex(VT_LW, 0x1060, 1,2,3,4,5,6,7,8,9,10)\n    end\n  end\n  -- set_array \u5199\u6cd5: \u519910,20,...,90,100\n  if addr == 0x1060 then\n    local t = {}\n    for i=1,10 do t[i] = i*10 end\n    set_array(VT_LW, 0x1070, t)\n  end\nend<\/pre>\n<div class=\"tip\" style=\"border-left:4px solid #f7941e;padding:10px 14px;background:#fff8ef;margin:14px 0\">\u5bc4\u5b58\u5668\u7c7b\u578b\u9009\u9519\u662f\u6700\u5e38\u89c1\u7684 bug\uff1a<strong>PLC \u662f INT16 \u4f60\u6309 UINT16 \u8bfb\uff0c\u8d1f\u503c\u4f1a\u53d8\u6210 65535 \u7684\u5927\u6570\uff1b32 \u4f4d\u4ee5\u4e0a\u5fd8\u914d set_endian\uff0c\u9ad8\u4f4e\u5b57\u98a0\u5012<\/strong>\u3002\u5148\u786e\u8ba4\u6570\u636e\u7c7b\u578b\u548c\u5b57\u8282\u5e8f\u518d\u5199\u4ee3\u7801\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\u5bc4\u5b58\u5668\u64cd\u4f5c\u300d\uff0c\u7248\u6743\u5f52\u5927\u5f69\u79d1\u6280\u6240\u6709\u3002<\/p>\n<span id=\"magicpostMarker\"><\/span>","protected":false},"excerpt":{"rendered":"<p>\u5bc4\u5b58\u5668\u8bfb\u5199 API \u662f LUA \u548c\u6570\u636e\u6253\u4ea4\u9053\u7684\u6838\u5fc3\u3002\u4ee5 DC&#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":[3318,3260,3317,3134,3319,1714,3316],"class_list":["post-51495","post","type-post","status-publish","format-standard","hentry","category-hmi-lua","category-hmi-dev","tag-get_bit","tag-set_array","tag-set_uint16","tag-visualhmi","tag-vt_4x","tag-1714","tag-3316"],"_links":{"self":[{"href":"http:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/posts\/51495","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=51495"}],"version-history":[{"count":0,"href":"http:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/posts\/51495\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/media?parent=51495"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/categories?post=51495"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/tags?post=51495"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}