17class ModbusController;
25#pragma GCC diagnostic push
26#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
27using modbus::ModbusExceptionCode;
28using modbus::ModbusFunctionCode;
29using modbus::ModbusRegisterType;
30#pragma GCC diagnostic pop
33ESPDEPRECATED(
"Use modbus::helpers::value_type_is_float() instead. Removed in 2026.10.0",
"2026.4.0")
36ESPDEPRECATED(
"Use modbus::helpers::modbus_register_read_function() instead. Removed in 2026.10.0",
"2026.4.0")
41ESPDEPRECATED(
"Use modbus::helpers::modbus_register_write_function() instead. Removed in 2026.10.0",
"2026.4.0")
46ESPDEPRECATED(
"Use modbus::helpers::c_to_hex() instead. Removed in 2026.10.0",
"2026.4.0")
49ESPDEPRECATED(
"Use modbus::helpers::byte_from_hex_str() instead. Removed in 2026.10.0",
"2026.4.0")
50inline uint8_t byte_from_hex_str(const
std::
string &value, uint8_t
pos) {
54ESPDEPRECATED(
"Use modbus::helpers::word_from_hex_str() instead. Removed in 2026.10.0",
"2026.4.0")
55inline uint16_t word_from_hex_str(const
std::
string &value, uint8_t
pos) {
59ESPDEPRECATED(
"Use modbus::helpers::dword_from_hex_str() instead. Removed in 2026.10.0",
"2026.4.0")
60inline
uint32_t dword_from_hex_str(const
std::
string &value, uint8_t
pos) {
64ESPDEPRECATED(
"Use modbus::helpers::qword_from_hex_str() instead. Removed in 2026.10.0",
"2026.4.0")
65inline uint64_t qword_from_hex_str(const
std::
string &value, uint8_t
pos) {
70ESPDEPRECATED(
"Use modbus::helpers::get_data() instead. Removed in 2026.10.0",
"2026.4.0")
71T get_data(const
std::vector<uint8_t> &
data,
size_t buffer_offset) {
84ESPDEPRECATED(
"Use modbus::helpers::get_data() instead. Removed in 2027.2.0",
"2026.8.0")
85T get_data(
std::span<const uint8_t>
data,
size_t buffer_offset) {
90ESPDEPRECATED(
"Use modbus::helpers::bit_from_packed() instead. Removed in 2027.2.0",
"2026.4.0")
91inline
bool coil_from_vector(
int coil, const
std::vector<uint8_t> &
data) {
96ESPDEPRECATED(
"Use modbus::helpers::bit_from_packed() instead. Removed in 2027.2.0",
"2026.8.0")
97inline
bool coil_from_vector(
int coil,
std::span<const uint8_t>
data) {
102ESPDEPRECATED(
"Use modbus::helpers::mask_and_shift_by_rightbit() instead. Removed in 2026.10.0",
"2026.4.0")
107ESPDEPRECATED(
"Use modbus::helpers::number_to_payload() instead. Removed in 2026.10.0",
"2026.4.0")
112ESPDEPRECATED(
"Use modbus::helpers::payload_to_number() instead. Removed in 2026.10.0",
"2026.4.0")
113inline int64_t payload_to_number(const
std::vector<uint8_t> &
data,
SensorValueType sensor_value_type, uint8_t offset,
119ESPDEPRECATED(
"Use modbus::helpers::float_to_payload() instead. Removed in 2026.10.0",
"2026.4.0")
120inline
std::vector<uint16_t> float_to_payload(
float value,
SensorValueType value_type) {
121 std::vector<uint16_t>
data;
126class ModbusController;
227using SensorSet = std::set<SensorItem *, SensorItemsComparator>;
273 void on_response(std::span<const uint8_t> request_pdu, std::span<const uint8_t> response_pdu)
override;
277 void on_not_sent(std::span<const uint8_t> request_pdu)
override;
279 void on_sent(std::span<const uint8_t> request_pdu)
override;
281 bool on_no_response(std::span<const uint8_t> request_pdu)
override;
343 const std::vector<bool> &values);
388inline bool offline_retry_due(uint16_t update_counter, uint16_t module_offline_at, uint16_t offline_skip_updates) {
389 return static_cast<uint16_t
>(update_counter + 1 - module_offline_at) % (offline_skip_updates + 1) == 0;
405 void setup()
override;
428 void set_online(
bool online,
int function_code,
int register_address);
470 cmd.sensors = std::move(
range.sensors);
471 cmd.skip_updates =
range.skip_updates;
527 float_value =
static_cast<float>(number);
536ESPDEPRECATED(
"Pass the offset explicitly: payload_to_float(data, item, item.offset). Removed in 2027.2.0",
"2026.8.0")
This class simplifies creating components that periodically check a state.
Small buffer optimization - stores data inline when small, heap-allocates for large data This avoids ...
ModbusClientDevice()=default
const std::vector< uint8_t > * custom_data_
Custom polling commands reference the PDU bytes owned by their SensorItem instead of copying them.
EntityType register_type() const
static ModbusCommandItem create_read_command(ModbusController *modbusdevice, EntityType register_type, uint16_t start_address, uint16_t register_count, std::function< void(EntityType register_type, uint16_t start_address, std::span< const uint8_t > data)> &&handler)
factory methods
SmallInlineBuffer< 8 > payload
Write data bytes for the command (register/coil values), or the raw frame of a one-shot custom comman...
void on_response(std::span< const uint8_t > request_pdu, std::span< const uint8_t > response_pdu) override
called when a modbus response was parsed without errors
static ModbusCommandItem create_write_multiple_coils(ModbusController *modbusdevice, uint16_t start_address, const std::vector< bool > &values)
Create modbus write multiple registers command Function 15 (0Fhex) Write Multiple Coils.
void set_command_(FunctionCode function_code, EntityType register_type, uint16_t start_address, uint16_t register_count)
bool on_no_response(std::span< const uint8_t > request_pdu) override
called on timeout; returns true to have the hub re-queue the frame for a retry
ModbusController * controller_
static ModbusCommandItem create_write_single_coil(ModbusController *modbusdevice, uint16_t address, bool value)
Create modbus write single registers command Function 05 (05hex) Write Single Coil.
EntityType register_type_
bool send()
Queue this command's frame on the hub.
static ModbusCommandItem create_write_single_command(ModbusController *modbusdevice, uint16_t start_address, uint16_t value)
Create modbus write multiple registers command Function 16 (10hex) Write Multiple Registers.
static ModbusCommandItem create_custom_command(ModbusController *modbusdevice, const std::vector< uint8_t > &values, std::function< void(EntityType register_type, uint16_t start_address, std::span< const uint8_t > data)> &&handler=nullptr)
Create custom modbus command.
ModbusCommandItem(ModbusController &controller, modbus::ModbusClientHub *parent, uint8_t address)
Empty command with no controller connection (kept for source compatibility with value-type usage).
void on_error(std::span< const uint8_t > request_pdu, modbus::ExceptionCode exception_code) override
called when a modbus error (exception) response was received
static ModbusCommandItem create_write_multiple_command(ModbusController *modbusdevice, uint16_t start_address, uint16_t register_count, const std::vector< uint16_t > &values)
Create modbus read command Function code 02-04.
std::function< void(EntityType register_type, uint16_t start_address, std::span< const uint8_t > data)> on_data_func
void on_not_sent(std::span< const uint8_t > request_pdu) override
called when the command could not be sent
void on_sent(std::span< const uint8_t > request_pdu) override
called when the command's frame is actually written to the wire; fires the on_command_sent trigger
ModbusCommandItem & operator=(ModbusCommandItem &&)=delete
uint16_t register_address() const
FunctionCode function_code_
uint16_t register_count() const
void add_sensor_item(SensorItem *item)
Registers a sensor with the controller. Called by esphomes code generator.
uint8_t cmd_non_responses_
consecutive non-responses; drives can_send() and offline detection
void create_polling_command_(RegisterRange &&range)
build one persistent polling command from a range and add it to polling_command_items_
void command_sent(int function_code, int register_address)
Fire the on_command_sent trigger (called when a command's frame reaches the wire).
uint8_t device_address() const
void set_offline_skip_updates(uint16_t offline_skip_updates)
called by esphome generated code to set the offline_skip_updates
std::list< std::unique_ptr< ModbusCommandItem > > one_shot_command_items_
Dynamically queued one-shot commands (writes, custom commands). std::list keeps stable addresses.
void queue_command(ModbusCommandItem command)
Queues a one-shot modbus command (writes, custom commands); taken by value, so std::move to avoid a c...
void update_range_(ModbusCommandItem &cmd)
send a range's polling command if it is due this update
CallbackManager< void(int, int)> command_sent_callback_
Command sent callback.
void increment_non_response_count()
A command timed out; bump the consecutive-timeout counter used by can_send()/offline detection.
void set_parent(modbus::ModbusClientHub *hub)
CallbackManager< void(int, int)> offline_callback_
Server offline callback.
void add_on_command_sent_callback(F &&callback)
Set callback for commands.
SensorSet sensorset_
Collection of all sensors for this component.
void unqueue_command(const ModbusCommandItem *command)
Flags a finished one-shot command for removal.
uint8_t max_cmd_retries_
How many times we will retry a command if we get no response.
bool can_send()
Whether more retries are allowed before the device is considered offline.
void sweep_completed_one_shots_()
Erases one-shot commands flagged by unqueue_command().
uint8_t get_max_cmd_retries()
get how many times a command will be (re)sent if no response is received
void create_polling_commands_()
parse sensormap_ and create range of sequential addresses Group the registered sensors into contiguou...
uint16_t module_offline_at_
update_counter_ value at which the module went offline (for offline_skip_updates timing)
void set_max_cmd_retries(uint8_t max_cmd_retries)
called by esphome generated code to set the max_cmd_retries.
bool module_offline_
if module didn't respond the last command
uint16_t offline_skip_updates_
how many updates to skip if module is offline
modbus::ModbusClientHub * hub() const
The hub and modbus address this controller talks to.
bool get_module_offline()
get if the module is offline, didn't respond the last command
void set_online(bool online, int function_code, int register_address)
Update the online/offline state after a response or a run of timeouts, firing the callbacks.
uint16_t update_counter_
counts update() cycles; drives skip_updates and offline timing
void set_address(uint8_t address)
void add_on_online_callback(F &&callback)
Set callback for online changes.
void add_on_offline_callback(F &&callback)
Set callback for offline changes.
void dump_config() override
std::vector< ModbusCommandItem > polling_command_items_
One persistent command per register range, each its own ModbusClientDevice.
void on_write_register_response(EntityType register_type, uint16_t start_address, std::span< const uint8_t > data)
Handles a write command acknowledgement (used by write command on_data_func handlers).
modbus::ModbusClientHub * hub_
The hub this controller's commands/entities send through, and the modbus address they target.
CallbackManager< void(int, int)> online_callback_
Server online callback.
virtual void parse_and_publish(std::span< const uint8_t > data)=0
Parse this sensor's slice out of its range's response and publish it.
void set_custom_data(const std::vector< uint8_t > &data)
modbus::EntityType register_type
void set_address(uint16_t address)
Sets the configured address, and points the range base at it.
void set_offset_from_start_address(uint8_t offset)
Records the offset as configured, and seeds the resolved position with it.
SensorValueType sensor_value_type
void set_register_size(uint8_t register_size)
uint16_t range_start_address
First register of the range this sensor is polled in; equals start_address for an unpolled item.
uint8_t offset
Position of this sensor's data within its range's response - a byte offset for registers,...
uint8_t offset_from_start_address
The offset exactly as configured: measured from this sensor's own start_address, where offset is meas...
bool addresses_bits() const
Coils and discrete inputs address individual bits; every other type addresses 16-bit registers.
std::vector< uint8_t > custom_data
uint16_t write_address() const
Address a write entity (switch/number/select) targets, derived from its resolved position within the ...
virtual size_t get_register_size() const
bool operator()(const SensorItem *lhs, const SensorItem *rhs) const
bool value_type_is_float(SensorValueType v)
void number_to_payload(Container &data, int64_t value, SensorValueType value_type)
Append the Modbus register words for value to data.
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).
T get_data(const uint8_t *data, size_t buffer_offset)
Extract data from modbus response buffer.
FunctionCode modbus_register_write_function(EntityType reg_type, bool multiple=false)
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.
FunctionCode modbus_register_read_function(EntityType reg_type)
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.
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 .
bool is_entity_type_binary(EntityType type)
Coils and discrete inputs are the bit-addressed entity tables; the other types are 16-bit registers.
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.
ESPDEPRECATED("Use modbus::helpers::value_type_is_float() instead. Removed in 2026.10.0", "2026.4.0") inline bool value_type_is_float(SensorValueType v)
const std::vector< uint8_t > & data
std::set< SensorItem *, SensorItemsComparator > SensorSet
float payload_to_float(std::span< const uint8_t > data, const SensorItem &item, uint8_t offset)
Convert vector<uint8_t> response payload to float.
bool offline_retry_due(uint16_t update_counter, uint16_t module_offline_at, uint16_t offline_skip_updates)
Whether an offline probe is due this update cycle: every offline_skip_updates + 1 cycles,...
To bit_cast(const From &src)
Convert data between types, without aliasing issues or undefined behaviour.
modbus::EntityType register_type