This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

Modbus tcp scripting help
#1
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()
Best regards
Christian
Reply
#2
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
Reply
#3
Thank you very much! Big Grin
Best regards
Christian
Reply


Forum Jump: