27.03.2024, 16:57 (This post was last modified: 27.03.2024, 17:13 by djaval.)
(26.02.2024, 09:08)Daniel Wrote: This is how it meant to work. You need to convert it back to float on master device.
mb.setfloat16precision(2) sets how float16 is converted to integer. By default it's 2 decimals (0.01 precision). Change 2 to 0 to convert to integer without any decimals.
Hi! Yes, it works. Not a big problem to convert back to float on master device, but just to clarify. Is it possible to make pure float register (either it can be little or big endian)?
I am Using Daikin Ac and Ac Guy Given me address and holding register ID. So query is that when I'm triggering from Logic Machine not getting response from AC. even i used json file.
We have 5 Zones in our project and each zone has an Automation DB having KNX Actuators and a KNX keypad.
So now we have to implement Auto/Manual logic in that way that if we press the button on KNX keypad the DB will go on the manual mode, it means now we are only able to control the DB devices (KNX Actuators) with keypad only and no command can be executed from the SCADA system until we remove the Manual mode by pressing the Auto mode key on the KNX keypad.
This Auto/Manual logic is to be implemented for all the 5 Zones.
Is it possible to make different resident script for different zone in MODBUS RTU so that we can disable the communication of particular zone by just disabling the resident script of that particular zone. We are using one LM5 as MODBUS RTU Slave for all 5 Zones.
Hi, I have done the changes in the mbslave (user library script). But still I didn't understand how will I disable one Zone to communicate with the SCADA over modbus rtu. Do I need to make some changes in resident script as well. Please guide.
(18.04.2025, 15:16)imprashant Wrote: Hi, I have done the changes in the mbslave (user library script). But still I didn't understand how will I disable one Zone to communicate with the SCADA over modbus rtu. Do I need to make some changes in resident script as well. Please guide.
Previously the script I was using was working but sometimes I have to send command 2-3 times from Modscan in order to execute command in LM5. The script I was using:
local mb = require('user.mbslave')
local mbrtu = require('luamodbus').rtu()
while true do
local val1 = grp.getvalue(ga1)
local val2 = grp.getvalue(ga2)
if not val1 and current_mapping ~= "mapping1" then
mb.setmapping(mapping1)
current_mapping = "mapping1"
log("Applied Mapping 1")
elseif not val2 and current_mapping ~= "mapping2" then
mb.setmapping(mapping2)
current_mapping = "mapping2"
log("Applied Mapping 2")
end
mb.rtuhandler(mbrtu)
os.sleep(1) -- small delay to prevent CPU hogging
end
The previous solution is meant to be used with multiple slave IDs. Writing to slave ID 1 is allowed when 1/1/1 is true, writing to slave ID 2 is allowed when 1/1/2 is true. writeallowed should be modified as needed.
Alternative solution is to add logic AND gates to mapped group addresses.
Updated user library:
Code:
local _M = {}
local byteswap, wordswap
local f16mult = 1
local f16float = false
local mapping = {}
local exception = {}
local reply = {}
local function touint8(buf, off)
return buf:byte(off)
end
local function touint16(buf, off, swap)
local b1, b2 = buf:byte(off, off + 1)
if swap then
b1, b2 = b2, b1
end
return b1 * 0x100 + b2
end
local function getmapping(slaveid, fncode)
local mapfncode = mapfncodes[ fncode ]
local slave = mapping[ slaveid ]
if not slave then
slave = mapping['*']
end
if slave then
return slave[ mapfncode ]
end
end
local function readbits(slaveid, fncode, data)
if #data ~= 4 then
return
end
local addr = touint16(data, 1)
local count = touint16(data, 3)
if count == 0 or count > limits.readbits then
return excodes.illegaldataaddress
elseif (addr + count) > maxaddr then
return excodes.illegaldataaddress
end
local map = getmapping(slaveid, fncode)
if not map then
return excodes.illegaldataaddress
end
local res = {}
local bits, byte = 0, 0
for i = 0, (count - 1) do
local mapaddr = map[ addr + i ]
if mapaddr then
local bval = grp.getvalue(mapaddr)
if toboolean(bval) then
byte = byte + bit.lshift(1, bits)
end
end
bits = bits + 1
if bits == 8 then
res[ #res + 1 ] = string.char(byte)
bits, byte = 0, 0
end
end
if bits ~= 0 then
res[ #res + 1 ] = string.char(byte)
end
return string.char(#res) .. table.concat(res)
end
local zeroreg = string.char(0, 0)
local function readvalue(res, value, dpt)
if dpt == dt.float16 then
if f16float then
dpt = dt.float32
else
value = value * f16mult
dpt = dt.int16
end
end
local enc = busdatatype.encode(value, dpt)
if not enc.dataraw then
return
end
local raw = enc.dataraw
if #raw % 2 == 1 then
local pad = value < 0 and 0xFF or 0
raw = string.char(pad) .. raw
end
local words = #raw / 2
local offset = #res
for i = 1, words do
local word = raw:sub(i * 2 - 1, i * 2)
if byteswap then
word = word:sub(2, 2) .. word:sub(1, 1)
end
if wordswap then
res[ offset + i ] = word
else
res[ offset + 1 + words - i ] = word
end
end
return true
end
local function readregisters(slaveid, fncode, data)
if #data ~= 4 then
return
end
local addr = touint16(data, 1)
local count = touint16(data, 3)
if count == 0 or count > limits.readregisters then
return excodes.illegaldataaddress
elseif (addr + count) > maxaddr then
return excodes.illegaldataaddress
end
local map = getmapping(slaveid, fncode)
if not map then
return excodes.illegaldataaddress
end
local res = {}
local max = count - 1
while #res <= max do
local mapobj = map[ addr + #res ]
local success
if mapobj then
local value = grp.getvalue(mapobj.address)
if type(value) == 'boolean' then
value = value and 1 or 0
end
if type(value) ~= 'number' then
alert('invalid value ' .. mapobj.address .. ' ' .. tostring(value))
value = 0
end
success = readvalue(res, value, mapobj.dpt)
end
if not success then
res[ #res + 1 ] = zeroreg
end
end
if #res ~= count then
return excodes.illegaldataaddress
end
return string.char(count * 2) .. table.concat(res)
end
local function writevalue(obj, data, offset)
local words = obj.len
local len = words * 2
local raw = data:sub(offset, offset + len - 1)
if #raw < len then
return
end
local buf = {}
for i = 1, words do
local word = touint16(raw, i * 2 - 1, byteswap)
local hex = string.format('%04X', word)
if wordswap then
buf[ #buf + 1 ] = hex
else
table.insert(buf, 1, hex)
end
end
local hexval = table.concat(buf)
local dpt = obj.dpt
if dpt == dt.float16 then
dpt = f16float and dt.float32 or dt.int16
end
local value = busdatatype.decode(hexval, dpt)
if obj.dpt == dt.float16 and not f16float then
value = value / f16mult
end
if not gate or grp.getvalue(gate) then
grp.write(addr, value, dt)
end
end
while true do
mb.rtuhandler(mbrtu)
end
In gates table key (left side) is mapped group address and value (right side) is controlling gate object. Write to the given mapped group address will only happen if the gate value is true. Writing to group addresses without a gate group address is always allowed.
Hi, Thanks for sharing. It is working fine for the coils i.e 1 bit object. But we have holding registers as well that we are using for scene control in which we are sending 0,1 and 2 value from the same group address. How in the above script we can use gate function for the holding registers.