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.

LM as a Modbus slave : number of slaves
#6
(09.08.2022, 06:27)admin Wrote: If you only need read-only values on the modbus side you can simply periodically update the register values in the resident script itself. Modify register mapping in updateregisters() as needed. Each script must use a unique port (502, 503 etc).
Code:
if not mb then
  require('luamodbus')
  mb = luamodbus.tcp()

  mb:open('0.0.0.0', 502)
  mb:setslave(2)

  function setint64(grpaddr, regaddr)
    local value = grp.getvalue(grpaddr) or 0
    local raw = knxdatatype.encode(value, dt.int64).dataraw
    local r1 = raw:byte(2) * 0x100 + raw:byte(1)
    local r2 = raw:byte(4) * 0x100 + raw:byte(3)
    local r3 = raw:byte(6) * 0x100 + raw:byte(5)
    local r4 = raw:byte(8) * 0x100 + raw:byte(7)

    mb:setregisters(regaddr, r4, r3, r2, r1)
  end

  function updateregisters()
    setint64('32/1/1', 3204)
  end

  mb:setmapping(0, 0, 3300, 0)
  updateregisters()

  prev = os.time()
end

mb:handleslave()

curr = os.time()
if curr ~= prev then
  updateregisters()
  prev = curr
end

Sorry Admin, using your script (without modify it, except '32/1/1') no communication with Modbus master is possible.
Note : I suspended cyclic "Modbus handler" script and preserved "mbproxy" script in common functions.

Perhaps interesting for you 'timeout ?' > founded by monotoring in log : 

res, err = mb:handleslave()
log("mb:handleslave :",res,err)
* arg: 1
  * string: mb:handleslave :
* arg: 2
  * nil
* arg: 3
  * string: timeout
Reply


Messages In This Thread
RE: LM as a Modbus slave : number of slaves - by SigmaTec - 09.08.2022, 09:58

Forum Jump: