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.

LM as a Modbus slave : number of slaves
#4
(08.08.2022, 13:08)admin Wrote: In TCP mode you can have multiple slave scripts but they must use different ports (e.g. 502, 503 etc). In RTU mode only one slave is supported at the moment.

Hi Admin,

Ok, I understood the principle. But how to decompose the cyclic script which sets the port (eg. 502) and the slave id (eg. 2) into several slave scripts with different ports?

How will the mbConfusedetregisters() function find its way?

Code:
-- modbus init cyclic script

if not mb then


require('luamodbus')

-->>>>>>>>>Modbus slave setting<<<<<<<<<<<<<<<<<<<<<<<<
 
------------------------------------------------------
-- Pour Modbus RTU
------------------------------------------------------
--mb = luamodbus.rtu()
--mb:open('/dev/RS485', 19200, 'E', 8, 1, 'H')
--mb:connect()
------------------------------------------------------
-- Pour Modbus TCP
------------------------------------------------------
mb = luamodbus.tcp()
mb:open('0.0.0.0', 502)
------------------------------------------------------ 

-->>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 
 
-- slave id
mb:setslave(2)
 
-- init slave storage for coils, discrete inputs, holding registers and input registers
mb:setmapping(1, 1, 3300, 1)

  --*****************************************************
-- >>>>>>>>>>>Coil write callback<<<<<<<<<<<<<<<<<<<<<<
mb:setwritecoilcb(function(coil, value)
if coil == 0 then
grp.write('0/0/12', value, dt.bool)
else
alert('coil: %d = %s', coil, tostring(value))
end
end)
--*****************************************************
-->>>>>>>>>>>Register write callback<<<<<<<<<<<<<<<<<<<
mb:setwriteregistercb(function(register, value)
if register == 0 then
-- send value limited to 0..100
grp.write('0/0/13', math.min(100, value), dt.scale)
else
alert('register: %d = %d', register, value)
end
end)
--*****************************************************
end
-- server part init
if not server then
log("Modbus slave handler (cyclic) : IN...server init")
require('rpc')
-- incoming data handler
local handler = function(request)
local fn, res
fn = tostring(request.fn)
if not mb[ fn ] then
return { nil, 'unknown function ' .. fn }
end
if type(request.params) == 'table' then
table.insert(request.params, 1, mb)
res = { mb[ fn ](unpack(request.params)) }
else
res = { mb[ fn ](mb) }
end
return res
end
server = rpc.server('127.0.0.1', 28002, 'mbproxy', handler, 0.01)
log("Modbus slave handler (cyclic) : OUT...handleslave = ")
end
res, err = mb:handleslave()
res, err = server:step()

Thank's for your time....
Reply


Messages In This Thread
RE: LM as a Modbus slave : number of slaves - by SigmaTec - 09.08.2022, 04:59

Forum Jump: