ESPHome 2026.9.0-dev
Loading...
Searching...
No Matches
modbus_helpers.h File Reference

Go to the source code of this file.

Namespaces

namespace  esphome
 
namespace  esphome::modbus
 
namespace  esphome::modbus::helpers
 

Typedefs

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.
 
using esphome::modbus::helpers::PduBuffer = StaticVector<uint8_t, MAX_PDU_SIZE>
 
using esphome::modbus::helpers::ReadPdu = StaticVector<uint8_t, READ_PDU_SIZE>
 
using esphome::modbus::helpers::WriteSinglePdu = StaticVector<uint8_t, WRITE_SINGLE_PDU_SIZE>
 
using esphome::modbus::helpers::WriteFewRegistersPdu = StaticVector<uint8_t, WRITE_MULTIPLE_HEADER_SIZE + 2 * MAX_FEW_REGISTERS>
 
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.
 

Enumerations

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

Functions

bool esphome::modbus::helpers::is_function_code_read_only (uint8_t function_code)
 
bool esphome::modbus::helpers::is_function_code_read (uint8_t function_code)
 
bool esphome::modbus::helpers::is_function_code_write (uint8_t function_code)
 
bool esphome::modbus::helpers::address_range_fits (uint16_t start_address, size_t count)
 
bool esphome::modbus::helpers::is_function_code_exception (uint8_t function_code)
 
bool esphome::modbus::helpers::is_function_code_custom (uint8_t function_code)
 
bool esphome::modbus::helpers::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 esphome::modbus::helpers::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 esphome::modbus::helpers::server_pdu_length (const uint8_t *frame, size_t size)
 
uint16_t esphome::modbus::helpers::server_frame_length (const uint8_t *frame, size_t size)
 
uint16_t esphome::modbus::helpers::client_pdu_length (const uint8_t *frame, size_t size)
 
uint16_t esphome::modbus::helpers::client_frame_length (const uint8_t *frame, size_t size)
 
bool esphome::modbus::helpers::is_server_pdu_standard (const uint8_t *pdu, size_t size)
 
bool esphome::modbus::helpers::is_client_pdu_standard (const uint8_t *pdu, size_t size)
 
 esphome::modbus::helpers::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
 
 esphome::modbus::helpers::switch (static_cast< FunctionCode >(frame[1]))
 
std::span< const uint8_t > esphome::modbus::helpers::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 esphome::modbus::helpers::client_frame_data_offset (const uint8_t *, size_t)
 
template<typename T >
esphome::modbus::helpers::get_data (const uint8_t *data, size_t buffer_offset)
 Extract data from modbus response buffer.
 
uint8_t esphome::modbus::helpers::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 > esphome::modbus::helpers::client_pdu_start_address (std::span< const uint8_t > pdu)
 Start address of a standard client request PDU ([fc, addr_hi, addr_lo, ...]).
 
bool esphome::modbus::helpers::value_type_is_float (SensorValueType v)
 
constexpr uint16_t esphome::modbus::helpers::register_width_for (SensorValueType v)
 Number of 16-bit registers a value of this type occupies (RAW counts as one register).
 
bool esphome::modbus::helpers::is_entity_type_binary (EntityType type)
 Coils and discrete inputs are the bit-addressed entity tables; the other types are 16-bit registers.
 
FunctionCode esphome::modbus::helpers::modbus_register_read_function (EntityType reg_type)
 
FunctionCode esphome::modbus::helpers::modbus_register_write_function (EntityType reg_type, bool multiple=false)
 
uint8_t esphome::modbus::helpers::c_to_hex (char c)
 
uint8_t esphome::modbus::helpers::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 esphome::modbus::helpers::word_from_hex_str (const std::string &value, uint8_t pos)
 Get a word from a hex string.
 
uint32_t esphome::modbus::helpers::dword_from_hex_str (const std::string &value, uint8_t pos)
 Get a dword from a hex string.
 
uint64_t esphome::modbus::helpers::qword_from_hex_str (const std::string &value, uint8_t pos)
 Get a qword from a hex string.
 
template<typename T >
esphome::modbus::helpers::get_data (const std::vector< uint8_t > &data, size_t buffer_offset)
 
bool esphome::modbus::helpers::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 .
 
 esphome::modbus::helpers::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 esphome::modbus::helpers::pack_bits (Out &out, const Bits &bits)
 Append packed bytes (LSB first) for the given bits onto a growable byte container.
 
template<typename 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.
 
void esphome::modbus::helpers::log_unsupported_value_type (SensorValueType value_type)
 
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.
 
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.
 
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)
 Convert a response payload span to number; std::nullopt if the payload is too short.
 
 esphome::modbus::helpers::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::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).
 
constexpr uint32_t esphome::modbus::helpers::registers_to_uint32 (uint16_t high_word, uint16_t low_word)
 Combine two register words into a 32-bit value.
 
constexpr uint64_t esphome::modbus::helpers::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 esphome::modbus::helpers::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 > > esphome::modbus::helpers::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.
 
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.
 
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.
 
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.
 
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.
 
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.
 
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.
 
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.
 
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.
 
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>.
 
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.
 
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).
 
 esphome::modbus::helpers::ESPDEPRECATED ("Use the container overload of float_to_payload() instead. Removed in 2027.2.0", "2026.8.0") inline std
 

Variables

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