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.

is modbus device online
#1
Is there a way in lua to return true or false if a specific modbus device is online? say modbus device at address 3?

Many thanks
Reply
#2
You can check it like this
Code:
-- First parameter is slave ID, second is optional RTU port number (1, 2 or 3; default is 1)
function checkrtu(slave, portnr)
  local query = 'SELECT active FROM modbus_devices WHERE proto="rtu" AND slave=? AND portnr=?'
  if type(portnr) == 'number' and portnr == 2 or portnr == 3 then
    portnr = portnr - 1
  else
    portnr = 0
  end
  local res = db:getone(query, slave, portnr)
  return toboolean(res)
end

function checktcp(ip, port)
  local settings, res
  settings = tostring(ip) .. ':' .. tostring(port)
  res = db:getone('SELECT active FROM modbus_devices WHERE proto="tcp" AND settings=?', settings)
  return toboolean(res)
end
------------------------------
Ctrl+F5
Reply
#3
(04.09.2020, 07:29)Daniel. Wrote: You can check it like this
Code:
-- First parameter is slave ID, second is optional RTU port number (1, 2 or 3; default is 1)
function checkrtu(slave, portnr)
  local query = 'SELECT active FROM modbus_devices WHERE proto="rtu" AND slave=? AND portnr=?'
  if type(portnr) == 'number' and portnr == 2 or portnr == 3 then
    portnr = portnr - 1
  else
    portnr = 0
  end
  local res = db:getone(query, slave, portnr)
  return toboolean(res)
end

function checktcp(ip, port)
  local settings, res
  settings = tostring(ip) .. ':' .. tostring(port)
  res = db:getone('SELECT active FROM modbus_devices WHERE proto="tcp" AND settings=?', settings)
  return toboolean(res)
end

Many Thanks, it works perfectly  Smile
Reply
#4
(04.09.2020, 07:29)Daniel. Wrote: You can check it like this
Code:
-- First parameter is slave ID, second is optional RTU port number (1, 2 or 3; default is 1)
function checkrtu(slave, portnr)
  local query = 'SELECT active FROM modbus_devices WHERE proto="rtu" AND slave=? AND portnr=?'
  if type(portnr) == 'number' and portnr == 2 or portnr == 3 then
    portnr = portnr - 1
  else
    portnr = 0
  end
  local res = db:getone(query, slave, portnr)
  return toboolean(res)
end

function checktcp(ip, port)
  local settings, res
  settings = tostring(ip) .. ':' .. tostring(port)
  res = db:getone('SELECT active FROM modbus_devices WHERE proto="tcp" AND settings=?', settings)
  return toboolean(res)
end
Hi
As i can get the result in a group address??
Thanks in advance
Reply
#5
grp.checkwrite('1/1/1', checkrtu(1, 1))

or

grp.checkwrite('1/1/1', checktcp('192.168.0.11', 502))
Reply
#6
(13.09.2020, 17:57)Erwin van der Zwart Wrote: grp.checkwrite('1/1/1', checkrtu(1, 1))

or

grp.checkwrite('1/1/1', checktcp('192.168.0.11', 502))
thanks Erwin!
Reply


Forum Jump: