03.10.2016, 07:18
First, you should never access serial port directly if it's used by the Modbus mapper, it will lead to random errors.
It might be that read fails for some reason and returns nil which ends up being sent as 0.
Try this:
Another approach is to create a scheduled script which runs every X minutes (depending on frequently you want to resend values). Mark each required object with resend tag, tune interval value in your script as needed.
It might be that read fails for some reason and returns nil which ends up being sent as 0.
Try this:
Code:
result = mb:readregisters(26)
if result then
grp.write('2/3/7',result)
end
Another approach is to create a scheduled script which runs every X minutes (depending on frequently you want to resend values). Mark each required object with resend tag, tune interval value in your script as needed.
Code:
now = os.time()
interval = 120 -- resend time in seconds
-- go through all objects with "resend" tag
objects = grp.tag('resend')
for _, object in ipairs(objects) do
delta = now - object.updatetime
-- check if value must be sent again
if delta >= interval then
object:write(object.data)
end
end