08.09.2022, 11:28
If you want quick polling you need to use one resident script per each connection because Modbus mapper can only poll one TCP device at a time.
Use this example as a starting point. The connection is kept open between requests. Multiple registers are read at once to speed things up.
Use this example as a starting point. The connection is kept open between requests. Multiple registers are read at once to speed things up.
Code:
if not mb then
require('luamodbus')
mb = luamodbus.tcp()
mb:open('192.168.0.9')
res, err = mb:connect()
if not res then
mb:close()
mb = nil
log('connect failed', err)
end
end
if mb then
v1, v2, v3 = mb:readregisters(10, 3)
if v1 ~= nil then
grp.checkupdate('1/1/1', v1)
grp.checkupdate('1/1/2', v2)
grp.checkupdate('1/1/3', v3)
else
mb:close()
mb = nil
log('read failed', v2)
end
end