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 Polling Delay
#10
You need to create different objects for control and status. Reading script will update status objects while writing script will use control object values. The reading script will check the time when the control object was updated. If it's less than 2 seconds then the status object will not be updated. The writing script can also update the status object value right away so the status value is synchronized.

Code:
if not mb then
  require('luamodbus')
  mb = luamodbus.tcp()
  mb:open('192.168.0.9')
  
  res, err = mb:connect()
  if not res then
    mb:close()
    mb = nil
    log('connect failed', err)
  end

  function update(control, status, value)
    local ut = grp.find(control).updatetime
    local delta = math.abs(os.time() - ut)

    if delta >= 2 then
      grp.checkupdate(status, value)
    end
  end
end

if mb then
  v1, v2, v3 = mb:readregisters(10, 3)

  if v1 ~= nil then
    update('1/1/1', '1/1/4', v1)
    update('1/1/2', '1/1/5', v2)
    update('1/1/3', '1/1/6', v3)
  else
    mb:close()
    mb = nil
    log('read failed', v2)
  end
end
Reply


Messages In This Thread
Modbus TCP Polling Delay - by manos@dynamitec - 08.09.2022, 10:23
RE: Modbus TCP Polling Delay - by Daniel - 08.09.2022, 10:40
RE: Modbus TCP Polling Delay - by admin - 08.09.2022, 11:28
RE: Modbus TCP Polling Delay - by admin - 08.09.2022, 11:49
RE: Modbus TCP Polling Delay - by admin - 12.09.2022, 12:08
RE: Modbus TCP Polling Delay - by admin - 14.09.2022, 06:54

Forum Jump: