ESPHome
2024.11.1
|
Modbus controller class. More...
#include <modbus_controller.h>
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 | add_on_online_callback (std::function< void(int, int)> &&callback) |
Set callback for online changes. More... | |
void | add_on_offline_callback (std::function< void(int, int)> &&callback) |
Set callback for offline changes. 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< RegisterRange > | register_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_ {} |
Command sent callback. More... | |
CallbackManager< void(int, int)> | online_callback_ {} |
Server online callback. More... | |
CallbackManager< void(int, int)> | offline_callback_ {} |
Server offline callback. 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 |
Modbus * | parent_ |
uint8_t | address_ |
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.
void esphome::modbus_controller::ModbusController::add_on_command_sent_callback | ( | std::function< void(int, int)> && | callback | ) |
Set callback for commands.
Definition at line 673 of file modbus_controller.cpp.
void esphome::modbus_controller::ModbusController::add_on_offline_callback | ( | std::function< void(int, int)> && | callback | ) |
Set callback for offline changes.
Definition at line 681 of file modbus_controller.cpp.
void esphome::modbus_controller::ModbusController::add_on_online_callback | ( | std::function< void(int, int)> && | callback | ) |
Set callback for online changes.
Definition at line 677 of file modbus_controller.cpp.
|
inline |
Registers a sensor with the controller. Called by esphomes code generator.
Definition at line 440 of file modbus_controller.h.
|
inline |
Registers a server register with the controller. Called by esphomes code generator.
Definition at line 442 of file modbus_controller.h.
|
protected |
parse sensormap_ and create range of sequential addresses
Definition at line 242 of file modbus_controller.cpp.
|
overridevirtual |
Reimplemented from esphome::Component.
Definition at line 348 of file modbus_controller.cpp.
|
protected |
dump the parsed sensormap for diagnostics
Definition at line 392 of file modbus_controller.cpp.
|
protected |
Definition at line 154 of file modbus_controller.cpp.
|
inline |
get if a duplicate command can be sent
Definition at line 460 of file modbus_controller.h.
|
inline |
get the number of queued modbus commands (should be mostly empty)
Definition at line 466 of file modbus_controller.h.
|
inline |
get how many times a command will be (re)sent if no response is received
Definition at line 478 of file modbus_controller.h.
|
inline |
get if the module is offline, didn't respond the last command
Definition at line 468 of file modbus_controller.h.
|
overridevirtual |
Reimplemented from esphome::Component.
Definition at line 373 of file modbus_controller.cpp.
|
overridevirtual |
called when a modbus response was parsed without errors
Implements esphome::modbus::ModbusDevice.
Definition at line 61 of file modbus_controller.cpp.
|
overridevirtual |
called when a modbus error response was received
Reimplemented from esphome::modbus::ModbusDevice.
Definition at line 93 of file modbus_controller.cpp.
|
finalvirtual |
called when a modbus request (function code 3 or 4) was parsed without errors
Reimplemented from esphome::modbus::ModbusDevice.
Definition at line 108 of file modbus_controller.cpp.
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 168 of file modbus_controller.cpp.
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 387 of file modbus_controller.cpp.
|
protected |
parse incoming modbus data
Definition at line 87 of file modbus_controller.cpp.
void esphome::modbus_controller::ModbusController::queue_command | ( | const ModbusCommandItem & | command | ) |
queues a modbus command in the send queue
Definition at line 179 of file modbus_controller.cpp.
|
protected |
send the next modbus command from the send queue
Definition at line 18 of file modbus_controller.cpp.
|
inline |
Allow a duplicate command to be sent.
Definition at line 456 of file modbus_controller.h.
|
inline |
called by esphome generated code to set the command_throttle period
Definition at line 462 of file modbus_controller.h.
|
inline |
called by esphome generated code to set the max_cmd_retries.
Definition at line 476 of file modbus_controller.h.
|
inline |
called by esphome generated code to set the offline_skip_updates
Definition at line 464 of file modbus_controller.h.
|
overridevirtual |
Reimplemented from esphome::Component.
Definition at line 10 of file modbus_controller.cpp.
|
overridevirtual |
Implements esphome::PollingComponent.
Definition at line 228 of file modbus_controller.cpp.
|
protected |
submit the read command for the address range to the send queue
Definition at line 197 of file modbus_controller.cpp.
|
protected |
if duplicate commands can be sent
Definition at line 504 of file modbus_controller.h.
|
protected |
Hold the pending requests to be sent.
Definition at line 500 of file modbus_controller.h.
|
protected |
Command sent callback.
Definition at line 516 of file modbus_controller.h.
|
protected |
min time in ms between sending modbus commands
Definition at line 508 of file modbus_controller.h.
|
protected |
modbus response data waiting to get processed
Definition at line 502 of file modbus_controller.h.
|
protected |
when was the last send operation
Definition at line 506 of file modbus_controller.h.
|
protected |
How many times we will retry a command if we get no response.
Definition at line 514 of file modbus_controller.h.
|
protected |
if module didn't respond the last command
Definition at line 510 of file modbus_controller.h.
|
protected |
Server offline callback.
Definition at line 520 of file modbus_controller.h.
|
protected |
how many updates to skip if module is offline
Definition at line 512 of file modbus_controller.h.
|
protected |
Server online callback.
Definition at line 518 of file modbus_controller.h.
|
protected |
Continuous range of modbus registers.
Definition at line 498 of file modbus_controller.h.
|
protected |
Collection of all sensors for this component.
Definition at line 494 of file modbus_controller.h.
|
protected |
Collection of all server registers for this component.
Definition at line 496 of file modbus_controller.h.