I'm using a json profile to read modbus registers from a 4g router.
Reading normal values work perfect but I didn't find a solution how to read out 16 registers and convert it to string format.
I tried your code, but unfortunately I got no result anywhere. "log(result)" serves nothing! It looks like the code stops at the (in homelynk unknown ?) function called "lmcore.inttostr()" ???
I read out the registers 71 to 87 which should contain the router name: "RUT955" and logged the variables (r1 to r16) directly and this is the current log
function twobytetochars(value)
charsvalue = lmcore.hextostr(lmcore.inttohex(value,2))
return charsvalue
end
if r1 then r1 = twobytetochars(r1) else r1 = "" end
if r2 then r2 = twobytetochars(r2) else r2 = "" end
if r3 then r3 = twobytetochars(r3) else r3 = "" end
if r4 then r4 = twobytetochars(r4) else r4 = "" end
if r5 then r5 = twobytetochars(r5) else r5 = "" end
if r6 then r6 = twobytetochars(r6) else r6 = "" end
if r7 then r7 = twobytetochars(r7) else r7 = "" end
if r8 then r8 = twobytetochars(r8) else r8 = "" end
if r9 then r9 = twobytetochars(r9) else r9 = "" end
if r10 then r10 = twobytetochars(r10) else r10 = "" end
if r11 then r11 = twobytetochars(r11) else r11 = "" end
if r12 then r12 = twobytetochars(r12) else r12 = "" end
if r13 then r13 = twobytetochars(r13) else r13 = "" end
if r14 then r14 = twobytetochars(r14) else r14 = "" end
if r15 then r15 = twobytetochars(r15) else r15 = "" end
if r16 then r16 = twobytetochars(r16) else r16 = "" end
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