22.02.2021, 08:02
There's no "read single register" function in modbus, this only applies to writing where there are two different commands for single/multiple registers.
Keep in mind that voltage registers are 32-bit so each value consists of 2 registers. This might be why profile was not working for you.
Another thing is that you should not close the connection after each run. Profiles also support this by enabling "persistent" connection mode.
See if this works for you:
Keep in mind that voltage registers are 32-bit so each value consists of 2 registers. This might be why profile was not working for you.
Another thing is that you should not close the connection after each run. Profiles also support this by enabling "persistent" connection mode.
See if this works for you:
Code:
if not mb then
require('luamodbus')
mb = luamodbus.tcp()
mb:open('10.36.0.25', 502)
mb:setresponsetimeout(5)
res, err = mb:connect()
if not res then
log('modbus connect failed ' .. tostring(err))
mb:close()
mb = nil
end
end
if mb then
reg10240, reg10241, reg10242, reg10243, reg10244, reg10245, reg10246, reg10247 = mb:readregisters(10240, 8)
if reg10240 then
mains_l1_voltage = reg10240 * 0x10000 + reg10241
mains_l2_voltage = reg10242 * 0x10000 + reg10243
mains_l3_voltage = reg10244 * 0x10000 + reg10245
genset_l1_voltage = reg10246 * 0x10000 + reg10247
log('voltage values', mains_l1_voltage, mains_l2_voltage, mains_l3_voltage, genset_l1_voltage)
else
log('modbus read failed ' .. tostring(reg10241))
mb:close()
mb = nil
end
end