4-inch touch screen Lu script for numerical judgment, output control, and Modbus relay

freeFree Technical Resource

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

4-inch touch screen Lu script for numerical judgment, output control, and Modbus relay

Code description:

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, "已停止") -- 设置文本为 "已停止"
    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, "已停止") -- 设置文本为 "已停止"
    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, "已停止") -- 设置文本为 "已停止"
    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, "已停止") -- 设置文本为 "已停止"
    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, "已停止") -- 设置文本为 "已停止"
    end
end

 

Advanced Lua scripting techniques in 2026

In practical project usage, the following optimizations can significantly enhance the stability and maintainability of scripts:

  1. Add dead zone (lag interval)Avoid frequent switching of the relay near the threshold. For example, turn on the dehumidifier when the humidity is above 70% and turn it off only when it is below 65% - a 5% dead zone can effectively prevent relay "jitter".
  2. Initial state check after power-onAfter the touch screen is powered on, it is recommended to read the current status of all coils during the first scheduled callback to ensure that the displayed image is consistent with the actual situation.
  3. Communication exception handlingWhen Modbus reading fails, do not trigger an action immediately. Instead, set up a continuous failure counter (such as alarming only after three consecutive reading failures) to avoid false alarms due to transient interference.
  4. Multi-condition priorityWhen multiple interlocks exist (such as temperature and humidity exceeding limits), it is necessary to clearly define the priority and processing order of each condition to avoid logical conflicts.

Complete Lua programming reference documentation and sample code can be accessed from the development documentation section of modbus.cn for the latest version.

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 *.