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 example
#1
This script is outdated use this script instead: https://forum.logicmachine.net/showthread.php?tid=4288



Simple example of how to implement Modbus TCP slave via a resident script (sleep time = 0). It only supports binary objects as coils and 1-byte / 2-byte integer objects as registers. Number of coils and registers is not limited, object mapping can be set by filling coils, registers and regdt tables.

Code:
if not mb then   require('genohm-scada.eibdgm')   require('luamodbus')   -- list of coil mapping, starting from 0   coils = { '1/1/1', '1/1/2' }   -- list of register mapping, starting from 0   registers = { '2/2/2', '3/3/3' }   -- list of register data types, element count must match registers table   regdt = { dt.int8, dt.uint16 }   -- 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:loop(0.1)


Messages In This Thread
Modbus TCP slave example - by admin - 08.01.2016, 08:46
RE: Modbus TCP slave example - by Deniss - 15.01.2016, 08:58
RE: Modbus TCP slave example - by admin - 15.01.2016, 14:39
RE: Modbus TCP slave example - by admin - 18.01.2016, 12:57
RE: Modbus TCP slave example - by automatikas - 02.02.2017, 11:30
RE: Modbus TCP slave example - by admin - 02.02.2017, 12:09
RE: Modbus TCP slave example - by automatikas - 02.02.2017, 13:35
RE: Modbus TCP slave example - by automatikas - 29.03.2017, 10:26
RE: Modbus TCP slave example - by admin - 29.03.2017, 10:37
RE: Modbus TCP slave example - by leondias - 29.03.2017, 10:40
RE: Modbus TCP slave example - by automatikas - 29.03.2017, 10:41
RE: Modbus TCP slave example - by admin - 29.03.2017, 10:55
RE: Modbus TCP slave example - by automatikas - 29.03.2017, 11:15
RE: Modbus TCP slave example - by Deniss - 20.04.2017, 06:47
RE: Modbus TCP slave example - by admin - 20.04.2017, 15:38
RE: Modbus TCP slave example - by duvelken - 02.12.2017, 08:13
RE: Modbus TCP slave example - by admin - 03.12.2017, 09:58
RE: Modbus TCP slave example - by FatMax - 13.12.2018, 14:44
RE: Modbus TCP slave example - by admin - 13.12.2018, 15:23
RE: Modbus TCP slave example - by FatMax - 13.12.2018, 15:53
RE: Modbus TCP slave example - by rw_echo - 16.03.2023, 07:57
RE: Modbus TCP slave example - by admin - 17.03.2023, 10:52

Forum Jump: