12.12.2022, 07:37
This will try writing 3 times to 3 slave IDs (1, 2, 3). Any possible errors can be found in Logs.
Code:
require('luamodbus')
if event.getvalue() >= 0 then
value = 0 -- Manual stop command
else
value = 3 -- Auto command
end
for retry = 1, 3 do
mb = luamodbus.tcp()
mb:open('192.168.1.7', 502)
cres, cerr = mb:connect()
if cres then
for id = 1, 3 do
mb:setslave(id)
wres, werr = mb:writeregisters(367, value)
if not wres then
log('write failed', id, werr)
end
end
else
log('connect failed', cerr)
os.sleep(1)
end
mb:close()
if cres then
break
end
end