4寸触摸屏Lu脚本实现数值判断输出控制ModbusRelay

freeFree Technical Resource

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

4寸触摸屏Lu脚本实现数值判断输出控制Modbus继电器缩略图
🌐 This page is not yet available in English. Showing the Chinese version. Back to Chinese page.

代码说明:

当您运行这段Lua代码,它将在每隔1秒钟的定时回调函数中执行以下操作:

1. 重新读取各个保持寄存器的值,这些寄存器存储了不同传感器的数据,如湿度、温度和烟雾感应器等。

2. 获取文本控件的数值,这些数值是您在界面上设置的阈值,用于判断是否满足条件以控制线圈的状态。

3. 根据条件判断,将根据不同的条件设置线圈的状态,以控制相应的设备。例如,如果湿度值高于阈值,代码会打开相应的线圈,表示设备正在运行,并将文本控件显示为”运行中”。如果湿度值低于阈值减3,代码会关闭线圈,表示设备已停止,并将文本控件显示为”stopped”。

4. 通过这些操作,您可以根据传感器的数据状态,实时控制设备的运行状态,同时通过文本控件在界面上清晰地显示设备的状态信息。这使您能够实现设备的自动控制和状态显示,以满足不同条件下的需求。

 

function on_systick()
    -- 重新读取保持寄存器的值
    local reg1 = get_variant("1#湿度")
    local reg2 = get_variant("6#湿度")
    local reg3 = get_variant("8#湿度")
    local reg4 = get_variant("机箱温度")
    local reg5 = get_variant("烟雾感应器")

    -- 获取文本控件的数值
    local txtValue1 = get_value(10, 1)
    local txtValue2 = get_value(10, 2)
    local txtValue3 = get_value(10, 3)
    local txtValue4 = get_value(14, 2)
    local txtValue5 = get_value(14, 4)

    -- 根据条件设置线圈的值并更新文本控件
    if reg1 >= txtValue1 then
        mb_write_coil_05(16, 0, 1)  -- 打开线圈1
        print("打开线圈1")
        set_text(10, 16, "运行中") -- 设置文本为 "运行中"
    elseif reg1 <= (txtValue1-3) then
        mb_write_coil_05(16, 0, 0)  -- 关闭线圈1
        print("关闭线圈1")
        set_text(10, 16, "stopped") -- 设置文本为 "stopped"
    end

    if reg2 >= txtValue2 then
        mb_write_coil_05(16, 1, 1)  -- 打开线圈2
        print("打开线圈2")
        set_text(10, 17, "运行中") -- 设置文本为 "运行中"
    elseif reg2 <= (txtValue2-3) then
        mb_write_coil_05(16, 1, 0)  -- 关闭线圈2
        print("关闭线圈2")
        set_text(10, 17, "stopped") -- 设置文本为 "stopped"
    end

    if reg3 >= txtValue3 then
        mb_write_coil_05(16, 2, 1)  -- 打开线圈3
        print("打开线圈3")
        set_text(10, 19, "运行中") -- 设置文本为 "运行中"
    elseif reg3 <= (txtValue3-3) then
        mb_write_coil_05(16, 2, 0)  -- 关闭线圈3
        print("关闭线圈3")
        set_text(10, 19, "stopped") -- 设置文本为 "stopped"
    end

    if reg4 >= txtValue4 then
        mb_write_coil_05(16, 3, 1)  -- 打开线圈4
        print("打开线圈4")
        set_text(21, 1, "运行中") -- 设置文本为 "运行中"
    elseif reg4 <= (txtValue4-3) then
        mb_write_coil_05(16, 3, 0)  -- 关闭线圈4
        print("关闭线圈4")
        set_text(21, 1, "stopped") -- 设置文本为 "stopped"
    end

    if reg5 <= txtValue5 then
        mb_write_coil_05(16, 4, 1)  -- 打开线圈5
        print("打开线圈5")
        set_text(20, 1, "运行中") -- 设置文本为 "运行中"
    elseif reg5 <= (txtValue5-3) then
        mb_write_coil_05(16, 4, 0)  -- 关闭线圈5
        print("关闭线圈5")
        set_text(20, 1, "stopped") -- 设置文本为 "stopped"
    end
end

 

2026年Lua脚本进阶技巧

在实际项目使用中,以下几点优化可以显著提升脚本的稳定性和可维护性:

  1. 加入死区(滞后区间):避免继电器在阈值附近频繁开关。例如湿度高于70%开启除湿,低于65%才关闭——5%的死区能有效防止继电器”抖动”。
  2. 上电初始状态检查:触摸屏上电后,建议在首次定时回调中读取所有线圈的当前状态,确保画面显示与实际一致。
  3. 通信异常处理:当Modbus读取失败时,不要立即触发动作,应设置一个连续失败计数器(如连续3次读取失败才报警),避免因瞬时干扰误报。
  4. 多条件优先级:当存在多重联锁时(例如温度超标且湿度超标),需要明确定义各条件的优先级和处理顺序,避免逻辑冲突。

完整的Lua编程参考文档和示例代码,可访问 modbus.cn 开发文档专区获取最新版本。

技术术语(共 4 个)—— Click to Expand
registerModbus 寄存器存储数据单元,分线圈/离散输入/保持/输入寄存器四类
Sensors将物理量转换为电信号的检测装置
线圈Modbus位可读写数据,地址从00001开始
保持寄存器Modbus 16位可读写数据,地址从40001开始
来源/工具信息 —— Click to Expand
来源 Modbus Chinese Network(modbus.cn) —— China leadingModbuscommunication protocol technical community Category Uncategorized 字数 2125 字 · 阅读约 6 分钟 更新 2026-07-01 永久链接 https://www.modbus.cn/24521.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 *.