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 Slave
#1
Hello following the update of a HomeLynk I have my script for sending data on a IRIO that no longer works:


Code:
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
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.
Reply
#2
Replace all instances of cnv.tonumber(val) with tonumber(val) or 0
Reply
#3
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.
Reply
#4
Can you log what value are you trying to convert to number. Is it Boolean value?
Reply
#5
I sent data 2 Bytes floating point.
B.R.
Reply
#6
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:
1234567
function mbtonumber(val)   if type(val) == 'boolean' then     return val and 1 or 0   else     return tonumber(val) or 0   end end
Reply


Forum Jump: