-
Notifications
You must be signed in to change notification settings - Fork 368
Open
Description
If you have a structure and it is designed in such a way like the data coming via Modbus, you can copy all data or individual parts directly into the structure.
if u have a struct like
typedef struct{
uint16_t Voltage[3];
uint32_t Current[3];
int32_t PowerW[4];
}
with aktual code u have to do
Smartmeter.Current[0] = (node.getResponseBuffer(3)<<16) | node.getResponseBuffer(4);
Smartmeter.Current[1] = (node.getResponseBuffer(5)<<16) | node.getResponseBuffer(6);
Smartmeter.Current[2] = (node.getResponseBuffer(7)<<16) | node.getResponseBuffer(8);
with the template u can do this
getResponseInto(3, Smartmeter.Current);
or if all is perfect
getResponseInto(0, Smartmeter);
class function to add:
template <typename T>
void getResponseInto(uint8_t start_index, T& dest_ref) const {
constexpr size_t REG_SIZE = sizeof(uint16_t);
static_assert(sizeof(T) % REG_SIZE == 0 && sizeof(T) > 0, "Target type T must have a size that is a multiple of 16 bits (2 bytes).");
constexpr size_t REQUIRED_REGS = sizeof(T) / REG_SIZE;
if (start_index + REQUIRED_REGS > ku8MaxBufferSize) {
dest_ref = {};
return;
}
const uint16_t* p_source = _u16ResponseBuffer + start_index;
std::memcpy(&dest_ref, p_source, sizeof(T));
}
Metadata
Metadata
Assignees
Labels
No labels