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

#include <modbus_controller.h>

Public Member Functions

bool send ()
 
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 = {}
 
uint8_t send_countdown {MAX_SEND_REPEATS}
 

Static Public Attributes

static const size_t MAX_PAYLOAD_BYTES = 240
 
static const uint8_t MAX_SEND_REPEATS = 5
 

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 508 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 527 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 389 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 403 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 457 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 419 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 440 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 489 of file modbus_controller.cpp.

◆ is_equal()

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

Definition at line 561 of file modbus_controller.cpp.

◆ send()

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

Definition at line 549 of file modbus_controller.cpp.

Field Documentation

◆ function_code

ModbusFunctionCode esphome::modbus_controller::ModbusCommandItem::function_code

Definition at line 319 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.

◆ MAX_SEND_REPEATS

const uint8_t esphome::modbus_controller::ModbusCommandItem::MAX_SEND_REPEATS = 5
static

Definition at line 315 of file modbus_controller.h.

◆ modbusdevice

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

Definition at line 316 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 322 of file modbus_controller.h.

◆ payload

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

Definition at line 323 of file modbus_controller.h.

◆ register_address

uint16_t esphome::modbus_controller::ModbusCommandItem::register_address

Definition at line 317 of file modbus_controller.h.

◆ register_count

uint16_t esphome::modbus_controller::ModbusCommandItem::register_count

Definition at line 318 of file modbus_controller.h.

◆ register_type

ModbusRegisterType esphome::modbus_controller::ModbusCommandItem::register_type

Definition at line 320 of file modbus_controller.h.

◆ send_countdown

uint8_t esphome::modbus_controller::ModbusCommandItem::send_countdown {MAX_SEND_REPEATS}

Definition at line 327 of file modbus_controller.h.


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