"# VisualHMI \u7cfb\u7edf\u56de\u8c03\u51fd\u6570\uff1aon_init\/on_run\/on_press\/on_update \u5168\u89e3\n\n> 2026-08-04 | 2026-08-05\n> http:\/\/www.modbus.cn\/en\/51494.html\n\n**LUA \u811a\u672c, \u4e32\u53e3\u5c4f\/HMI \u5f00\u53d1**\n\n---\n\n\u7cfb\u7edf\u56de\u8c03\u662f LUA \u548c HMI \u5185\u6838\u4e4b\u95f4\u7684**\u4e8b\u4ef6\u63a5\u53e3**\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\n\n## 1. \u56de\u8c03\u603b\u8868\n\n\u56de\u8c03\u89e6\u53d1\u65f6\u673a\u7528\u9014`on_init()`\u4e0a\u7535\u52a0\u8f7d\u540e\u6267\u884c\u4e00\u6b21dofile \u52a0\u8f7d\u6a21\u5757\u3001\u521d\u59cb\u5316\u53c2\u6570\/PLC\/\u9a71\u52a8\u677f`on_run(screen)`\u5468\u671f\u6267\u884c\uff08set_run_cycle \u8bbe\u5b9a\u5468\u671f\uff09\u6838\u5fc3\u8c03\u5ea6\u5165\u53e3\uff0c\u5468\u671f\u6027\u4efb\u52a1`on_press(state,x,y)`\u89e6\u6478\u4e8b\u4ef6\uff0c100ms \u6700\u591a\u4e00\u6b21\u6309\u952e\u5904\u7406\u3001\u7a7a\u95f2\u5f85\u673a`on_update(slave,vtype,addr)`\u53d8\u91cf\u503c\u53d8\u5316\u6570\u636e\u9a71\u52a8\u903b\u8f91\uff08\u63a8\u8350\u4f18\u5148\u7528\uff09`on_screen_change(screen)`\u753b\u9762\u5207\u6362\u540e\u753b\u9762\u521d\u59cb\u5316\u3001\u72b6\u6001\u540c\u6b65\n\n## 2. on_run\uff1a\u5468\u671f\u8c03\u5ea6\uff08\u6ce8\u610f\u522b\u5361\u6b7b\uff09\n\non_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\n\n- **Modbus RTU Master \u9ad8\u9891\u5199\u5165\u6613\u603b\u7ebf\u5835\u585e**\u2014\u2014on_run \u91cc\u522b\u6bcf\u79d2\u72c2\u53d1\u5199\u5e27- **\u4e0d\u53ef\u7528 delay_ms\u3001\u957f\u5faa\u73af\u3001\u7f51\u7edc\u540c\u6b65**\u2014\u2014\u963b\u585e\u4e3b\u5faa\u73af\uff0c\u753b\u9762\u5168\u5361- **\u4e25\u7981 set_run_cycle \u6b7b\u9501**\u2014\u2014\u56de\u8c03\u91cc\u6539\u5468\u671f\u53ef\u80fd\u9020\u6210\u6c38\u4e0d\u89e6\u53d1\n```\nfunction 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\n```\n\n## 3. on_press\uff1a\u89e6\u6478\u56de\u8c03\u4e0e\u7a7a\u95f2\u5f85\u673a\n\non_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**\u7a7a\u95f2\u5f85\u673a\u8282\u80fd**\uff1a\u4e00\u6bb5\u65f6\u95f4\u6ca1\u89e6\u6478\u81ea\u52a8\u7184\u5c4f\/\u8df3\u4e3b\u9875\u3002\n\n```\n_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\n```\n\n## 4. on_update\uff1a\u6570\u636e\u9a71\u52a8\uff08\u4f18\u5148\u7528\u8fd9\u4e2a\uff09\n\non_update(slave, vtype, addr) \u5728\u53d8\u91cf\u503c\u53d8\u5316\u65f6\u89e6\u53d1\uff0c\u662f**\u6570\u636e\u9a71\u52a8**\u6a21\u5f0f\u2014\u2014\u6bd4 on_run \u8f6e\u8be2\u7701\u8d44\u6e90\u3002\u4e09\u4e2a\u7279\u6027\u5fc5\u987b\u77e5\u9053\uff1a\n\n- **\u5916\u90e8\u4e32\u53e3\/Modbus \u4e3b\u7ad9\u5199\u5165\u4e0d\u89e6\u53d1**\u2014\u2014\u53ea\u5bf9 HMI \u5185\u90e8\u53d8\u91cf\u53d8\u5316\u751f\u6548- **on_update \u5185\u8c03 set_xxx \u88ab\u5c4f\u853d**\u2014\u2014\u9632\u65e0\u9650\u9012\u5f52\uff0c\u6539\u503c\u4e0d\u4f1a\u518d\u6b21\u89e6\u53d1- **\u5168\u5c40\u53d8\u91cf _EN_ON_UPDATA_API_ \u63a7\u5236\u5f00\u5173**\u2014\u2014\u7f6e 0 \u53ef\u4e34\u65f6\u7981\u7528\uff0c\u6279\u91cf\u521d\u59cb\u5316\u65f6\u7528\n```\n_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\n```\n\n## 5. on_screen_change\uff1a\u753b\u9762\u5207\u6362\u540e\u521d\u59cb\u5316\n\n\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\n\n\u56db\u4e2a\u5751\uff1a**\u2460on_run \u91cc\u4e25\u7981 delay_ms\/\u957f\u5faa\u73af**\uff0c\u4f1a\u5361\u6b7b\u4e3b\u5faa\u73af\uff1b**\u2461Modbus \u4e3b\u7ad9\u522b\u5728 on_run \u9ad8\u9891\u5199**\uff0c\u603b\u7ebf\u5835\u585e\u6ca1\u5546\u91cf\uff1b**\u2462on_update \u91cc set_xxx \u4e0d\u4f1a\u518d\u6b21\u89e6\u53d1\u56de\u8c03**\uff0c\u522b\u6307\u671b\u94fe\u5f0f\u89e6\u53d1\uff1b**\u2463\u6279\u91cf\u521d\u59cb\u5316\u5bc4\u5b58\u5668\u524d\u628a _EN_ON_UPDATA_API_ \u7f6e 0**\uff0c\u9632\u56de\u8c03\u98ce\u66b4\u3002\n\n\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\n\n---\n*modbus.cn*\n"