VisualHMI Modbus one master and multiple slaves: start_read polling, cycle scheduling, and read/write slaves

freeFree Technical Resource

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

Modbus One Master Multiple Slaves: The HMI serves as the Modbus master, polling the registers of multiple slaves through the start_read function in the background. The core functions include start_read to create tasks, get_xxx to read from the cache, and on_cmd_resp to receive results - without blocking the script.

1. start_read parameters

start_read(index, vtype, addr, quantity, cycle, cycle_run, mode)
parametersdescription
indextask index 0~127, stop_read(index) uses
vtyperegister type (VT_0x/VT_3x/VT_4x, etc.)
addrstarting address
quantitynumber of registers to read 1~120
cycleoptional, polling cycle multiplier (default 0=read every cycle)
cycle_runoptional, the execution count within the cycle (0-based, must be < cycle)
modeoptional, 0=continuous cycle read (default)/1=read once only (requires create_resp_que() first)

2. Basic Usage

function on_init()
  -- 每周期都读从机0的 4x1000~1009(10个寄存器)
  start_read(0, VT_4x, 0x1000, 10)
  -- 每 3 个周期读一次从机1的 4x2000~2009,且在周期内第3次执行
  start_read(1, VT_4x, 0x2000, 10, 3, 2)
end

function on_run(screen)
  -- 读数据直接拿缓存,零通信开销
  local v = get_uint16(VT_4x, 0x1000)
  local f = get_float(VT_4x, 0x2000)
end

start_read only creates tasks, actual data is read from local cache through get_xxx - the same mechanism as in the register operation section. To receive a read completion notification, use the on_cmd_resp callback (requires create_resp_que).

3. Multi-Slave Polling Scheduling

-- 8 台从机,每台读 20 个寄存器,错开周期避免总线拥堵
function on_init()
  for i = 0, 7 do
    start_read(i, VT_4x, 0x0000, 20, 8, i)  -- 8周期轮一遍,每周期读一台
  end
end

function on_cmd_resp(slave, vtype, addr, count, ret, wr)
  if ret == 0 then
    -- 第 slave 台从机读取成功,处理数据
  else
    -- 通信异常,标记从机离线
  end
end

With multiple slaves, bus bandwidth is limited, so use cycle/cycle_run for off-peak reading; exceptions are determined by checking if ret is non-zero in on_cmd_resp, and marked offline.

Three Pitfalls:① quantity can only be up to 120, exceeding this requires multiple start_reads;② cycle_run must be less than cycle, and cycle_run starts counting from 0;③ stop_read retains the last value in the cache after the task is stopped- it will not be reset to zero, and offline determination relies on ret in on_cmd_resp.

Compiled from Guangzhou Dacai Technology VisualHMI development documentation (hmi-doc.gz-dc.com) LUA tutorial "Modbus One Master Multiple Slaves", copyright reserved by Dacai Technology.

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