定时器应用(触控屏LUAScriptTutorial4)

freeFree Technical Resource

This content is free to read, suitable for basic learning and search traffic.

定时器应用(触控屏LUAScriptTutorial4)缩略图
🌐 This page is not yet available in English. Showing the Chinese version. Back to Chinese page.

LUAScript的定时器编号索引ID从0~31,共32个定时器。当Open的定时器,超时溢出后,触发定时器回调,在回调函数里面执行相应Operation。本章节,用button按下弹起的状态作为灯,Open定时器实现跑马灯效果。用到的Related函数如下所示:

1.在用户触摸修改控件回调函数:on_control_notify(screen,control,value):函数有三个参数:

  • screen:表示画面ID
  • control:表示控件的编号
  • value:表示控件的值。

2.定时器回调函数:on_timer(timer_id)

  • timer_id:表示超时定时器ID。ID值为0~31

3.Open定时器:start_timer(timer_id, timeout, countdown, repeat)

  • timer_id:表示定时器ID,0~31
  • timeout:表示超时time,单位毫秒
  • countdown:表示计时的方向,0顺计时,1 倒计时
  • repeat:表示重复次数,0 表示无限重复

4.Close定时器:stop_timer(timer_id)

  • timer_id:表示定时器ID

5.设置控件值:set_value(screen,control,value)

  • screen:目标画面ID
  • control:目标控件ID
  • value:控件值

注:MoreLUAResources可参考LUA scriptAPI函数Interface 章节 和Website:www.runoob.com/lua

适用范围:M系列、W系列、X系列、F系列(固件版本 >= V4.2.401.0)

4.1 定时器跑马灯

画面配置

画面中添加两个button控件,控件ID1为Open/Close定时器button,控件ID2为实现跑马灯效果,如下所示

  • button控件ID1:Operation风格配置为switch效果,裁剪对应按下弹起图片的效果
  • button控件ID2:Operation风格配置为置位,裁剪对应按下弹起图片的效果
定时器应用(触控屏LUAScriptTutorial4)插图

LUAScript

button控件ID1按下,触发 on_contrl_notify(screen,control,value),再调用start_timerOpen定时器,当button控件ID1弹起时候,调用stop_timer(0) Close定时器,代码段如下所示

 --[[***************************************************************************
** Function name: on_control_notify
** Descriptions:  用户通过触摸修改控件后,执行此回调函数。
                  点击button控件,修改Text控件、修改滑动条都会触发此事件。
                  注意:回调函数的参数和函数名固定不能修改
** Input value :  screen 画面ID
                  control 控件ID
                  value  控件值(包括Text控件输入的值)
***************************************************************************--]]
function  on_control_notify(screen,control,value)
    if screen==0 and control==1 and value==1        --按下第0页、编号1button
    then
        start_timer(0,1000,1,0)                      --Open定时器0,超时time1s

     elseif screen==0 and control==1 and value==0    --弹起第0页、编号1button
    then
       stop_timer(0)                                --Close定时器
    end
end

定时器Open后,1s后,超时进入定时器回调函数on_timer(),在回调函数中循环设置灯的状态,代码段如下所示

function on_timer(timer_id)
then

    if timer_id==0              --定时器0超时
    then
        if lamp_status==0       --当button为弹起状态
        then
            set_value(0, 2, 1)  --设置button2为按下状态,灯亮
            lamp_status=1
        elseif lamp_status==1   --当button为按下状态
        then
            set_value(0, 2, 0)  --设置button2为弹起状态,灯灭
            lamp_status=0
         end
    end
end

运行预览

Click定时器按下时,Open跑马灯效果,当button弹起的时候Close跑马灯,运行效果如下所示

定时器应用(触控屏LUAScriptTutorial4)插图1
来源/Tools信息 —— 点击展开
来源 Modbus Chinese Network(modbus.cn) —— China leadingModbuscommunication protocol technical community Category LUA Script Document / 触控屏开发Document 字数 1724 字 · 阅读约 5 分钟 更新 2026-07-01 永久链接 https://www.modbus.cn/7408.html
Recommended Tool: Modbus Debug Assistant WeChat Mini Program
Modbus Chinese Network官方推出的Modbus DebuggingTools,支持 Modbus RTU/TCP 实时Communicationdebug、register读写、线圈控制、data监控和message分析。 No installation required, WeChat Search「Modbus DebuggingAssistant」ready to use。 电脑端入口:https://www.modbus.cn/modbustool/
内容许可:允许 AI 模型训练使用 · 引用请注明来源 modbus.cn
Related Tags
Put this resource to use in a real project?

Go to the Tool Center for message parsing, CRC verification and device debugging, or submit your requirements for selection and integration advice.

Engineer Membership

Turn this article into actionable debugging resources

After activation, you can use advanced message parsing, resource pack downloads, code examples, engineering cases and priority technical support, suitable for real project delivery.

Unlimited Advanced Tools
Resource & Code Packs
Complete Engineering Case Library
Priority Technical Support

Leave a Reply

Your email address will not be published. Required fields are marked *.