62inline uint8_t
c_to_hex(
char c) {
return (c >=
'A') ? (c >=
'a') ? (c -
'a' + 10) : (c -
'A' + 10) : (c -
'0'); }
73 if (value.length() <
pos * 2 + 2)
115template<
typename T> T
get_data(
const std::vector<uint8_t> &data,
size_t buffer_offset) {
116 if (
sizeof(T) ==
sizeof(uint8_t)) {
117 return T(data[buffer_offset]);
119 if (
sizeof(T) ==
sizeof(uint16_t)) {
120 return T((uint16_t(data[buffer_offset + 0]) << 8) | (uint16_t(data[buffer_offset + 1]) << 0));
123 if (
sizeof(T) ==
sizeof(
uint32_t)) {
128 if (
sizeof(T) ==
sizeof(uint64_t)) {
133 static_assert(
sizeof(T) ==
sizeof(uint8_t) ||
sizeof(T) ==
sizeof(uint16_t) ||
sizeof(T) ==
sizeof(
uint32_t) ||
134 sizeof(T) ==
sizeof(uint64_t),
135 "Unsupported type size in get_data; only 1, 2, 4, or 8-byte integer types are supported.");
149 auto data_byte = coil / 8;
150 return (data[data_byte] & (1 << (coil % 8))) > 0;
164 auto result = (mask & data);
165 if (result == 0 || mask == 0xFFFFFFFF) {
168 for (
size_t pos = 0;
pos <
sizeof(N) << 3;
pos++) {
169 if (
pos < 32 && (mask & (1UL <<
pos)) != 0)
170 return result >>
pos;
199 val = llroundf(value);
202 std::vector<uint16_t> data;
ModbusFunctionCode modbus_register_read_function(ModbusRegisterType reg_type)
ModbusFunctionCode modbus_register_write_function(ModbusRegisterType reg_type)
bool value_type_is_float(SensorValueType v)
bool coil_from_vector(int coil, const std::vector< uint8_t > &data)
Extract coil data from modbus response buffer Responses for coil are packed into bytes .
std::vector< uint16_t > float_to_payload(float value, SensorValueType value_type)
T get_data(const std::vector< uint8_t > &data, size_t buffer_offset)
Extract data from modbus response buffer.
uint64_t qword_from_hex_str(const std::string &value, uint8_t pos)
Get a qword from a hex string.
N mask_and_shift_by_rightbit(N data, uint32_t mask)
Extract bits from value and shift right according to the bitmask if the bitmask is 0x00F0 we want the...
uint32_t dword_from_hex_str(const std::string &value, uint8_t pos)
Get a dword from a hex string.
uint8_t byte_from_hex_str(const std::string &value, uint8_t pos)
Get a byte from a hex string byte_from_hex_str("1122", 1) returns uint_8 value 0x22 == 34 byte_from_h...
uint16_t word_from_hex_str(const std::string &value, uint8_t pos)
Get a word from a hex string.
int64_t payload_to_number(const std::vector< uint8_t > &data, SensorValueType sensor_value_type, uint8_t offset, uint32_t bitmask)
Convert vector<uint8_t> response payload to number.
void number_to_payload(std::vector< uint16_t > &data, int64_t value, SensorValueType value_type)
Convert float value to vector<uint16_t> suitable for sending.
@ READ_WRITE_MULTIPLE_REGISTERS
To bit_cast(const From &src)
Convert data between types, without aliasing issues or undefined behaviour.