ESPHome  2022.11.3
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

 ModbusController (uint16_t throttle=0)
 
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 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_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_command_throttle (uint16_t command_throttle)
 called by esphome generated code to set the command_throttle period 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...
 
- 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 ()
 
virtual bool can_proceed ()
 
bool status_has_warning ()
 
bool status_has_error ()
 
void status_set_warning ()
 
void status_set_error ()
 
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...
 
size_t get_command_queue_length_ ()
 get the number of queued modbus commands (should be mostly empty) 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()> &&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()> &&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< 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...
 
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...
 
- 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 410 of file modbus_controller.h.

Constructor & Destructor Documentation

◆ ModbusController()

esphome::modbus_controller::ModbusController::ModbusController ( uint16_t  throttle = 0)
inline

Definition at line 412 of file modbus_controller.h.

Member Function Documentation

◆ 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 421 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 168 of file modbus_controller.cpp.

◆ dump_config()

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

Reimplemented from esphome::Component.

Definition at line 274 of file modbus_controller.cpp.

◆ dump_sensors_()

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

dump the parsed sensormap for diagnostics

Definition at line 311 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 82 of file modbus_controller.cpp.

◆ get_command_queue_length_()

size_t esphome::modbus_controller::ModbusController::get_command_queue_length_ ( )
inlineprotected

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

Definition at line 447 of file modbus_controller.h.

◆ loop()

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

Reimplemented from esphome::Component.

Definition at line 292 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 49 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 67 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 96 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 306 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 61 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 107 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 21 of file modbus_controller.cpp.

◆ 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 433 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 154 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 123 of file modbus_controller.cpp.

Field Documentation

◆ 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 455 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 461 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 457 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 459 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 453 of file modbus_controller.h.

◆ sensorset_

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

Collection of all sensors for this component.

Definition at line 451 of file modbus_controller.h.


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