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

Typedefs

template<SensorValueType VALUE_TYPE>
using RegisterValueType = decltype(registers_to_value<VALUE_TYPE>(static_cast<const uint16_t *>(nullptr)))
 The type registers_to_value() yields for a given value type.
 
using PduBuffer = StaticVector<uint8_t, MAX_PDU_SIZE>
 
using ReadPdu = StaticVector<uint8_t, READ_PDU_SIZE>
 
using WriteSinglePdu = StaticVector<uint8_t, WRITE_SINGLE_PDU_SIZE>
 
using WriteFewRegistersPdu = StaticVector<uint8_t, WRITE_MULTIPLE_HEADER_SIZE + 2 * MAX_FEW_REGISTERS>
 
using CoilPackBuffer = StaticVector<uint8_t, packed_bit_bytes(MAX_NUM_OF_COILS_TO_WRITE)>
 Scratch space for packing coils into wire layout: one bit per coil, sized for the spec maximum.
 

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 , U_WORD_S = 0xE , S_WORD_S = 0xF
}
 

Functions

uint16_t server_pdu_length (const uint8_t *frame, size_t size)
 
uint16_t client_pdu_length (const uint8_t *frame, size_t size)
 
bool is_server_pdu_standard (const uint8_t *pdu, size_t size)
 
bool is_client_pdu_standard (const uint8_t *pdu, 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).
 
ReadPdu create_read_pdu (FunctionCode function_code, uint16_t start_address, uint16_t number_of_entities)
 Create a modbus read request PDU.
 
PduBuffer create_client_pdu (FunctionCode function_code, uint16_t start_address, uint16_t number_of_entities, const uint8_t *values=nullptr, size_t values_len=0)
 Create a modbus client pdu for reading/writing single/multiple coils/register/inputs.
 
PduBuffer create_write_registers_pdu (uint16_t start_address, std::span< const uint16_t > values)
 Create modbus write multiple registers command Function 0x10 Write Multiple Registers.
 
WriteFewRegistersPdu create_write_few_registers_pdu (uint16_t start_address, std::span< const uint16_t > values)
 Create modbus write multiple registers command (function 0x10) on a right-sized stack buffer.
 
PduBuffer create_read_write_multiple_registers_pdu (uint16_t read_start_address, uint16_t read_count, uint16_t write_start_address, std::span< const uint16_t > write_values)
 Create modbus read/write multiple registers command Function 0x17 Read/Write Multiple Registers Writes write_values then reads read_count registers in one transaction (write first, per Modbus 6.17); the response carries only the read registers.
 
WriteSinglePdu create_write_single_register_pdu (uint16_t start_address, uint16_t value)
 Create modbus write single register command Function 0x06 Write Single Register.
 
WriteSinglePdu create_write_single_coil_pdu (uint16_t address, bool value)
 Create modbus write single coil command Function 0x05 Write Single Coil.
 
PduBuffer create_write_coils_pdu (uint16_t start_address, PackedBits bits)
 Create modbus write multiple coils command (function 0x0F) from bits packed as on the wire.
 
PduBuffer create_write_coils_pdu (uint16_t start_address, std::span< const bool > values)
 Create modbus write multiple coils command Function 0x0F Write Multiple Coils.
 
PduBuffer create_write_coils_pdu (uint16_t start_address, const std::vector< bool > &values)
 Create modbus write multiple coils command (function 0x0F) from a std::vector<bool>.
 
bool is_function_code_read_only (uint8_t function_code)
 
bool is_function_code_read (uint8_t function_code)
 
bool is_function_code_write (uint8_t function_code)
 
bool address_range_fits (uint16_t start_address, size_t count)
 
bool is_function_code_exception (uint8_t function_code)
 
bool is_function_code_custom (uint8_t function_code)
 
bool is_function_code_unknown_length (uint8_t function_code)
 True for any function code whose frame length the parsers cannot predict - everything the server_pdu_length()/client_pdu_length() switches fall through to default on (keep the case list in step with those switches).
 
bool is_function_code_broadcastable (uint8_t function_code)
 True when the underlying function code (exception bit masked off) may be broadcast (address 0).
 
uint16_t server_frame_length (const uint8_t *frame, size_t size)
 
uint16_t client_frame_length (const uint8_t *frame, size_t size)
 
 ESPDEPRECATED ("Use server_pdu_payload() on the response PDU instead. Removed in 2027.2.0", "2026.8.0") inline uint8_t server_frame_data_offset(const uint8_t *frame
 
 switch (static_cast< FunctionCode >(frame[1]))
 
std::span< const uint8_t > server_pdu_payload (std::span< const uint8_t > pdu)
 Returns the payload portion of a server response PDU: the bytes after the function code, and for the read responses (0x01-0x04 and 0x17) also after the byte-count byte.
 
uint8_t client_frame_data_offset (const uint8_t *, size_t)
 
template<typename T >
get_data (const uint8_t *data, size_t buffer_offset)
 Extract data from modbus response buffer.
 
uint8_t pdu_function_code (std::span< const uint8_t > pdu)
 Function code of a PDU, exception flag masked; 0 for an empty PDU.
 
std::optional< uint16_t > client_pdu_start_address (std::span< const uint8_t > pdu)
 Start address of a standard client request PDU ([fc, addr_hi, addr_lo, ...]).
 
bool value_type_is_float (SensorValueType v)
 
constexpr uint16_t register_width_for (SensorValueType v)
 Number of 16-bit registers a value of this type occupies (RAW counts as one register).
 
bool is_entity_type_binary (EntityType type)
 Coils and discrete inputs are the bit-addressed entity tables; the other types are 16-bit registers.
 
FunctionCode modbus_register_read_function (EntityType reg_type)
 
FunctionCode modbus_register_write_function (EntityType 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 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 Out , typename Bits >
void pack_bits (Out &out, const Bits &bits)
 Append packed bytes (LSB first) for the given bits onto a growable byte container.
 
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
 
constexpr uint32_t registers_to_uint32 (uint16_t high_word, uint16_t low_word)
 Combine two register words into a 32-bit value.
 
constexpr uint64_t registers_to_uint64 (uint16_t word0, uint16_t word1, uint16_t word2, uint16_t word3)
 Combine four register words into a 64-bit value, most significant word first.
 
template<SensorValueType VALUE_TYPE>
constexpr auto registers_to_value (const uint16_t *registers)
 Decode one value whose type is known at compile time, from registers in host byte order.
 
template<SensorValueType VALUE_TYPE>
constexpr std::optional< RegisterValueType< VALUE_TYPE > > value_at (std::span< const uint16_t > registers, uint16_t start_address, uint16_t address)
 The value stored at an absolute register address, or nullopt when it is not wholly inside this response.
 
template<typename Container >
void float_to_payload (Container &data, float value, SensorValueType value_type)
 Append a float converted to register words to any push_back container (heap-free with StaticVector).
 
 ESPDEPRECATED ("Use the container overload of float_to_payload() instead. Removed in 2027.2.0", "2026.8.0") inline std
 

Variables

size_t size
 
std::span< const uint8_t > data { return bit_from_packed(coil, data)
 
template<SensorValueType >
constexpr bool VALUE_TYPE_SUPPORTED = false
 

Typedef Documentation

◆ CoilPackBuffer

using esphome::modbus::helpers::CoilPackBuffer = StaticVector<uint8_t, packed_bit_bytes(MAX_NUM_OF_COILS_TO_WRITE)>

Scratch space for packing coils into wire layout: one bit per coil, sized for the spec maximum.

Definition at line 567 of file modbus_helpers.h.

◆ PduBuffer

using esphome::modbus::helpers::PduBuffer = StaticVector<uint8_t, MAX_PDU_SIZE>

Definition at line 562 of file modbus_helpers.h.

◆ ReadPdu

using esphome::modbus::helpers::ReadPdu = StaticVector<uint8_t, READ_PDU_SIZE>

Definition at line 563 of file modbus_helpers.h.

◆ RegisterValueType

template<SensorValueType VALUE_TYPE>
using esphome::modbus::helpers::RegisterValueType = decltype(registers_to_value<VALUE_TYPE>(static_cast<const uint16_t *>(nullptr)))

The type registers_to_value() yields for a given value type.

Distinct from modbus::RegisterValues, which is a container of raw words.

Definition at line 537 of file modbus_helpers.h.

◆ WriteFewRegistersPdu

using esphome::modbus::helpers::WriteFewRegistersPdu = StaticVector<uint8_t, WRITE_MULTIPLE_HEADER_SIZE + 2 * MAX_FEW_REGISTERS>

Definition at line 565 of file modbus_helpers.h.

◆ WriteSinglePdu

using esphome::modbus::helpers::WriteSinglePdu = StaticVector<uint8_t, WRITE_SINGLE_PDU_SIZE>

Definition at line 564 of file modbus_helpers.h.

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 
U_WORD_S 
S_WORD_S 

Definition at line 208 of file modbus_helpers.h.

Function Documentation

◆ address_range_fits()

bool esphome::modbus::helpers::address_range_fits ( uint16_t start_address,
size_t count )
inline

Definition at line 42 of file modbus_helpers.h.

◆ 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
bitindex of the bit to extract
datamodbus response buffer (uint8_t)
Returns
value of the requested bit

Definition at line 340 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
posoffset 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 295 of file modbus_helpers.h.

◆ c_to_hex()

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

Definition at line 285 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 162 of file modbus_helpers.h.

◆ client_frame_length()

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

Definition at line 114 of file modbus_helpers.h.

◆ client_pdu_length()

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

Definition at line 62 of file modbus_helpers.cpp.

◆ client_pdu_start_address()

std::optional< uint16_t > esphome::modbus::helpers::client_pdu_start_address ( std::span< const uint8_t > pdu)
inline

Start address of a standard client request PDU ([fc, addr_hi, addr_lo, ...]).

Empty when the PDU is too short or its function code has no known layout - custom-frame bytes are not misread as an address.

Definition at line 198 of file modbus_helpers.h.

◆ create_client_pdu()

PduBuffer esphome::modbus::helpers::create_client_pdu ( FunctionCode function_code,
uint16_t start_address,
uint16_t number_of_entities,
const uint8_t * values = nullptr,
size_t values_len = 0 )

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

Generic entry point; prefer the direction- and type-specific builders (create_read_pdu(), create_write_registers_pdu(), create_write_single_register_pdu(), create_write_coils_pdu(), create_write_single_coil_pdu()) which bound their inputs per spec.

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 413 of file modbus_helpers.cpp.

◆ create_read_pdu()

ReadPdu esphome::modbus::helpers::create_read_pdu ( FunctionCode function_code,
uint16_t start_address,
uint16_t number_of_entities )

Create a modbus read request PDU.

Parameters
function_codeone of READ_COILS, READ_DISCRETE_INPUTS, READ_HOLDING_REGISTERS, READ_INPUT_REGISTERS
start_addresscoil/register/input starting address
number_of_entitiesnumber of coils/registers/inputs to read
Returns
PDU (function code + data, no address, no CRC); empty on invalid input

Definition at line 369 of file modbus_helpers.cpp.

◆ create_read_write_multiple_registers_pdu()

PduBuffer esphome::modbus::helpers::create_read_write_multiple_registers_pdu ( uint16_t read_start_address,
uint16_t read_count,
uint16_t write_start_address,
std::span< const uint16_t > write_values )

Create modbus read/write multiple registers command Function 0x17 Read/Write Multiple Registers Writes write_values then reads read_count registers in one transaction (write first, per Modbus 6.17); the response carries only the read registers.

Parameters
read_start_addressmodbus address of the first register to read back
read_countnumber of registers to read (at most MAX_NUM_OF_REGISTERS_TO_READ)
write_start_addressmodbus address of the first register to write
write_valuesregister values to write; the register count is write_values.size() (at most MAX_NUM_OF_REGISTERS_TO_WRITE_RW). Any contiguous uint16_t container converts.
Returns
PDU (function code + data, no address, no CRC); an empty PDU on any out-of-range input

Definition at line 544 of file modbus_helpers.cpp.

◆ create_write_coils_pdu() [1/3]

PduBuffer esphome::modbus::helpers::create_write_coils_pdu ( uint16_t start_address,
const std::vector< bool > & values )

Create modbus write multiple coils command (function 0x0F) from a std::vector<bool>.

Prefer the span overload above whenever the coils are already in contiguous storage - a std::array<bool, N> or any other contiguous bool container converts to it. This overload exists only because std::vector<bool> is bit-packed and so cannot convert to a span; without it every caller holding one re-implements the packing.

Parameters
start_addressmodbus address of the first coil to write
valuescoil values to write; the coil count is values.size() (at most MAX_NUM_OF_COILS_TO_WRITE, an over-long set is rejected and an empty PDU is returned)
Returns
PDU (function code + data, no address, no CRC)

Definition at line 639 of file modbus_helpers.cpp.

◆ create_write_coils_pdu() [2/3]

PduBuffer esphome::modbus::helpers::create_write_coils_pdu ( uint16_t start_address,
PackedBits bits )

Create modbus write multiple coils command (function 0x0F) from bits packed as on the wire.

Parameters
start_addressmodbus address of the first coil to write
bitsPackedBits view of the coils to write (at most MAX_NUM_OF_COILS_TO_WRITE); invalid input returns an empty PDU
Returns
PDU (function code + data, no address, no CRC)

Definition at line 612 of file modbus_helpers.cpp.

◆ create_write_coils_pdu() [3/3]

PduBuffer esphome::modbus::helpers::create_write_coils_pdu ( uint16_t start_address,
std::span< const bool > values )

Create modbus write multiple coils command Function 0x0F Write Multiple Coils.

Parameters
start_addressmodbus address of the first coil to write
valuescoil values to write; the coil count is values.size() (at most MAX_NUM_OF_COILS_TO_WRITE, an over-long set is rejected and an empty PDU is returned)
Returns
PDU (function code + data, no address, no CRC)

Definition at line 635 of file modbus_helpers.cpp.

◆ create_write_few_registers_pdu()

WriteFewRegistersPdu esphome::modbus::helpers::create_write_few_registers_pdu ( uint16_t start_address,
std::span< const uint16_t > values )

Create modbus write multiple registers command (function 0x10) on a right-sized stack buffer.

Identical wire bytes to create_write_registers_pdu() for any accepted input.

Parameters
start_addressmodbus address of the first register to write
valuesregister values to write, at most MAX_FEW_REGISTERS (an over-long or empty set is rejected and an empty PDU is returned)
Returns
PDU (function code + data, no address, no CRC)

Definition at line 540 of file modbus_helpers.cpp.

◆ create_write_registers_pdu()

PduBuffer esphome::modbus::helpers::create_write_registers_pdu ( uint16_t start_address,
std::span< const uint16_t > values )

Create modbus write multiple registers command Function 0x10 Write Multiple Registers.

Parameters
start_addressmodbus address of the first register to write
valuesregister values to write; the register count is values.size() (at most MAX_NUM_OF_REGISTERS_TO_WRITE, an over-long set is rejected and an empty PDU is returned). Any contiguous uint16_t container converts (std::vector, std::array).
Returns
PDU (function code + data, no address, no CRC)

Definition at line 536 of file modbus_helpers.cpp.

◆ create_write_single_coil_pdu()

WriteSinglePdu esphome::modbus::helpers::create_write_single_coil_pdu ( uint16_t address,
bool value )

Create modbus write single coil command Function 0x05 Write Single Coil.

Parameters
addressmodbus address of the coil to write
valuecoil value to write
Returns
PDU (function code + data, no address, no CRC)

Definition at line 575 of file modbus_helpers.cpp.

◆ create_write_single_register_pdu()

WriteSinglePdu esphome::modbus::helpers::create_write_single_register_pdu ( uint16_t start_address,
uint16_t value )

Create modbus write single register command Function 0x06 Write Single Register.

Parameters
start_addressmodbus address of the register to write
valueuint16_t value to write
Returns
PDU (function code + data, no address, no CRC)

Definition at line 569 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
posoffset in bytes (see byte_from_hex_str)
Returns
dword value

Definition at line 315 of file modbus_helpers.h.

◆ ESPDEPRECATED() [1/4]

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

◆ ESPDEPRECATED() [2/4]

esphome::modbus::helpers::ESPDEPRECATED ( "Use server_pdu_payload() on the response PDU instead. Removed in 2027.2.0" ,
"2026.8.0"  ) const

◆ ESPDEPRECATED() [3/4]

esphome::modbus::helpers::ESPDEPRECATED ( "Use the container overload of float_to_payload() instead. Removed in 2027.2.0" ,
"2026.8.0"  )

Definition at line 695 of file modbus_helpers.h.

◆ ESPDEPRECATED() [4/4]

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 460 of file modbus_helpers.h.

◆ float_to_payload()

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

Append a float converted to register words to any push_back container (heap-free with StaticVector).

Parameters
datacontainer the register words are appended to
valuevalue to convert
value_typedefines if 16/32/64 bits or FP32 is used

Definition at line 682 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 328 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 170 of file modbus_helpers.h.

◆ is_client_pdu_standard()

bool esphome::modbus::helpers::is_client_pdu_standard ( const uint8_t * pdu,
size_t size )

Definition at line 138 of file modbus_helpers.cpp.

◆ is_entity_type_binary()

bool esphome::modbus::helpers::is_entity_type_binary ( EntityType type)
inline

Coils and discrete inputs are the bit-addressed entity tables; the other types are 16-bit registers.

Definition at line 252 of file modbus_helpers.h.

◆ is_function_code_broadcastable()

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

True when the underlying function code (exception bit masked off) may be broadcast (address 0).

Refused: the reads (including read-write), plus every other code whose response length the parser knows (file record, FIFO). Allowed: the writes, and any code the parser does not know, since the hub cannot tell one of those apart from a vendor write.

Definition at line 91 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 50 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 46 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 24 of file modbus_helpers.h.

◆ is_function_code_read_only()

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

Definition at line 15 of file modbus_helpers.h.

◆ is_function_code_unknown_length()

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

True for any function code whose frame length the parsers cannot predict - everything the server_pdu_length()/client_pdu_length() switches fall through to default on (keep the case list in step with those switches).

Deliberately wider than is_function_code_custom(): the user-defined ranges are unknown to the parser too, but so are the assigned-but-unimplemented codes (READ_EXCEPTION_STATUS, DIAGNOSTICS, GET_COMM_EVENT_*, REPORT_SERVER_ID) and every unassigned value. Exception-flagged codes (0x80 set) are always the 2-byte spec exception shape, so never unknown.

Definition at line 64 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 30 of file modbus_helpers.h.

◆ is_server_pdu_standard()

bool esphome::modbus::helpers::is_server_pdu_standard ( const uint8_t * pdu,
size_t size )

Definition at line 102 of file modbus_helpers.cpp.

◆ log_unsupported_value_type()

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

Definition at line 210 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 382 of file modbus_helpers.h.

◆ modbus_register_read_function()

FunctionCode esphome::modbus::helpers::modbus_register_read_function ( EntityType reg_type)
inline

Definition at line 256 of file modbus_helpers.h.

◆ modbus_register_write_function()

FunctionCode esphome::modbus::helpers::modbus_register_write_function ( EntityType reg_type,
bool multiple = false )
inline

Definition at line 271 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 400 of file modbus_helpers.h.

◆ pack_bits()

template<typename Out , typename Bits >
void esphome::modbus::helpers::pack_bits ( Out & out,
const Bits & bits )

Append packed bytes (LSB first) for the given bits onto a growable byte container.

push_back-based so callers can build a payload incrementally (e.g. a std::vector<uint8_t> with no fixed upper bound). A non-byte-aligned count appends n+1 bytes, the last holding the remaining bits in its low positions.

Parameters
outdestination byte container exposing push_back(uint8_t)
bitscontainer of bool exposing range-based iteration

Definition at line 356 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 214 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 454 of file modbus_helpers.h.

◆ pdu_function_code()

uint8_t esphome::modbus::helpers::pdu_function_code ( std::span< const uint8_t > pdu)
inline

Function code of a PDU, exception flag masked; 0 for an empty PDU.

Definition at line 192 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
posoffset in bytes (see byte_from_hex_str)
Returns
qword value

Definition at line 324 of file modbus_helpers.h.

◆ register_width_for()

uint16_t esphome::modbus::helpers::register_width_for ( SensorValueType v)
constexpr

Number of 16-bit registers a value of this type occupies (RAW counts as one register).

Definition at line 232 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 294 of file modbus_helpers.cpp.

◆ registers_to_uint32()

uint32_t esphome::modbus::helpers::registers_to_uint32 ( uint16_t high_word,
uint16_t low_word )
constexpr

Combine two register words into a 32-bit value.

Definition at line 477 of file modbus_helpers.h.

◆ registers_to_uint64()

uint64_t esphome::modbus::helpers::registers_to_uint64 ( uint16_t word0,
uint16_t word1,
uint16_t word2,
uint16_t word3 )
constexpr

Combine four register words into a 64-bit value, most significant word first.

Definition at line 482 of file modbus_helpers.h.

◆ registers_to_value()

template<SensorValueType VALUE_TYPE>
auto esphome::modbus::helpers::registers_to_value ( const uint16_t * registers)
constexpr

Decode one value whose type is known at compile time, from registers in host byte order.

Unlike registers_to_number(), the type is a template argument, so only the one decode is compiled and the caller gets the value's natural type back rather than an int64_t. The "_R" types take the low word first; the rest take the high word first. Supports every fixed-width type: the WORD, DWORD, QWORD and FP32 families, including their _S and _R forms. RAW and BIT have no fixed width and fail to compile. Use register_width_for() for the number of registers the caller must supply. Note that the FP32 branches are only usable in a constant expression where std::bit_cast is available; elsewhere bit_cast falls back to a non-constexpr memcpy (see core/helpers.h).

Definition at line 500 of file modbus_helpers.h.

◆ server_frame_length()

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

Definition at line 105 of file modbus_helpers.h.

◆ server_pdu_length()

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

Definition at line 24 of file modbus_helpers.cpp.

◆ server_pdu_payload()

std::span< const uint8_t > esphome::modbus::helpers::server_pdu_payload ( std::span< const uint8_t > pdu)
inline

Returns the payload portion of a server response PDU: the bytes after the function code, and for the read responses (0x01-0x04 and 0x17) also after the byte-count byte.

Response 0x14 also carries a byte-count byte, but that code is not implemented and its count byte is left in the payload. For an exception PDU the payload is the exception code byte (the read check must not see the masked function code, or an exception-of-read would classify as a read and return an empty span). Returns an empty span if the PDU is too short.

Definition at line 155 of file modbus_helpers.h.

◆ switch()

esphome::modbus::helpers::switch ( static_cast< FunctionCode > frame[1])

Definition at line 137 of file modbus_helpers.h.

◆ value_at()

template<SensorValueType VALUE_TYPE>
std::optional< RegisterValueType< VALUE_TYPE > > esphome::modbus::helpers::value_at ( std::span< const uint16_t > registers,
uint16_t start_address,
uint16_t address )
constexpr

The value stored at an absolute register address, or nullopt when it is not wholly inside this response.

Lets a device decode by address rather than by offset, so a poll split across several requests needs no extra bookkeeping: a value outside the response simply yields nullopt.

Parameters
registersthe response registers, in host byte order
start_addressthe address the response begins at
addressthe address of the wanted value

Definition at line 547 of file modbus_helpers.h.

◆ value_type_is_float()

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

Definition at line 227 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
posoffset in bytes (see byte_from_hex_str)
Returns
word value

Definition at line 306 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 347 of file modbus_helpers.h.

◆ size

size_t esphome::modbus::helpers::size
Initial value:
{
if (size < 2)
return 0

Definition at line 134 of file modbus_helpers.h.

◆ VALUE_TYPE_SUPPORTED

template<SensorValueType >
bool esphome::modbus::helpers::VALUE_TYPE_SUPPORTED = false
inlineconstexpr

Definition at line 488 of file modbus_helpers.h.