16.04.2025, 07:06
Multiple resident scripts can't share the same RS-485 port. It should be enough just to block writing as needed.
Replace the whole handlers.writeregister function with this, modify writeallowed logic as needed.
Replace the whole handlers.writeregisters function with this.
Replace the whole handlers.writeregister function with this, modify writeallowed logic as needed.
Code:
local function writeallowed(slaveid)
if slaveid == 1 then
return grp.getvalue('1/1/1')
elseif slaveid == 2 then
return grp.getvalue('1/1/2')
end
end
handlers.writeregister = function(slaveid, fncode, data)
if #data ~= 4 then
return
end
local addr = touint16(data, 1)
local map = getmapping(slaveid, fncode)
if not map or not map[ addr ] then
return excodes.illegaldataaddress
end
local mapobj = map[ addr ]
if mapobj.len ~= 1 then
return excodes.illegaldataaddress
end
if writeallowed(slaveid) then
writevalue(mapobj, data, 3)
end
return data
end
Replace the whole handlers.writeregisters function with this.
Code:
handlers.writeregisters = function(slaveid, fncode, data)
if #data < 5 then
return
end
local addr = touint16(data, 1)
local count = touint16(data, 3)
local bytes = touint8(data, 5)
if #data ~= (bytes + 5) then
return
end
if count == 0 or count > limits.writeregisters or bytes ~= count * 2 then
return excodes.illegaldataaddress
end
local map = getmapping(slaveid, fncode)
if not map then
return excodes.illegaldataaddress
end
if writeallowed(slaveid) then
for i = 0, (count - 1) do
local mapobj = map[ addr + i ]
if mapobj and not mapobj.readonly then
local offset = 6 + i * 2
writevalue(mapobj, data, offset)
end
end
end
return data:sub(1, 4)
end