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: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: ModBus TCP Slave (/showthread.php?tid=2357)



ModBus TCP Slave - Gadjoken - 20.11.2019

Hello following the update of a HomeLynk I have my script for sending data on a IRIO that no longer works:


Code:
SLAVE_ADDRESS='192.168.70.246'
SLAVE_PORT=502
SLAVE_ID=1
NB=300   -- Number of coils, discrete inputs, holding registers and input registers
WORD=16
DWORD=32



function update_registers(knx_address,registre_nb,size)
val=grp.getvalue(knx_address)

 
  if size==16 then
   
    mb:setregisters(registre_nb,cnv.tonumber(val))

  elseif size==32 then
 
    shift=bit.rshift(cnv.tonumber(val),16)
    mb:setregisters(registre_nb, shift,cnv.tonumber(val))
   
  end

end


if not mb then
require('luamodbus')
mb = luamodbus.tcp()


--¤ Set correct parameters for the connection

t=mb:open(SLAVE_ADDRESS, SLAVE_PORT) -- TCP slave IP

--¤ Connection

o=mb:connect()

--¤ Set TCP slave address

mb:setslave(SLAVE_ID)
 
-- init slave storage for coils, discrete inputs, holding registers and input registers
mb:setmapping(0, 0, NB, 0)

end

update_registers("3/1/10",0,DWORD)
update_registers("3/2/10",2,DWORD)
update_registers("3/3/1",4,DWORD)
update_registers("3/3/2",6,DWORD)
update_registers("3/4/1",8,DWORD)
update_registers("3/4/2",10,DWORD)
update_registers("3/5/1",12,DWORD)
update_registers("3/5/2",14,DWORD)
update_registers("3/6/10",16,DWORD)
update_registers("2/0/0",18,DWORD)
update_registers("2/0/1",20,DWORD)
update_registers("2/0/2",22,DWORD)
update_registers("2/0/3",24,DWORD)

mb:handleslave()
here is the error : 
Resident script:20: attempt to index global 'cnv' (a nil value)
stack traceback:
Resident script:20: in function 'update_registers'


Do you have a solution to restore the connection?
Thanks.
B.R.


RE: ModBus TCP Slave - admin - 20.11.2019

Replace all instances of cnv.tonumber(val) with tonumber(val) or 0


RE: ModBus TCP Slave - Gadjoken - 20.11.2019

Replace with tonumber (val) and error message is :
Resident script:20: bad argument #1 to 'rshift' (number expected, got nil)
stack traceback:
[C]: in function 'rshift'
Resident script:20: in function 'update_registers'

I tried with 0 no error but no communication.
B.R.


RE: ModBus TCP Slave - admin - 20.11.2019

Can you log what value are you trying to convert to number. Is it Boolean value?


RE: ModBus TCP Slave - Gadjoken - 21.11.2019

I sent data 2 Bytes floating point.
B.R.


RE: ModBus TCP Slave - admin - 21.11.2019

2 Bytes floating point is a number so it cannot raise an error. Can you post data types for all objects?

Try this function instead of tonumber (add it to script and replace tonumber with mbtonumber):
Code:
function mbtonumber(val)
  if type(val) == 'boolean' then
    return val and 1 or 0
  else
    return tonumber(val) or 0
  end
end