ESPHome  2024.10.2
Public Member Functions | Protected Member Functions | Protected Attributes
esphome::modbus_controller::ModbusController Class Reference

Modbus controller class. More...

#include <modbus_controller.h>

Inheritance diagram for esphome::modbus_controller::ModbusController:
esphome::PollingComponent esphome::modbus::ModbusDevice esphome::Component

Public Member Functions

void dump_config () override
 
void loop () override
 
void setup () override
 
void update () override
 
void queue_command (const ModbusCommandItem &command)
 queues a modbus command in the send queue More...
 
void add_sensor_item (SensorItem *item)
 Registers a sensor with the controller. Called by esphomes code generator. More...
 
void add_server_register (ServerRegister *server_register)
 Registers a server register with the controller. Called by esphomes code generator. More...
 
void on_modbus_data (const std::vector< uint8_t > &data) override
 called when a modbus response was parsed without errors More...
 
void on_modbus_error (uint8_t function_code, uint8_t exception_code) override
 called when a modbus error response was received More...
 
void on_modbus_read_registers (uint8_t function_code, uint16_t start_address, uint16_t number_of_registers) final
 called when a modbus request (function code 3 or 4) was parsed without errors More...
 
void on_register_data (ModbusRegisterType register_type, uint16_t start_address, const std::vector< uint8_t > &data)
 default delegate called by process_modbus_data when a response has retrieved from the incoming queue More...
 
void on_write_register_response (ModbusRegisterType register_type, uint16_t start_address, const std::vector< uint8_t > &data)
 default delegate called by process_modbus_data when a response for a write response has retrieved from the incoming queue More...
 
void set_allow_duplicate_commands (bool allow_duplicate_commands)
 Allow a duplicate command to be sent. More...
 
bool get_allow_duplicate_commands ()
 get if a duplicate command can be sent More...
 
void set_command_throttle (uint16_t command_throttle)
 called by esphome generated code to set the command_throttle period More...
 
void set_offline_skip_updates (uint16_t offline_skip_updates)
 called by esphome generated code to set the offline_skip_updates More...
 
size_t get_command_queue_length ()
 get the number of queued modbus commands (should be mostly empty) More...
 
bool get_module_offline ()
 get if the module is offline, didn't respond the last command More...
 
void add_on_command_sent_callback (std::function< void(int, int)> &&callback)
 Set callback for commands. More...
 
void set_max_cmd_retries (uint8_t max_cmd_retries)
 called by esphome generated code to set the max_cmd_retries. More...
 
uint8_t get_max_cmd_retries ()
 get how many times a command will be (re)sent if no response is received More...
 
- Public Member Functions inherited from esphome::PollingComponent
 PollingComponent ()
 
 PollingComponent (uint32_t update_interval)
 Initialize this polling component with the given update interval in ms. More...
 
virtual void set_update_interval (uint32_t update_interval)
 Manually set the update interval in ms for this polling object. More...
 
void call_setup () override
 
virtual uint32_t get_update_interval () const
 Get the update interval in ms of this sensor. More...
 
void start_poller ()
 
void stop_poller ()
 
- Public Member Functions inherited from esphome::Component
virtual float get_setup_priority () const
 priority of setup(). More...
 
float get_actual_setup_priority () const
 
void set_setup_priority (float priority)
 
virtual float get_loop_priority () const
 priority of loop(). More...
 
void call ()
 
virtual void on_shutdown ()
 
virtual void on_safe_shutdown ()
 
uint32_t get_component_state () const
 
virtual void mark_failed ()
 Mark this component as failed. More...
 
bool is_failed () const
 
bool is_ready () const
 
virtual bool can_proceed ()
 
bool status_has_warning () const
 
bool status_has_error () const
 
void status_set_warning (const char *message="unspecified")
 
void status_set_error (const char *message="unspecified")
 
void status_clear_warning ()
 
void status_clear_error ()
 
void status_momentary_warning (const std::string &name, uint32_t length=5000)
 
void status_momentary_error (const std::string &name, uint32_t length=5000)
 
bool has_overridden_loop () const
 
void set_component_source (const char *source)
 Set where this component was loaded from for some debug messages. More...
 
const char * get_component_source () const
 Get the integration where this component was declared as a string. More...
 
- Public Member Functions inherited from esphome::modbus::ModbusDevice
void set_parent (Modbus *parent)
 
void set_address (uint8_t address)
 
void send (uint8_t function, uint16_t start_address, uint16_t number_of_entities, uint8_t payload_len=0, const uint8_t *payload=nullptr)
 
void send_raw (const std::vector< uint8_t > &payload)
 
bool waiting_for_response ()
 

Protected Member Functions

size_t create_register_ranges_ ()
 parse sensormap_ and create range of sequential addresses More...
 
SensorSet find_sensors_ (ModbusRegisterType register_type, uint16_t start_address) const
 
void update_range_ (RegisterRange &r)
 submit the read command for the address range to the send queue More...
 
void process_modbus_data_ (const ModbusCommandItem *response)
 parse incoming modbus data More...
 
bool send_next_command_ ()
 send the next modbus command from the send queue More...
 
void dump_sensors_ ()
 dump the parsed sensormap for diagnostics More...
 
- Protected Member Functions inherited from esphome::Component
virtual void call_loop ()
 
virtual void call_dump_config ()
 
void set_interval (const std::string &name, uint32_t interval, std::function< void()> &&f)
 Set an interval function with a unique name. More...
 
void set_interval (uint32_t interval, std::function< void()> &&f)
 
bool cancel_interval (const std::string &name)
 Cancel an interval function. More...
 
void set_retry (const std::string &name, uint32_t initial_wait_time, uint8_t max_attempts, std::function< RetryResult(uint8_t)> &&f, float backoff_increase_factor=1.0f)
 Set an retry function with a unique name. More...
 
void set_retry (uint32_t initial_wait_time, uint8_t max_attempts, std::function< RetryResult(uint8_t)> &&f, float backoff_increase_factor=1.0f)
 
bool cancel_retry (const std::string &name)
 Cancel a retry function. More...
 
void set_timeout (const std::string &name, uint32_t timeout, std::function< void()> &&f)
 Set a timeout function with a unique name. More...
 
void set_timeout (uint32_t timeout, std::function< void()> &&f)
 
bool cancel_timeout (const std::string &name)
 Cancel a timeout function. More...
 
void defer (const std::string &name, std::function< void()> &&f)
 Defer a callback to the next loop() call. More...
 
void defer (std::function< void()> &&f)
 Defer a callback to the next loop() call. More...
 
bool cancel_defer (const std::string &name)
 Cancel a defer callback using the specified name, name must not be empty. More...
 

Protected Attributes

SensorSet sensorset_
 Collection of all sensors for this component. More...
 
std::vector< ServerRegister * > server_registers_
 Collection of all server registers for this component. More...
 
std::vector< RegisterRangeregister_ranges_
 Continuous range of modbus registers. More...
 
std::list< std::unique_ptr< ModbusCommandItem > > command_queue_
 Hold the pending requests to be sent. More...
 
std::queue< std::unique_ptr< ModbusCommandItem > > incoming_queue_
 modbus response data waiting to get processed More...
 
bool allow_duplicate_commands_
 if duplicate commands can be sent More...
 
uint32_t last_command_timestamp_
 when was the last send operation More...
 
uint16_t command_throttle_
 min time in ms between sending modbus commands More...
 
bool module_offline_
 if module didn't respond the last command More...
 
uint16_t offline_skip_updates_
 how many updates to skip if module is offline More...
 
uint8_t max_cmd_retries_ {4}
 How many times we will retry a command if we get no response. More...
 
CallbackManager< void(int, int)> command_sent_callback_ {}
 
- Protected Attributes inherited from esphome::PollingComponent
uint32_t update_interval_
 
- Protected Attributes inherited from esphome::Component
uint32_t component_state_ {0x0000}
 State of this component. More...
 
float setup_priority_override_ {NAN}
 
const char * component_source_ {nullptr}
 
- Protected Attributes inherited from esphome::modbus::ModbusDevice
friend Modbus
 
Modbusparent_
 
uint8_t address_
 

Detailed Description

Modbus controller class.

Each instance handles the modbus commuinication for all sensors with the same modbus address

all sensor items (sensors, switches, binarysensor ...) are parsed in modbus address ranges. when esphome calls ModbusController::Update the commands for each range are created and sent Responses for the commands are dispatched to the modbus sensor items.

Definition at line 430 of file modbus_controller.h.

Member Function Documentation

◆ add_on_command_sent_callback()

void esphome::modbus_controller::ModbusController::add_on_command_sent_callback ( std::function< void(int, int)> &&  callback)

Set callback for commands.

Definition at line 669 of file modbus_controller.cpp.

◆ add_sensor_item()

void esphome::modbus_controller::ModbusController::add_sensor_item ( SensorItem item)
inline

Registers a sensor with the controller. Called by esphomes code generator.

Definition at line 440 of file modbus_controller.h.

◆ add_server_register()

void esphome::modbus_controller::ModbusController::add_server_register ( ServerRegister server_register)
inline

Registers a server register with the controller. Called by esphomes code generator.

Definition at line 442 of file modbus_controller.h.

◆ create_register_ranges_()

size_t esphome::modbus_controller::ModbusController::create_register_ranges_ ( )
protected

parse sensormap_ and create range of sequential addresses

Definition at line 238 of file modbus_controller.cpp.

◆ dump_config()

void esphome::modbus_controller::ModbusController::dump_config ( )
overridevirtual

Reimplemented from esphome::Component.

Definition at line 344 of file modbus_controller.cpp.

◆ dump_sensors_()

void esphome::modbus_controller::ModbusController::dump_sensors_ ( )
protected

dump the parsed sensormap for diagnostics

Definition at line 388 of file modbus_controller.cpp.

◆ find_sensors_()

SensorSet esphome::modbus_controller::ModbusController::find_sensors_ ( ModbusRegisterType  register_type,
uint16_t  start_address 
) const
protected

Definition at line 150 of file modbus_controller.cpp.

◆ get_allow_duplicate_commands()

bool esphome::modbus_controller::ModbusController::get_allow_duplicate_commands ( )
inline

get if a duplicate command can be sent

Definition at line 460 of file modbus_controller.h.

◆ get_command_queue_length()

size_t esphome::modbus_controller::ModbusController::get_command_queue_length ( )
inline

get the number of queued modbus commands (should be mostly empty)

Definition at line 466 of file modbus_controller.h.

◆ get_max_cmd_retries()

uint8_t esphome::modbus_controller::ModbusController::get_max_cmd_retries ( )
inline

get how many times a command will be (re)sent if no response is received

Definition at line 474 of file modbus_controller.h.

◆ get_module_offline()

bool esphome::modbus_controller::ModbusController::get_module_offline ( )
inline

get if the module is offline, didn't respond the last command

Definition at line 468 of file modbus_controller.h.

◆ loop()

void esphome::modbus_controller::ModbusController::loop ( )
overridevirtual

Reimplemented from esphome::Component.

Definition at line 369 of file modbus_controller.cpp.

◆ on_modbus_data()

void esphome::modbus_controller::ModbusController::on_modbus_data ( const std::vector< uint8_t > &  data)
overridevirtual

called when a modbus response was parsed without errors

Implements esphome::modbus::ModbusDevice.

Definition at line 59 of file modbus_controller.cpp.

◆ on_modbus_error()

void esphome::modbus_controller::ModbusController::on_modbus_error ( uint8_t  function_code,
uint8_t  exception_code 
)
overridevirtual

called when a modbus error response was received

Reimplemented from esphome::modbus::ModbusDevice.

Definition at line 89 of file modbus_controller.cpp.

◆ on_modbus_read_registers()

void esphome::modbus_controller::ModbusController::on_modbus_read_registers ( uint8_t  function_code,
uint16_t  start_address,
uint16_t  number_of_registers 
)
finalvirtual

called when a modbus request (function code 3 or 4) was parsed without errors

Reimplemented from esphome::modbus::ModbusDevice.

Definition at line 104 of file modbus_controller.cpp.

◆ on_register_data()

void esphome::modbus_controller::ModbusController::on_register_data ( ModbusRegisterType  register_type,
uint16_t  start_address,
const std::vector< uint8_t > &  data 
)

default delegate called by process_modbus_data when a response has retrieved from the incoming queue

Definition at line 164 of file modbus_controller.cpp.

◆ on_write_register_response()

void esphome::modbus_controller::ModbusController::on_write_register_response ( ModbusRegisterType  register_type,
uint16_t  start_address,
const std::vector< uint8_t > &  data 
)

default delegate called by process_modbus_data when a response for a write response has retrieved from the incoming queue

Definition at line 383 of file modbus_controller.cpp.

◆ process_modbus_data_()

void esphome::modbus_controller::ModbusController::process_modbus_data_ ( const ModbusCommandItem response)
protected

parse incoming modbus data

Definition at line 83 of file modbus_controller.cpp.

◆ queue_command()

void esphome::modbus_controller::ModbusController::queue_command ( const ModbusCommandItem command)

queues a modbus command in the send queue

Definition at line 175 of file modbus_controller.cpp.

◆ send_next_command_()

bool esphome::modbus_controller::ModbusController::send_next_command_ ( )
protected

send the next modbus command from the send queue

Definition at line 18 of file modbus_controller.cpp.

◆ set_allow_duplicate_commands()

void esphome::modbus_controller::ModbusController::set_allow_duplicate_commands ( bool  allow_duplicate_commands)
inline

Allow a duplicate command to be sent.

Definition at line 456 of file modbus_controller.h.

◆ set_command_throttle()

void esphome::modbus_controller::ModbusController::set_command_throttle ( uint16_t  command_throttle)
inline

called by esphome generated code to set the command_throttle period

Definition at line 462 of file modbus_controller.h.

◆ set_max_cmd_retries()

void esphome::modbus_controller::ModbusController::set_max_cmd_retries ( uint8_t  max_cmd_retries)
inline

called by esphome generated code to set the max_cmd_retries.

Definition at line 472 of file modbus_controller.h.

◆ set_offline_skip_updates()

void esphome::modbus_controller::ModbusController::set_offline_skip_updates ( uint16_t  offline_skip_updates)
inline

called by esphome generated code to set the offline_skip_updates

Definition at line 464 of file modbus_controller.h.

◆ setup()

void esphome::modbus_controller::ModbusController::setup ( )
overridevirtual

Reimplemented from esphome::Component.

Definition at line 10 of file modbus_controller.cpp.

◆ update()

void esphome::modbus_controller::ModbusController::update ( )
overridevirtual

Implements esphome::PollingComponent.

Definition at line 224 of file modbus_controller.cpp.

◆ update_range_()

void esphome::modbus_controller::ModbusController::update_range_ ( RegisterRange r)
protected

submit the read command for the address range to the send queue

Definition at line 193 of file modbus_controller.cpp.

Field Documentation

◆ allow_duplicate_commands_

bool esphome::modbus_controller::ModbusController::allow_duplicate_commands_
protected

if duplicate commands can be sent

Definition at line 500 of file modbus_controller.h.

◆ command_queue_

std::list<std::unique_ptr<ModbusCommandItem> > esphome::modbus_controller::ModbusController::command_queue_
protected

Hold the pending requests to be sent.

Definition at line 496 of file modbus_controller.h.

◆ command_sent_callback_

CallbackManager<void(int, int)> esphome::modbus_controller::ModbusController::command_sent_callback_ {}
protected

Definition at line 511 of file modbus_controller.h.

◆ command_throttle_

uint16_t esphome::modbus_controller::ModbusController::command_throttle_
protected

min time in ms between sending modbus commands

Definition at line 504 of file modbus_controller.h.

◆ incoming_queue_

std::queue<std::unique_ptr<ModbusCommandItem> > esphome::modbus_controller::ModbusController::incoming_queue_
protected

modbus response data waiting to get processed

Definition at line 498 of file modbus_controller.h.

◆ last_command_timestamp_

uint32_t esphome::modbus_controller::ModbusController::last_command_timestamp_
protected

when was the last send operation

Definition at line 502 of file modbus_controller.h.

◆ max_cmd_retries_

uint8_t esphome::modbus_controller::ModbusController::max_cmd_retries_ {4}
protected

How many times we will retry a command if we get no response.

Definition at line 510 of file modbus_controller.h.

◆ module_offline_

bool esphome::modbus_controller::ModbusController::module_offline_
protected

if module didn't respond the last command

Definition at line 506 of file modbus_controller.h.

◆ offline_skip_updates_

uint16_t esphome::modbus_controller::ModbusController::offline_skip_updates_
protected

how many updates to skip if module is offline

Definition at line 508 of file modbus_controller.h.

◆ register_ranges_

std::vector<RegisterRange> esphome::modbus_controller::ModbusController::register_ranges_
protected

Continuous range of modbus registers.

Definition at line 494 of file modbus_controller.h.

◆ sensorset_

SensorSet esphome::modbus_controller::ModbusController::sensorset_
protected

Collection of all sensors for this component.

Definition at line 490 of file modbus_controller.h.

◆ server_registers_

std::vector<ServerRegister *> esphome::modbus_controller::ModbusController::server_registers_
protected

Collection of all server registers for this component.

Definition at line 492 of file modbus_controller.h.


The documentation for this class was generated from the following files: