定时器应用(触控屏LUA脚本教程4)

freeFree Technical Resource

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

定时器应用(触控屏LUA脚本教程4)Thumbnail
🌐 This page is not yet available in English. Showing the Chinese version. Back to Chinese page.

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

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

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

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

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

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

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

4.关闭定时器:stop_timer(timer_id)

  • timer_id:表示定时器ID

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

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

注:MoreLUA资料可参考LUA scriptAPI函数接口 章节 和网站:www.runoob.com/lua

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

4.1 定时器跑马灯

画面配置

画面中添加两个按钮控件,controlID1为开启/关闭定时器按钮,controlID2为实现跑马灯效果,如下所示

  • 按钮控件ID1:操作风格配置为开关效果,裁剪对应按下弹起图片的效果
  • 按钮控件ID2:操作风格配置为置位,裁剪对应按下弹起图片的效果
定时器应用(触控屏LUA脚本教程4)Figure

LUAScript

按钮控件ID1按下,触发 on_contrl_notify(screen,control,value),再调用start_timer开启定时器,当按钮控件ID1弹起时候,调用stop_timer(0) 关闭定时器,代码段如下所示

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

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

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

function on_timer(timer_id)
then

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

运行预览

Click定时器按下时,开启跑马灯效果,当按钮弹起的时候关闭跑马灯,运行效果如下所示

定时器应用(触控屏LUA脚本教程4)Figure1
来源/工具信息 —— Click to Expand
来源 Modbus Chinese Network(modbus.cn) —— China leadingModbuscommunication protocol technical community Category LUA script documentation / Touch screen development documentation 字数 1724 字 · 阅读约 5 分钟 更新 2026-07-01 永久链接 https://www.modbus.cn/7408.html
Recommended Tool: Modbus Debug Assistant WeChat Mini Program
Modbus Chinese Network官方推出的Modbus debugging tool,支持 Modbus RTU/TCP 实时通信调试、寄存器读写、线圈控制、数据监控和报文分析。 No installation required, WeChat Search「Modbus Debugging Assistant」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 *.