21.12.2021, 13:49
Use bit.bxor() and don't use quotes around numbers because not all functions will convert strings to numbers automatically.
Code:
function encode(...)
local checksum = bit.bxor(...)
return string.char(...) .. string.char(checksum)
end
addrs = {0x01,0x03,0x05,0x07,0x09,0x0b,0x0d,0x0f,0x11,0x13,0x15,0x17,0x19,0x1b,0x1d,0x1f,0x21}
for _, addr in ipairs(addrs) do
command = encode(0x04,0x04,addr,0xd2)
reply = writetoport(port, 0.2, command)
decoded = str2hex(reply)
log('Device address: eA'.. i-1 .. ', Result: ' .. decoded)
end