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.

RE: MODBUS TCP SLAVE AND RECEIVE
#1
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
Reply
#2
In original description of this script it says it is read-only. Why don't you just use universal slave script?
https://openrb.com/lm-as-modbus-tcp-slave/
------------------------------
Ctrl+F5
Reply
#3
I understand that it is for read only. But I need to write too, can you help me with a script for writing?
Reply
#4
Use this
https://forum.logicmachine.net/showthread.php?tid=4288
------------------------------
Ctrl+F5
Reply


Forum Jump: