LogicMachine Forum
Modbus tcp scripting help - Printable Version

+- LogicMachine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: Modbus tcp scripting help (/showthread.php?tid=4437)



Modbus tcp scripting help - christian.hegland@caverion.com - 11.12.2022

Hi,

I'm having a hard time understanding how to make a simple script, for controlling a modbus device (off, Auto)

I've made a trigger sensor which I've tagged, and used in event based scripting.

But it seems like its working almost as expexted, nut sometimes the registers not get fullt set. Maybe theres some comm.issues 
which causes some timeouts or drops. 

Is there anyway to make a for-loop to run the script for X runs, until the register feedback is what i sent as command?

Would also be great to use only one script for multiple mbslave ID's. So a loop here also starting from slave 1 til 3 for instance.
Now I'm currently running this script duplicated for 3 devices on the same Modbus TCP gateway, modified only the mbslave ID.


This is the script:

Code:
require('luamodbus') mb = luamodbus.tcp() mb:open('192.168.1.7', 502) mb:connect() mb:setslave(2) if event.getvalue() >= 0 then   mb:writeregisters(367, 0) -- Manual stop command else   mb:writeregisters(367, 3) -- Auto command end mb:close()



RE: Modbus tcp scripting help - admin - 12.12.2022

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



RE: Modbus tcp scripting help - christian.hegland@caverion.com - 13.12.2022

Thank you very much! Big Grin