{"id":51500,"date":"2026-08-04T20:18:16","date_gmt":"2026-08-04T12:18:16","guid":{"rendered":"https:\/\/www.modbus.cn\/51500.html"},"modified":"2026-08-05T08:25:27","modified_gmt":"2026-08-05T00:25:27","slug":"visualhmi-%e5%ae%9a%e6%97%b6%e5%99%a8%ef%bc%9astart_timer-on_timer-%e4%b8%8e%e5%80%92%e8%ae%a1%e6%97%b6%e5%ae%8c%e6%95%b4%e6%a1%88%e4%be%8b","status":"publish","type":"post","link":"http:\/\/www.modbus.cn\/en\/51500.html","title":{"rendered":"VisualHMI \u5b9a\u65f6\u5668\uff1astart_timer\/on_timer \u4e0e\u5012\u8ba1\u65f6\u5b8c\u6574\u6848\u4f8b"},"content":{"rendered":"<p>VisualHMI \u7684 Lua \u652f\u6301 <strong>32 \u4e2a\u8f6f\u4ef6\u5b9a\u65f6\u5668<\/strong>\uff08\u7d22\u5f15 0~31\uff09\u3002\u542f\u52a8\u540e\u8d85\u65f6\u89e6\u53d1 <code>on_timer(timer_id)<\/code> \u56de\u8c03\uff0c\u505a\u5468\u671f\u6027\u4efb\u52a1\u3001\u5ef6\u65f6\u64cd\u4f5c\u3001\u72b6\u6001\u8f6e\u8be2\u3002\u26a0\ufe0f \u8fd9\u662f<strong>\u5e94\u7528\u5c42\u8f6f\u4ef6\u5b9a\u65f6\u5668<\/strong>\uff0c\u7cbe\u5ea6\u53d7\u7cfb\u7edf\u8d1f\u8f7d\u5f71\u54cd\uff0c<strong>\u4e0d\u9002\u7528\u4e8e\u786c\u5b9e\u65f6\u63a7\u5236<\/strong>\u3002<\/p>\n<h2>1. API \u603b\u8868<\/h2>\n<table border=\"1\" cellpadding=\"6\" style=\"border-collapse:collapse;font-size:14px\">\n<tr>\n<th>\u51fd\u6570<\/th>\n<th>\u53c2\u6570<\/th>\n<th>\u8bf4\u660e<\/th>\n<\/tr>\n<tr>\n<td><code>start_timer(timer_id, timeout, countdown, repeat)<\/code><\/td>\n<td>timeout \u6beb\u79d2\uff08\u6700\u5c0f\u7ea6 10~20ms\uff09\uff1bcountdown 0=\u987a\u8ba1\u65f6\/1=\u5012\u8ba1\u65f6\uff1brepeat 0=\u65e0\u9650\u91cd\u590d\/N=\u89e6\u53d1 N \u6b21\u540e\u81ea\u52a8\u505c<\/td>\n<td>\u542f\u52a8\u5b9a\u65f6\u5668<\/td>\n<\/tr>\n<tr>\n<td><code>stop_timer(timer_id)<\/code><\/td>\n<td>\u5b9a\u65f6\u5668 ID<\/td>\n<td>\u7acb\u5373\u505c\u6b62<\/td>\n<\/tr>\n<tr>\n<td><code>on_timer(timer_id)<\/code><\/td>\n<td>\u8d85\u65f6\u56de\u8c03\u7edf\u4e00\u5165\u53e3<\/td>\n<td>\u7528 timer_id \u533a\u5206\u5e76\u53d1\u5b9a\u65f6\u5668<\/td>\n<\/tr>\n<tr>\n<td><code>get_timer_value(timer_id)<\/code><\/td>\n<td>\u5b9a\u65f6\u5668 ID<\/td>\n<td>\u83b7\u53d6\u5f53\u524d\u8ba1\u65f6\u503c\uff08ms\uff09\uff0c\u652f\u6301\u987a\/\u5012\u8ba1\u65f6<\/td>\n<\/tr>\n<\/table>\n<p>countdown \u53c2\u6570<strong>\u53ea\u5f71\u54cd\u5185\u90e8\u8ba1\u65f6\u663e\u793a\/\u8c03\u8bd5<\/strong>\uff0c\u4e0d\u6539 on_timer \u7684\u89e6\u53d1\u884c\u4e3a\u2014\u2014\u987a\u8ba1\u65f6\u5230 timeout \u89e6\u53d1\uff0c\u5012\u8ba1\u65f6\u4ece timeout \u51cf\u5230 0 \u89e6\u53d1\uff0c\u56de\u8c03\u90fd\u662f\u540c\u4e00\u4e2a\u3002<\/p>\n<h2>2. \u57fa\u7840\u7528\u6cd5<\/h2>\n<pre style=\"background:#f5f5f5;padding:10px;border-radius:4px\">function on_init()\n  start_timer(0, 1000, 0, 0)   -- 1\u79d2\u5468\u671f\uff0c\u65e0\u9650\u91cd\u590d\n  start_timer(1, 5000, 1, 1)   -- 5\u79d2\u5012\u8ba1\u65f6\uff0c\u53ea\u89e6\u53d11\u6b21\nend\n\nfunction on_timer(timer_id)\n  if timer_id == 0 then\n    -- \u6bcf\u79d2\u505a\u4e00\u6b21\uff1a\u72b6\u6001\u8f6e\u8be2\n  elseif timer_id == 1 then\n    -- 5\u79d2\u540e\u6267\u884c\u4e00\u6b21\n  end\nend<\/pre>\n<h2>3. \u5e94\u7528\uff1a\u5012\u8ba1\u65f6\u5b8c\u6210\u540e\u7ed9 PLC \u53d1\u6307\u4ee4<\/h2>\n<p>\u9700\u6c42\uff1a\u8bbe\u5b9a\u65f6\/\u5206\u540e\u5012\u8ba1\u65f6\uff0c\u5012\u8ba1\u65f6\u7ed3\u675f\u590d\u4f4d\u6309\u94ae\u5e76\u7ed9\u4e3b\u677f\u53d1\u6307\u4ee4\u3002\u63a7\u4ef6\u5e03\u5c40\uff1a\u4f4d\u72b6\u6001\u6307\u793a\u706f LW1002\uff08\u5207\u6362\u5f00\u5173\uff09\u63a7\u5236\u542f\u505c\uff1b\u6eda\u8f6e\u63a7\u4ef6 LW1000\uff08\u65f6\uff0c0~23\uff09\u3001LW1001\uff08\u5206\uff09\u8bbe\u65f6\u957f\uff1b\u6587\u672c\u63a7\u4ef6 LW1010 \u663e\u793a\u5012\u8ba1\u65f6\u3002<\/p>\n<pre style=\"background:#f5f5f5;padding:10px;border-radius:4px\">_TIMER_REPEAT_ = 0     -- \u603b\u65f6\u957f\uff08\u79d2\uff09\n_TIMER_CNT_ = 0        -- \u5df2\u89e6\u53d1\u6b21\u6570\n\nfunction on_update(slave, vtype, addr)\n  if vtype == VT_LW and addr == 0x1002 then\n    if get_uint16(VT_LW, 0x1002) == 1 and _TIMER_REPEAT_ &gt; 0 then\n      stop_timer(0)\n      start_timer(0, 1000, 0, _TIMER_REPEAT_)  -- \u6bcf\u79d2\u4e00\u6b21\uff0c\u5171 repeat \u6b21\n    else\n      stop_timer(0)\n    end\n  end\nend\n\nfunction on_timer(timer_id)\n  if timer_id == 0 then\n    _TIMER_CNT_ = _TIMER_CNT_ + 1\n    local remain = _TIMER_REPEAT_ - _TIMER_CNT_\n    -- \u663e\u793a\u5269\u4f59\u65f6\u95f4 mm:ss\n    set_string(VT_LW, 0x1010,\n      string.format(\"%02d:%02d\", math.floor(remain \/ 60), remain % 60))\n    if remain == 0 then\n      set_uint16(VT_LW, 0x1002, 0x00)   -- \u590d\u4f4d\u542f\u505c\u6309\u94ae\n      set_uint16(VT_LW, 0x1003, 0x01)   -- \u53d1\u6307\u4ee4\u7ed9\u4e3b\u677f\n    end\n  end\nend<\/pre>\n<p>\u8981\u70b9\uff1a_TIMER_REPEAT_ \u5728\u7528\u6237\u8bbe\u5b9a\u65f6\u957f\u65f6\u8ba1\u7b97\uff08hour*3600 + min*60\uff09\uff1bstart_timer \u7684 repeat \u4f20\u603b\u6b21\u6570\uff0con_timer \u6bcf\u6b21\u7d2f\u52a0 _TIMER_CNT_\uff0c\u76f8\u7b49\u5373\u7ed3\u675f\u3002set_notify(0\/1) \u63a7\u5236\u5b9a\u65f6\u5668\u671f\u95f4\u662f\u5426\u53d1\u51fa\u6307\u4ee4\u901a\u77e5\uff0c\u9700\u8981\u4e0b\u53d1\u65f6\u7f6e 1\u3002<\/p>\n<div class=\"tip\" style=\"border-left:4px solid #f7941e;padding:10px 14px;background:#fff8ef;margin:14px 0\">\u4e09\u4e2a\u5751\uff1a<strong>\u2460\u8f6f\u4ef6\u5b9a\u65f6\u5668\u7cbe\u5ea6\u53d7\u7cfb\u7edf\u8d1f\u8f7d\u5f71\u54cd<\/strong>\uff0c\u505a\u786c\u5b9e\u65f6\u63a7\u5236\u4f1a\u7ffb\u8f66\uff1b<strong>\u2461timeout \u6700\u5c0f\u7ea6 10~20ms<\/strong>\uff0c\u522b\u4f20 1ms \u671f\u671b\u51c6\uff1b<strong>\u2462repeat=N \u89e6\u53d1 N \u6b21\u540e\u81ea\u52a8\u505c<\/strong>\uff0c\u8981\u65e0\u9650\u5faa\u73af\u4f20 0\uff1b\u591a\u4e2a\u5b9a\u65f6\u5668\u5e76\u53d1\u65f6\u7528 timer_id \u533a\u5206\uff0con_timer \u662f\u7edf\u4e00\u5165\u53e3\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\u5b9a\u65f6\u5668\u300d\uff0c\u7248\u6743\u5f52\u5927\u5f69\u79d1\u6280\u6240\u6709\u3002<\/p>\n<span id=\"magicpostMarker\"><\/span>","protected":false},"excerpt":{"rendered":"<p>VisualHMI \u7684 Lua \u652f\u6301 32 \u4e2a\u8f6f\u4ef6\u5b9a\u65f6\u5668\uff08\u7d22&#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":[3326,3325,3328,3134,3327,735],"class_list":["post-51500","post","type-post","status-publish","format-standard","hentry","category-hmi-lua","category-hmi-dev","tag-on_timer","tag-start_timer","tag-stop_timer","tag-visualhmi","tag-3327","tag-735"],"_links":{"self":[{"href":"http:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/posts\/51500","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=51500"}],"version-history":[{"count":1,"href":"http:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/posts\/51500\/revisions"}],"predecessor-version":[{"id":51649,"href":"http:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/posts\/51500\/revisions\/51649"}],"wp:attachment":[{"href":"http:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/media?parent=51500"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/categories?post=51500"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/tags?post=51500"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}