ESPHome 2026.8.0-dev
Loading...
Searching...
No Matches
esphome::modbus::helpers Namespace Reference

Enumerations

enum class  SensorValueType : uint8_t {
  RAW = 0x00 , U_WORD = 0x1 , U_DWORD = 0x2 , S_WORD = 0x3 ,
  S_DWORD = 0x4 , BIT = 0x5 , U_DWORD_R = 0x6 , S_DWORD_R = 0x7 ,
  U_QWORD = 0x8 , S_QWORD = 0x9 , U_QWORD_R = 0xA , S_QWORD_R = 0xB ,
  FP32 = 0xC , FP32_R = 0xD
}
 

Functions

uint16_t server_frame_length (const uint8_t *frame, size_t size)
 
uint16_t client_frame_length (const uint8_t *frame, size_t size)
 
void log_unsupported_value_type (SensorValueType value_type)
 
std::optional< int64_t > payload_to_number (const uint8_t *data, size_t size, SensorValueType sensor_value_type, uint8_t offset, uint32_t bitmask)
 Convert a raw response payload to a number.
 
std::optional< int64_t > registers_to_number (const uint16_t *registers, size_t count, SensorValueType sensor_value_type)
 Reconstruct a number from register words (host byte order).
 
StaticVector< uint8_t, MAX_PDU_SIZE > create_client_pdu (ModbusFunctionCode function_code, uint16_t start_address, uint16_t number_of_entities, const uint8_t *values=nullptr, size_t values_len=0)
 Create a modbus clinet pdu for reading/writing single/multiple coils/register/inputs.
 
bool is_function_code_read (uint8_t function_code)
 
bool is_function_code_write (uint8_t function_code)
 
bool is_function_code_exception (uint8_t function_code)
 
bool is_function_code_custom (uint8_t function_code)
 
uint8_t server_frame_data_offset (const uint8_t *frame, size_t size)
 
uint8_t client_frame_data_offset (const uint8_t *, size_t)
 
bool value_type_is_float (SensorValueType v)
 
ModbusFunctionCode modbus_register_read_function (ModbusRegisterType reg_type)
 
ModbusFunctionCode modbus_register_write_function (ModbusRegisterType reg_type, bool multiple=false)
 
uint8_t c_to_hex (char c)
 
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_hex_str("1122", 0) returns 0x11.
 
uint16_t word_from_hex_str (const std::string &value, uint8_t pos)
 Get a word from a hex string.
 
uint32_t dword_from_hex_str (const std::string &value, uint8_t pos)
 Get a dword from a hex string.
 
uint64_t qword_from_hex_str (const std::string &value, uint8_t pos)
 Get a qword from a hex string.
 
template<typename T >
get_data (const uint8_t *data, size_t buffer_offset)
 Extract data from modbus response buffer.
 
template<typename T >
get_data (const std::vector< uint8_t > &data, size_t buffer_offset)
 
