Logic Machine Forum
MODBUS TCP SLAVE - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Gateway (https://forum.logicmachine.net/forumdisplay.php?fid=10)
+--- Thread: MODBUS TCP SLAVE (/showthread.php?tid=4256)



MODBUS TCP SLAVE - palomino - 22.09.2022

I have this exmple the modbus tcp slave.
But when I try to read the log I get an error.
why?

this is the type error that have 

* arg: 1
  * string: read failed
* arg: 2
  * string: Bad file descriptor


Code:
require('luamodbus')

if not mb then
   mb = luamodbus.tcp()
  end

  mb:open('0.0.0.0', 502)
  mb:connect()
  mb:setslave(2)
  mb:setmapping(1, 1, 3300, 1)
 
  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)

   val, err = mb:setregisters(regaddr, r4,r3,r2,r1)

end

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


  updateregisters()
 
val, err = mb:readregistervalue(3204, 'int64', 'b')
log (val,err)

  if val then
    log('read ok', val)
  else
    log('read failed', err)
  end



RE: MODBUS TCP SLAVE - admin - 22.09.2022

This is the original example: https://forum.logicmachine.net/showthread.php?tid=4182&pid=27011#pid27011
Yours is modified with master and slave functions mixed together.


RE: MODBUS TCP SLAVE AND RECEIVE - palomino - 05.10.2022

Hello.
I have a question. I need to send and receive data through the LM in Modbus slave mode.
When I write the data to my register, the master can read it, but when the master try to send me a data,  doesn't work.  I can't read the register. I think that the master can not write me in my register, I always see nill when i try to read. can you help
Code:
if not mb then
  require('luamodbus')
  
  mb = luamodbus.tcp()
  res, err = mb:open('0.0.0.0', 502)
  res, err = mb:setslave(2)
  log("Modbus slave IN : 502/2")
 
  function setintmo(grpaddr, regaddr)
   
    local value = grp.getvalue(grpaddr) or 0
    res, err = mb:setregisters(regaddr, value)
   
  end ------- setint(grpaddr, regaddr)

  function setintresgis (direc, regist)
         
       local valuer = mb:readregisters(regist)
    log (valuer)
    if valuer ~= nill then
       grp.write(direc, valuer)
      end
       
    end --- setintresgis (direc, regist)
 
 
    function updateregisters()
    setintmo('32/1/2', 2)
  end --- updateregisters()

    function updateinputregisters()
     setintresgis('32/1/4',10)
    end
   
   
  mb:setmapping(100, 100, 100, 100)
  updateregisters()
  updateinputregisters()
 
end --- mb

mb:handleslave()

curr = os.time()
if curr ~= prev then
updateregisters()
updateinputregisters()
log ('A')
  prev = curr

end
me?