Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
karlp opened this issue Oct 8, 2020 · 1 comment
Open

Comments

@karlp
Copy link
Member

karlp commented Oct 8, 2020

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)
@ganehag
Copy link

ganehag commented Jun 22, 2023

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()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants