ESPHome  2024.11.0
Public Member Functions | Static Public Member Functions | Data Fields | Static Public Attributes | Protected Attributes
esphome::modbus_controller::ModbusCommandItem Class Reference

#include <modbus_controller.h>

Public Member Functions

bool send ()
 
bool should_retry (uint8_t max_retries)
 Check if the command should be retried based on the max_retries parameter. More...
 
bool is_equal (const ModbusCommandItem &other)
 

Static Public Member Functions

static ModbusCommandItem create_read_command (ModbusController *modbusdevice, ModbusRegisterType register_type, uint16_t start_address, uint16_t register_count, std::function< void(ModbusRegisterType register_type, uint16_t start_address, const std::vector< uint8_t > &data)> &&handler)
 factory methods More...
 
static ModbusCommandItem create_read_command (ModbusController *modbusdevice, ModbusRegisterType register_type, uint16_t start_address, uint16_t register_count)
 Create modbus read command Function code 02-04. More...
 
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. More...
 
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. More...
 
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. More...
 
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. More...
 
static ModbusCommandItem create_custom_command (ModbusController *modbusdevice, const std::vector< uint8_t > &values, std::function< void(ModbusRegisterType register_type, uint16_t start_address, const std::vector< uint8_t > &data)> &&handler=nullptr)
 Create custom modbus command. More...
 
static ModbusCommandItem create_custom_command (ModbusController *modbusdevice, const std::vector< uint16_t > &values, std::function< void(ModbusRegisterType register_type, uint16_t start_address, const std::vector< uint8_t > &data)> &&handler=nullptr)
 Create custom modbus command. More...
 

Data Fields

ModbusControllermodbusdevice
 
uint16_t register_address
 
uint16_t register_count
 
ModbusFunctionCode function_code
 
ModbusRegisterType register_type
 
std::function< void(ModbusRegisterType register_type, uint16_t start_address, const std::vector< uint8_t > &data)> on_data_func
 
std::vector< uint8_t > payload = {}
 

Static Public Attributes

static const size_t MAX_PAYLOAD_BYTES = 240
 

Protected Attributes

uint8_t send_count_ {0}
 How many times this command has been sent. More...
 

Detailed Description

Definition at line 312 of file modbus_controller.h.

Member Function Documentation

◆ create_custom_command() [1/2]

ModbusCommandItem esphome::modbus_controller::ModbusCommandItem::create_custom_command ( ModbusController modbusdevice,
const std::vector< uint8_t > &  values,
std::function< void(ModbusRegisterType register_type, uint16_t start_address, const std::vector< uint8_t > &data)> &&  handler = nullptr 
)
static

Create custom modbus command.

Parameters
modbusdevicepointer to the device to execute the command
valuesbyte vector of data to be sent to the device. The complete payload must be provided with the exception of the crc codes
handlerfunction called when the response is received. Default is just logging a response
Returns
ModbusCommandItem with the prepared command

Definition at line 519 of file modbus_controller.cpp.

◆ create_custom_command() [2/2]

ModbusCommandItem esphome::modbus_controller::ModbusCommandItem::create_custom_command ( ModbusController modbusdevice,
const std::vector< uint16_t > &  values,
std::function< void(ModbusRegisterType register_type, uint16_t start_address, const std::vector< uint8_t > &data)> &&  handler = nullptr 
)
static

Create custom modbus command.

Parameters
modbusdevicepointer to the device to execute the command
valuesword vector of data to be sent to the device. The complete payload must be provided with the exception of the crc codes
handlerfunction called when the response is received. Default is just logging a response
Returns
ModbusCommandItem with the prepared command

Definition at line 538 of file modbus_controller.cpp.

◆ create_read_command() [1/2]

ModbusCommandItem esphome::modbus_controller::ModbusCommandItem::create_read_command ( ModbusController modbusdevice,
ModbusRegisterType  register_type,
uint16_t  start_address,
uint16_t  register_count,
std::function< void(ModbusRegisterType register_type, uint16_t start_address, const std::vector< uint8_t > &data)> &&  handler 
)
static

factory methods

Create modbus read command Function code 02-04

Parameters
modbusdevicepointer to the device to execute the command
function_codemodbus function code for the read command
start_addressmodbus address of the first register to read
register_countnumber of registers to read
handlerfunction called when the response is received
Returns
ModbusCommandItem with the prepared command

Definition at line 400 of file modbus_controller.cpp.

◆ create_read_command() [2/2]

ModbusCommandItem esphome::modbus_controller::ModbusCommandItem::create_read_command ( ModbusController modbusdevice,
ModbusRegisterType  register_type,
uint16_t  start_address,
uint16_t  register_count 
)
static

Create modbus read command Function code 02-04.

Parameters
modbusdevicepointer to the device to execute the command
function_codemodbus function code for the read command
start_addressmodbus address of the first register to read
register_countnumber of registers to read
Returns
ModbusCommandItem with the prepared command

Definition at line 414 of file modbus_controller.cpp.

◆ create_write_multiple_coils()

ModbusCommandItem esphome::modbus_controller::ModbusCommandItem::create_write_multiple_coils ( ModbusController modbusdevice,
uint16_t  start_address,
const std::vector< bool > &  values 
)
static

Create modbus write multiple registers command Function 15 (0Fhex) Write Multiple Coils.

Parameters
modbusdevicepointer to the device to execute the command
start_addressmodbus address of the first register to read
valuebool vector of values to be written to the registers
Returns
ModbusCommandItem with the prepared command

Definition at line 468 of file modbus_controller.cpp.

◆ create_write_multiple_command()

ModbusCommandItem esphome::modbus_controller::ModbusCommandItem::create_write_multiple_command ( ModbusController modbusdevice,
uint16_t  start_address,
uint16_t  register_count,
const std::vector< uint16_t > &  values 
)
static

Create modbus read command Function code 02-04.

Parameters
modbusdevicepointer to the device to execute the command
function_codemodbus function code for the read command
start_addressmodbus address of the first register to read
register_countnumber of registers to read
handlerfunction called when the response is received
Returns
ModbusCommandItem with the prepared command

Definition at line 430 of file modbus_controller.cpp.

◆ create_write_single_coil()

ModbusCommandItem esphome::modbus_controller::ModbusCommandItem::create_write_single_coil ( ModbusController modbusdevice,
uint16_t  address,
bool  value 
)
static

Create modbus write single registers command Function 05 (05hex) Write Single Coil.

Parameters
modbusdevicepointer to the device to execute the command
start_addressmodbus address of the first register to read
valueuint16_t data to be written to the registers
Returns
ModbusCommandItem with the prepared command

Definition at line 451 of file modbus_controller.cpp.

◆ create_write_single_command()

ModbusCommandItem esphome::modbus_controller::ModbusCommandItem::create_write_single_command ( ModbusController modbusdevice,
uint16_t  start_address,
uint16_t  value 
)
static

Create modbus write multiple registers command Function 16 (10hex) Write Multiple Registers.

Parameters
modbusdevicepointer to the device to execute the command
start_addressmodbus address of the first register to read
register_countnumber of registers to read
valueuint16_t single register value to write
Returns
ModbusCommandItem with the prepared command

Definition at line 500 of file modbus_controller.cpp.

◆ is_equal()

bool esphome::modbus_controller::ModbusCommandItem::is_equal ( const ModbusCommandItem other)

Definition at line 573 of file modbus_controller.cpp.

◆ send()

bool esphome::modbus_controller::ModbusCommandItem::send ( )

Definition at line 560 of file modbus_controller.cpp.

◆ should_retry()

bool esphome::modbus_controller::ModbusCommandItem::should_retry ( uint8_t  max_retries)
inline

Check if the command should be retried based on the max_retries parameter.

Definition at line 325 of file modbus_controller.h.

Field Documentation

◆ function_code

ModbusFunctionCode esphome::modbus_controller::ModbusCommandItem::function_code

Definition at line 318 of file modbus_controller.h.

◆ MAX_PAYLOAD_BYTES

const size_t esphome::modbus_controller::ModbusCommandItem::MAX_PAYLOAD_BYTES = 240
static

Definition at line 314 of file modbus_controller.h.

◆ modbusdevice

ModbusController* esphome::modbus_controller::ModbusCommandItem::modbusdevice

Definition at line 315 of file modbus_controller.h.

◆ on_data_func

std::function<void(ModbusRegisterType register_type, uint16_t start_address, const std::vector<uint8_t> &data)> esphome::modbus_controller::ModbusCommandItem::on_data_func

Definition at line 321 of file modbus_controller.h.

◆ payload

std::vector<uint8_t> esphome::modbus_controller::ModbusCommandItem::payload = {}

Definition at line 322 of file modbus_controller.h.

◆ register_address

uint16_t esphome::modbus_controller::ModbusCommandItem::register_address

Definition at line 316 of file modbus_controller.h.

◆ register_count

uint16_t esphome::modbus_controller::ModbusCommandItem::register_count

Definition at line 317 of file modbus_controller.h.

◆ register_type

ModbusRegisterType esphome::modbus_controller::ModbusCommandItem::register_type

Definition at line 319 of file modbus_controller.h.

◆ send_count_

uint8_t esphome::modbus_controller::ModbusCommandItem::send_count_ {0}
protected

How many times this command has been sent.

Definition at line 419 of file modbus_controller.h.


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