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
#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


Messages In This Thread
RE: Modbus tcp scripting help - by admin - 12.12.2022, 07:37

Forum Jump: