lua-libmodbus icon indicating copy to clipboard operation
lua-libmodbus copied to clipboard

Idea: get_32/64 routines to work on table/offsets as well as components

Open karlp opened this issue 5 years ago • 1 comments

Currently, the helpers to get/set 32/64 bit values from 16bit registers expect to get each register as a parameter.

regs = read_registers(0x2000, 20)
local number = mb.get_s64(regs[10],regs[11], regs[12], regs[13])

when pulling out multiple numbers, it might be nice to support indexing a register table instead? Could automatically attempt to do the right thing based on the type of the first argument?

regs = read_registers(0x2000, 20)
local number = mb.get_s64(regs, 10)

karlp avatar Oct 08 '20 13:10 karlp

How about adding a separate metatable to handle decoding the data from the registers? Similar to how it works in pyModbus?

Something along the line of;

local mb = require("libmodbus")

-- Perform a request to get some register values

local decoder = mb.new_decoder(register_values)
print(decoder.get_float32_abcd()) -- 1234.456
print(decoder.get_uint32()) -- 1000

The idea is to have the decoder keep track of the current offset in the register_values table.

To handle encoding, the reverse would apply.

local mb = require("libmodbus")

local encoder = mb.new_encoder()
encoder.add_float32_abcd(1234.456)
encoder.add_uint16(1000)
encoder.add_int32(2000)
print(encoder.registers()) -- or encoder.build()

ganehag avatar Jun 22 '23 03:06 ganehag