bool bit_from_packed (int bit, std::span< const uint8_t > data)
 Extract coil data from modbus response buffer Responses for coil are packed into bytes .
 
 ESPDEPRECATED ("Use bit_from_packed() instead. Removed in 2027.2.0", "2026.8.0") inline bool coil_from_vector(int coil
 
template<typename 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 values frrom bit 5 - 8.
 
template<typename Container >
void number_to_payload (Container &data, int64_t value, SensorValueType value_type)
 Append the Modbus register words for value to data.
 
std::optional< int64_t > payload_to_number (std::span< const uint8_t > data, SensorValueType sensor_value_type, uint8_t offset, uint32_t bitmask)
 Convert a response payload span to number; std::nullopt if the payload is too short.
 
 ESPDEPRECATED ("Use the std::span overload returning std::optional<int64_t> instead. Removed in 2027.2.0", "2026.8.0") inline int64_t payload_to_number(const std
 
std::vector< uint16_t > float_to_payload (float value, SensorValueType value_type)
 

Variables

std::span< const uint8_t > data { return bit_from_packed(coil, data)
 

Enumeration Type Documentation

◆ SensorValueType

enum class esphome::modbus::helpers::SensorValueType : uint8_t
strong
Enumerator
RAW 
U_WORD 
U_DWORD 
S_WORD 
S_DWORD 
BIT 
U_DWORD_R 
S_DWORD_R 
U_QWORD 
S_QWORD 
U_QWORD_R 
S_QWORD_R 
FP32 
FP32_R 

Definition at line 66 of file modbus_helpers.h.

Function Documentation

◆ bit_from_packed()

bool esphome::modbus::helpers::bit_from_packed ( int bit,
std::span< const uint8_t > data )
inline

Extract coil data from modbus response buffer Responses for coil are packed into bytes .

coil 3 is bit 3 of the first response byte coil 9 is bit 2 of the second response byte

Parameters
coilnumber of the cil
datamodbus response buffer (uint8_t)
Returns
content of coil register

Definition at line 202 of file modbus_helpers.h.

◆ byte_from_hex_str()

uint8_t esphome::modbus::helpers::byte_from_hex_str ( const std::string & value,
uint8_t pos )
inline

Get a byte from a hex string byte_from_hex_str("1122", 1) returns uint_8 value 0x22 == 34 byte_from_hex_str("1122", 0) returns 0x11.

Parameters
valuestring containing hex encoding
positionoffset in bytes. Because each byte is encoded in 2 hex digits the position of the original byte in the hex string is byte_pos * 2
Returns
byte value

Definition at line 126 of file modbus_helpers.h.

◆ c_to_hex()

uint8_t esphome::modbus::helpers::c_to_hex ( char c)
inline

Definition at line 116 of file modbus_helpers.h.

◆ client_frame_data_offset()

uint8_t esphome::modbus::helpers::client_frame_data_offset ( const uint8_t * ,
size_t  )
inline

Definition at line 64 of file modbus_helpers.h.

◆ client_frame_length()

uint16_t esphome::modbus::helpers::client_frame_length ( const uint8_t * frame,
size_t size )

Definition at line 46 of file modbus_helpers.cpp.

◆ create_client_pdu()

StaticVector< uint8_t, MAX_PDU_SIZE > esphome::modbus::helpers::create_client_pdu ( ModbusFunctionCode function_code,
uint16_t start_address,
uint16_t number_of_entities,
const uint8_t * values = nullptr,
size_t values_len = 0 )

Create a modbus clinet pdu for reading/writing single/multiple coils/register/inputs.

Parameters
function_codethe modbus function code to use. One of: READ_COILS READ_DISCRETE_INPUTS READ_HOLDING_REGISTERS READ_INPUT_REGISTERS WRITE_SINGLE_COIL WRITE_SINGLE_REGISTER WRITE_MULTIPLE_COILS WRITE_MULTIPLE_REGISTERS
start_addresscoil/register/input starting address
number_of_entitiesnumber of coils/registers/inputs to read/write
valuesoptional payload bytes to write (nullptr for read commands)
values_lenlength of values array
Returns
PDU (function code + data, no address, no CRC)

Definition at line 200 of file modbus_helpers.cpp.

◆ dword_from_hex_str()

uint32_t esphome::modbus::helpers::dword_from_hex_str ( const std::string & value,
uint8_t pos )
inline

Get a dword from a hex string.

Parameters
valuestring containing hex encoding
positionoffset in bytes. Because each byte is encoded in 2 hex digits the position of the original byte in the hex string is byte_pos * 2
Returns
dword value

Definition at line 148 of file modbus_helpers.h.

◆ ESPDEPRECATED() [1/2]

esphome::modbus::helpers::ESPDEPRECATED ( "Use bit_from_packed() instead. Removed in 2027.2.0" ,
"2026.8.0"  )

◆ ESPDEPRECATED() [2/2]

esphome::modbus::helpers::ESPDEPRECATED ( "Use the std::span overload returning std::optional<int64_t> instead. Removed in 2027.2.0" ,
"2026.8.0"  ) const

Definition at line 295 of file modbus_helpers.h.

◆ float_to_payload()

std::vector< uint16_t > esphome::modbus::helpers::float_to_payload ( float value,
SensorValueType value_type )
inline

Definition at line 331 of file modbus_helpers.h.

◆ get_data() [1/2]

template<typename T >
T esphome::modbus::helpers::get_data ( const std::vector< uint8_t > & data,
size_t buffer_offset )

Definition at line 190 of file modbus_helpers.h.

◆ get_data() [2/2]

template<typename T >
T esphome::modbus::helpers::get_data ( const uint8_t * data,
size_t buffer_offset )

Extract data from modbus response buffer.

Parameters
Tone of supported integer data types int_8,int_16,int_32,int_64
datamodbus response buffer (uint8_t)
buffer_offsetoffset in bytes.
Returns
value of type T extracted from buffer

Definition at line 169 of file modbus_helpers.h.

◆ is_function_code_custom()

bool esphome::modbus::helpers::is_function_code_custom ( uint8_t function_code)
inline

Definition at line 34 of file modbus_helpers.h.

◆ is_function_code_exception()

bool esphome::modbus::helpers::is_function_code_exception ( uint8_t function_code)
inline

Definition at line 30 of file modbus_helpers.h.

◆ is_function_code_read()

bool esphome::modbus::helpers::is_function_code_read ( uint8_t function_code)
inline

Definition at line 14 of file modbus_helpers.h.

◆ is_function_code_write()

bool esphome::modbus::helpers::is_function_code_write ( uint8_t function_code)
inline

Definition at line 22 of file modbus_helpers.h.

◆ log_unsupported_value_type()

void esphome::modbus::helpers::log_unsupported_value_type ( SensorValueType value_type)

Definition at line 104 of file modbus_helpers.cpp.

◆ mask_and_shift_by_rightbit()

template<typename N >
N esphome::modbus::helpers::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 values frrom bit 5 - 8.

the result is then shifted right by the position if the first right set bit in the mask Useful for modbus data where more than one value is packed in a 16 bit register Example: on Epever the "Length of night" register 0x9065 encodes values of the whole night length of time as D15 - D8 = hour, D7 - D0 = minute To get the hours use mask 0xFF00 and 0x00FF for the minute

Parameters
dataan integral value between 16 aand 32 bits,
bitmaskthe bitmask to apply

Definition at line 221 of file modbus_helpers.h.

◆ modbus_register_read_function()

ModbusFunctionCode esphome::modbus::helpers::modbus_register_read_function ( ModbusRegisterType reg_type)
inline

Definition at line 87 of file modbus_helpers.h.

◆ modbus_register_write_function()

ModbusFunctionCode esphome::modbus::helpers::modbus_register_write_function ( ModbusRegisterType reg_type,
bool multiple = false )
inline

Definition at line 102 of file modbus_helpers.h.

◆ number_to_payload()

template<typename Container >
void esphome::modbus::helpers::number_to_payload ( Container & data,
int64_t value,
SensorValueType value_type )

Append the Modbus register words for value to data.

Works with any container exposing push_back(uint16_t) (e.g. std::vector or StaticVector).

Definition at line 239 of file modbus_helpers.h.

◆ payload_to_number() [1/2]

std::optional< int64_t > esphome::modbus::helpers::payload_to_number ( const uint8_t * data,
size_t size,
SensorValueType sensor_value_type,
uint8_t offset,
uint32_t bitmask )

Convert a raw response payload to a number.

Parameters
datapayload with the data to convert
sizenumber of bytes available in data
sensor_value_typedefines if 16/32/64 bits or FP32 is used
offsetoffset to the data in data
bitmaskbitmask used for masking and shifting
Returns
64-bit number of the payload

Definition at line 108 of file modbus_helpers.cpp.

◆ payload_to_number() [2/2]

std::optional< int64_t > esphome::modbus::helpers::payload_to_number ( std::span< const uint8_t > data,
SensorValueType sensor_value_type,
uint8_t offset,
uint32_t bitmask )
inline

Convert a response payload span to number; std::nullopt if the payload is too short.

Definition at line 289 of file modbus_helpers.h.

◆ qword_from_hex_str()

uint64_t esphome::modbus::helpers::qword_from_hex_str ( const std::string & value,
uint8_t pos )
inline

Get a qword from a hex string.

Parameters
valuestring containing hex encoding
positionoffset in bytes. Because each byte is encoded in 2 hex digits the position of the original byte in the hex string is byte_pos * 2
Returns
qword value

Definition at line 158 of file modbus_helpers.h.

◆ registers_to_number()

std::optional< int64_t > esphome::modbus::helpers::registers_to_number ( const uint16_t * registers,
size_t count,
SensorValueType sensor_value_type )

Reconstruct a number from register words (host byte order).

Inverse of number_to_payload. Decodes the value at the start of the given span; advance the pointer to read successive values.

Parameters
registersregister values in host byte order
countnumber of registers available in registers
sensor_value_typedefines if 16/32/64 bits or FP32 is used
Returns
64-bit number of the registers

Definition at line 178 of file modbus_helpers.cpp.

◆ server_frame_data_offset()

uint8_t esphome::modbus::helpers::server_frame_data_offset ( const uint8_t * frame,
size_t size )
inline

Definition at line 50 of file modbus_helpers.h.

◆ server_frame_length()

uint16_t esphome::modbus::helpers::server_frame_length ( const uint8_t * frame,
size_t size )

Definition at line 10 of file modbus_helpers.cpp.

◆ value_type_is_float()

bool esphome::modbus::helpers::value_type_is_float ( SensorValueType v)
inline

Definition at line 83 of file modbus_helpers.h.

◆ word_from_hex_str()

uint16_t esphome::modbus::helpers::word_from_hex_str ( const std::string & value,
uint8_t pos )
inline

Get a word from a hex string.

Parameters
valuestring containing hex encoding
positionoffset in bytes. Because each byte is encoded in 2 hex digits the position of the original byte in the hex string is byte_pos * 2
Returns
word value

Definition at line 138 of file modbus_helpers.h.

Variable Documentation

◆ data

std::span<const uint8_t> esphome::modbus::helpers::data { return bit_from_packed(coil, data)

Definition at line 209 of file modbus_helpers.h.