22.02.2018, 07:38
More universal solution:
Code:
require('luamodbus')
mb = luamodbus.tcp()
mb:open('192.168.2.1', 502)
mb:connect()
function readstring(mb, address, length)
local data = { mb:readregisters(address, length) }
local bytes = {}
for _, reg in ipairs(data) do
if type(reg) == 'number' then
-- high 8 bits
local b1 = bit.band(bit.rshift(reg, 8), 0xFF)
if b1 > 0 then
bytes[ #bytes + 1 ] = b1
end
-- low 8 bits
local b2 = bit.band(reg, 0xFF)
if b2 > 0 then
bytes[ #bytes + 1 ] = b2
end
end
end
-- convert table of byte values to string
return string.char(unpack(bytes))
end
result = readstring(mb, 71, 16)
log(result)
mb:close()