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.

setup modbus tcp slave
#13
this is the script, its updating the 3 first register mapping data but not the 2 last. if i change order in them the values works. but still change the 2 last value to 0 after the update from knx. if i deactivate and activte the script, it shows again in the modbus till the next update. 



Code:
if not mb then
 require('genohm-scada.eibdgm')
 require('luamodbus')

 -- list of coil mapping, starting from 0
 coils = { '4/1/3', '6/1/3', '5/1/3' }

 -- list of register mapping, starting from 0
 registers = { '0/1/7', '0/1/8', '6/1/4', '4/1/7', '15/1/4' }

 -- list of register data types, element count must match registers table
 regdt = { dt.int8, dt.uint16, dt.int16, dt.bool }

 -- knx group write callback
 function knxgroupwrite(event)
   local value

   -- try to find matching coil
   for id, addr in ipairs(coils) do
     if event.dst == addr then
       value = knxdatatype.decode(event.datahex, dt.bool)
       mb:setcoils(id - 1, value)
     end
   end

   -- try to find matching register
   for id, addr in ipairs(registers) do
     if event.dst == addr then
       value = knxdatatype.decode(event.datahex, regdt[ id ])
       mb:setregisters(id - 1, value)
     end
   end
 end

 -- coil write callback
 function mbwritecoils(coil, value)
   local addr = coils[ coil + 1 ]
   if addr then
     grp.write(addr, value, dt.bool)
   end
 end

 -- register write callback
 function mbwriteregisters(register, value)
   local addr = registers[ register + 1 ]
   if addr then
     grp.write(addr, value, regdt[ register + 1])
   end
 end

 -- knx group monitor, handles group writes
 knxclient = eibdgm:new({ timeout = 0.1 })
 knxclient:sethandler('groupwrite', knxgroupwrite)

 -- modbus slave, listen on all interfaces and default port 502
 mb = luamodbus.tcp()
 mb:open('0.0.0.0', 502)

 -- setting slave id is optional
 -- mb:setslave(1)

 mb:setreceivetimeout(0,1)
 mb:setmapping(#coils, 0, #registers, 0)

 -- init coils
 for id, addr in ipairs(coils) do
   value = grp.getvalue(addr)
   mb:setcoils(id - 1, value)
 end

 -- init registers
 for id, addr in ipairs(registers) do
   value = grp.getvalue(addr)
   mb:setregisters(id - 1, value)
 end

 -- set callbacks for coil and register write
 mb:setwritecoilcb(mbwritecoils)
 mb:setwriteregistercb(mbwriteregisters)
end

-- handle modbus and knx
mb:handleslave()
knxclient:step()
Reply


Messages In This Thread
setup modbus tcp slave - by Patrik1985 - 12.02.2018, 07:24
RE: setup modbus tcp slave - by Daniel - 12.02.2018, 09:31
RE: setup modbus tcp slave - by Patrik1985 - 12.02.2018, 09:50
RE: setup modbus tcp slave - by Daniel - 12.02.2018, 10:13
RE: setup modbus tcp slave - by admin - 12.02.2018, 10:32
RE: setup modbus tcp slave - by Patrik1985 - 12.02.2018, 11:18
RE: setup modbus tcp slave - by admin - 12.02.2018, 12:17
RE: setup modbus tcp slave - by Patrik1985 - 12.02.2018, 12:59
RE: setup modbus tcp slave - by admin - 12.02.2018, 13:01
RE: setup modbus tcp slave - by Patrik1985 - 12.02.2018, 13:16
RE: setup modbus tcp slave - by Daniel - 12.02.2018, 13:28
RE: setup modbus tcp slave - by Patrik1985 - 13.02.2018, 09:29
RE: setup modbus tcp slave - by Patrik1985 - 13.02.2018, 11:25
RE: setup modbus tcp slave - by Daniel - 13.02.2018, 11:31
RE: setup modbus tcp slave - by Patrik1985 - 13.02.2018, 11:39
RE: setup modbus tcp slave - by Daniel - 13.02.2018, 11:42
RE: setup modbus tcp slave - by Patrik1985 - 13.02.2018, 13:36
RE: setup modbus tcp slave - by buuuudzik - 01.03.2018, 08:05
RE: setup modbus tcp slave - by admin - 01.03.2018, 08:20
RE: setup modbus tcp slave - by buuuudzik - 01.03.2018, 08:45
RE: setup modbus tcp slave - by admin - 01.03.2018, 11:11
RE: setup modbus tcp slave - by fabiorusco - 07.05.2018, 10:28
RE: setup modbus tcp slave - by admin - 07.05.2018, 11:30
RE: setup modbus tcp slave - by fabiorusco - 09.05.2018, 08:12
RE: setup modbus tcp slave - by admin - 09.05.2018, 08:30
RE: setup modbus tcp slave - by fabiorusco - 09.05.2018, 17:28

Forum Jump: