{"id":51494,"date":"2026-08-04T20:12:54","date_gmt":"2026-08-04T12:12:54","guid":{"rendered":"https:\/\/www.modbus.cn\/51494.html"},"modified":"2026-08-05T08:21:36","modified_gmt":"2026-08-05T00:21:36","slug":"visualhmi-%e7%b3%bb%e7%bb%9f%e5%9b%9e%e8%b0%83%e5%87%bd%e6%95%b0%ef%bc%9aon_init-on_run-on_press-on_update-%e5%85%a8%e8%a7%a3","status":"publish","type":"post","link":"http:\/\/www.modbus.cn\/en\/51494.html","title":{"rendered":"VisualHMI \u7cfb\u7edf\u56de\u8c03\u51fd\u6570\uff1aon_init\/on_run\/on_press\/on_update \u5168\u89e3"},"content":{"rendered":"<p>\u7cfb\u7edf\u56de\u8c03\u662f LUA \u548c HMI \u5185\u6838\u4e4b\u95f4\u7684<strong>\u4e8b\u4ef6\u63a5\u53e3<\/strong>\uff1a\u5185\u6838\u5728\u7279\u5b9a\u65f6\u673a\u81ea\u52a8\u8c03\u4f60\u7684\u51fd\u6570\uff0c\u4f60\u4e0d\u7528\u8f6e\u8be2\u3001\u4e0d\u7528\u81ea\u5df1\u89e6\u53d1\u3002\u641e\u61c2\u6bcf\u4e2a\u56de\u8c03\u7684\u89e6\u53d1\u65f6\u673a\u548c\u9650\u5236\uff0c\u662f\u5199\u51fa\u7a33\u5b9a\u811a\u672c\u7684\u524d\u63d0\u3002<\/p>\n<h2>1. \u56de\u8c03\u603b\u8868<\/h2>\n<table border=\"1\" cellpadding=\"6\" style=\"border-collapse:collapse;font-size:14px\">\n<tr>\n<th>\u56de\u8c03<\/th>\n<th>\u89e6\u53d1\u65f6\u673a<\/th>\n<th>\u7528\u9014<\/th>\n<\/tr>\n<tr>\n<td><code>on_init()<\/code><\/td>\n<td>\u4e0a\u7535\u52a0\u8f7d\u540e\u6267\u884c\u4e00\u6b21<\/td>\n<td>dofile \u52a0\u8f7d\u6a21\u5757\u3001\u521d\u59cb\u5316\u53c2\u6570\/PLC\/\u9a71\u52a8\u677f<\/td>\n<\/tr>\n<tr>\n<td><code>on_run(screen)<\/code><\/td>\n<td>\u5468\u671f\u6267\u884c\uff08set_run_cycle \u8bbe\u5b9a\u5468\u671f\uff09<\/td>\n<td>\u6838\u5fc3\u8c03\u5ea6\u5165\u53e3\uff0c\u5468\u671f\u6027\u4efb\u52a1<\/td>\n<\/tr>\n<tr>\n<td><code>on_press(state,x,y)<\/code><\/td>\n<td>\u89e6\u6478\u4e8b\u4ef6\uff0c100ms \u6700\u591a\u4e00\u6b21<\/td>\n<td>\u6309\u952e\u5904\u7406\u3001\u7a7a\u95f2\u5f85\u673a<\/td>\n<\/tr>\n<tr>\n<td><code>on_update(slave,vtype,addr)<\/code><\/td>\n<td>\u53d8\u91cf\u503c\u53d8\u5316<\/td>\n<td>\u6570\u636e\u9a71\u52a8\u903b\u8f91\uff08\u63a8\u8350\u4f18\u5148\u7528\uff09<\/td>\n<\/tr>\n<tr>\n<td><code>on_screen_change(screen)<\/code><\/td>\n<td>\u753b\u9762\u5207\u6362\u540e<\/td>\n<td>\u753b\u9762\u521d\u59cb\u5316\u3001\u72b6\u6001\u540c\u6b65<\/td>\n<\/tr>\n<\/table>\n<h2>2. on_run\uff1a\u5468\u671f\u8c03\u5ea6\uff08\u6ce8\u610f\u522b\u5361\u6b7b\uff09<\/h2>\n<p>on_run \u662f\u5468\u671f\u56de\u8c03\uff0c\u91cc\u9762\u5e72\u6d3b\u7684\u65f6\u957f\u5fc5\u987b\u8fdc\u5c0f\u4e8e\u5468\u671f\u3002\u4e09\u4e2a\u7981\u5fcc\uff1a<\/p>\n<ul>\n<li><strong>Modbus RTU Master \u9ad8\u9891\u5199\u5165\u6613\u603b\u7ebf\u5835\u585e<\/strong>\u2014\u2014on_run \u91cc\u522b\u6bcf\u79d2\u72c2\u53d1\u5199\u5e27<\/li>\n<li><strong>\u4e0d\u53ef\u7528 delay_ms\u3001\u957f\u5faa\u73af\u3001\u7f51\u7edc\u540c\u6b65<\/strong>\u2014\u2014\u963b\u585e\u4e3b\u5faa\u73af\uff0c\u753b\u9762\u5168\u5361<\/li>\n<li><strong>\u4e25\u7981 set_run_cycle \u6b7b\u9501<\/strong>\u2014\u2014\u56de\u8c03\u91cc\u6539\u5468\u671f\u53ef\u80fd\u9020\u6210\u6c38\u4e0d\u89e6\u53d1<\/li>\n<\/ul>\n<pre style=\"background:#f5f5f5;padding:10px;border-radius:4px\">function on_init()\n  set_run_cycle(1000)     -- \u8bbe\u5b9a 1 \u79d2\u5468\u671f\nend\nfunction on_run(screen)\n  -- \u6bcf\u79d2\u505a\u4e00\u6b21\u7684\u4e8b\u60c5\uff1a\u72b6\u6001\u8f6e\u8be2\u3001\u8d85\u65f6\u5224\u65ad\nend<\/pre>\n<h2>3. on_press\uff1a\u89e6\u6478\u56de\u8c03\u4e0e\u7a7a\u95f2\u5f85\u673a<\/h2>\n<p>on_press(state, x, y)\uff0cstate\uff1a0=\u62ac\u8d77\u30011=\u6309\u4e0b\u30012=\u957f\u6309\u3002\u6bcf 100ms \u6700\u591a\u89e6\u53d1\u4e00\u6b21\u3002\u5178\u578b\u7528\u9014\u662f<strong>\u7a7a\u95f2\u5f85\u673a\u8282\u80fd<\/strong>\uff1a\u4e00\u6bb5\u65f6\u95f4\u6ca1\u89e6\u6478\u81ea\u52a8\u7184\u5c4f\/\u8df3\u4e3b\u9875\u3002<\/p>\n<pre style=\"background:#f5f5f5;padding:10px;border-radius:4px\">_IDLE_TIMEOUT = 300000   -- 5\u5206\u949f\u65e0\u64cd\u4f5c\n_idle_tick = 0\nfunction on_press(state, x, y)\n  _idle_tick = 0          -- \u6709\u89e6\u6478\u5c31\u91cd\u7f6e\u8ba1\u65f6\nend\nfunction on_run(screen)\n  _idle_tick = _idle_tick + 1000   -- \u5047\u8bbe\u5468\u671f1s\n  if _idle_tick &gt;= _IDLE_TIMEOUT then\n    set_screen(0)         -- \u8df3\u56de\u4e3b\u753b\u9762\n    _idle_tick = 0\n  end\nend<\/pre>\n<h2>4. on_update\uff1a\u6570\u636e\u9a71\u52a8\uff08\u4f18\u5148\u7528\u8fd9\u4e2a\uff09<\/h2>\n<p>on_update(slave, vtype, addr) \u5728\u53d8\u91cf\u503c\u53d8\u5316\u65f6\u89e6\u53d1\uff0c\u662f<strong>\u6570\u636e\u9a71\u52a8<\/strong>\u6a21\u5f0f\u2014\u2014\u6bd4 on_run \u8f6e\u8be2\u7701\u8d44\u6e90\u3002\u4e09\u4e2a\u7279\u6027\u5fc5\u987b\u77e5\u9053\uff1a<\/p>\n<ul>\n<li><strong>\u5916\u90e8\u4e32\u53e3\/Modbus \u4e3b\u7ad9\u5199\u5165\u4e0d\u89e6\u53d1<\/strong>\u2014\u2014\u53ea\u5bf9 HMI \u5185\u90e8\u53d8\u91cf\u53d8\u5316\u751f\u6548<\/li>\n<li><strong>on_update \u5185\u8c03 set_xxx \u88ab\u5c4f\u853d<\/strong>\u2014\u2014\u9632\u65e0\u9650\u9012\u5f52\uff0c\u6539\u503c\u4e0d\u4f1a\u518d\u6b21\u89e6\u53d1<\/li>\n<li><strong>\u5168\u5c40\u53d8\u91cf _EN_ON_UPDATA_API_ \u63a7\u5236\u5f00\u5173<\/strong>\u2014\u2014\u7f6e 0 \u53ef\u4e34\u65f6\u7981\u7528\uff0c\u6279\u91cf\u521d\u59cb\u5316\u65f6\u7528<\/li>\n<\/ul>\n<pre style=\"background:#f5f5f5;padding:10px;border-radius:4px\">_EN_ON_UPDATA_API_ = 0    -- \u521d\u59cb\u5316\u671f\u95f4\u5173\u56de\u8c03\n-- ... \u6279\u91cf\u5199\u5bc4\u5b58\u5668 ...\n_EN_ON_UPDATA_API_ = 1    -- \u5f00\u56de\u56de\u8c03\n\nfunction on_update(slave, vtype, addr)\n  if vtype == VT_LW then\n    if addr == 0x1000 then\n      -- LW1000 \u53d8\u5316\u4e86\uff0c\u5904\u7406\u4e1a\u52a1\n    end\n  end\nend<\/pre>\n<h2>5. on_screen_change\uff1a\u753b\u9762\u5207\u6362\u540e\u521d\u59cb\u5316<\/h2>\n<p>\u753b\u9762\u5207\u6362\u5b8c\u6210\u540e\u89e6\u53d1\uff0c\u505a\u8be5\u753b\u9762\u7684\u521d\u59cb\u5316\u3001\u72b6\u6001\u540c\u6b65\u3001\u8d44\u6e90\u52a0\u8f7d\u2014\u2014\u6bd4\u5728\u5207\u5c4f\u524d\u5904\u7406\u66f4\u7a33\u3002<\/p>\n<div class=\"tip\" style=\"border-left:4px solid #f7941e;padding:10px 14px;background:#fff8ef;margin:14px 0\">\u56db\u4e2a\u5751\uff1a<strong>\u2460on_run \u91cc\u4e25\u7981 delay_ms\/\u957f\u5faa\u73af<\/strong>\uff0c\u4f1a\u5361\u6b7b\u4e3b\u5faa\u73af\uff1b<strong>\u2461Modbus \u4e3b\u7ad9\u522b\u5728 on_run \u9ad8\u9891\u5199<\/strong>\uff0c\u603b\u7ebf\u5835\u585e\u6ca1\u5546\u91cf\uff1b<strong>\u2462on_update \u91cc set_xxx \u4e0d\u4f1a\u518d\u6b21\u89e6\u53d1\u56de\u8c03<\/strong>\uff0c\u522b\u6307\u671b\u94fe\u5f0f\u89e6\u53d1\uff1b<strong>\u2463\u6279\u91cf\u521d\u59cb\u5316\u5bc4\u5b58\u5668\u524d\u628a _EN_ON_UPDATA_API_ \u7f6e 0<\/strong>\uff0c\u9632\u56de\u8c03\u98ce\u66b4\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\u7cfb\u7edf\u56de\u8c03\u300d\uff0c\u7248\u6743\u5f52\u5927\u5f69\u79d1\u6280\u6240\u6709\u3002<\/p>\n<span id=\"magicpostMarker\"><\/span>","protected":false},"excerpt":{"rendered":"<p>\u7cfb\u7edf\u56de\u8c03\u662f LUA \u548c HMI \u5185\u6838\u4e4b\u95f4\u7684\u4e8b\u4ef6\u63a5\u53e3\uff1a\u5185\u6838\u5728\u7279&#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":[3311,3314,3312,3315,3313,3134,3310],"class_list":["post-51494","post","type-post","status-publish","format-standard","hentry","category-hmi-lua","category-hmi-dev","tag-on_init","tag-on_press","tag-on_run","tag-on_screen_change","tag-on_update","tag-visualhmi","tag-3310"],"_links":{"self":[{"href":"http:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/posts\/51494","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=51494"}],"version-history":[{"count":1,"href":"http:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/posts\/51494\/revisions"}],"predecessor-version":[{"id":51642,"href":"http:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/posts\/51494\/revisions\/51642"}],"wp:attachment":[{"href":"http:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/media?parent=51494"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/categories?post=51494"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/tags?post=51494"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}