7static const char *
const TAG =
"modbus_controller";
24 if (!command->should_retry(this->max_cmd_retries_)) {
26 ESP_LOGW(TAG,
"Modbus device=%d set offline", this->
address_);
36 this->
offline_callback_.call((
int) command->function_code, command->register_address);
38 ESP_LOGD(TAG,
"Modbus command to device=%d register=0x%02X no response received - removed from send queue",
39 this->
address_, command->register_address);
42 ESP_LOGV(TAG,
"Sending next modbus command to device %d register 0x%02X count %d", this->
address_,
43 command->register_address, command->register_count);
51 if (!command->on_data_func) {
62 ESP_LOGW(TAG,
"Received modbus data but command queue is empty");
66 if (current_command !=
nullptr) {
68 ESP_LOGW(TAG,
"Modbus device=%d back online", this->
address_);
73 r.skip_updates_counter = 0;
78 this->
online_callback_.call((
int) current_command->function_code, current_command->register_address);
82 current_command->payload =
data;
84 ESP_LOGV(TAG,
"Modbus response queued");
91 ESP_LOGV(TAG,
"Process modbus response for address 0x%X size: %zu", response->
register_address,
97 ESP_LOGE(TAG,
"Modbus error function code: 0x%X exception: %d ", function_code, exception_code);
103 if (current_command !=
nullptr) {
105 "Modbus error - last command: function code=0x%X register address = 0x%X "
106 "registers count=%d "
108 function_code, current_command->register_address, current_command->register_count,
109 current_command->payload.size());
115 auto reg_it = std::find_if(
120 ESP_LOGE(TAG,
"No matching range for sensor found - start_address : 0x%X", start_address);
122 return reg_it->sensors;
129 const std::vector<uint8_t> &
data) {
130 ESP_LOGV(TAG,
"data for register address : 0x%X : ", start_address);
134 for (
auto *sensor : sensors) {
135 sensor->parse_and_publish(
data);
144 if (item->is_equal(command)) {
145 ESP_LOGW(TAG,
"Duplicate modbus command found: type=0x%x address=%u count=%u",
149 item->payload = command.
payload;
154 this->
command_queue_.push_back(make_unique<ModbusCommandItem>(command));
164 if (!sensors.empty()) {
165 auto sensor = sensors.cbegin();
167 this, (*sensor)->custom_data,
169 this->on_register_data(ModbusRegisterType::CUSTOM, start_address, data);
171 command_item.register_address = (*sensor)->start_address;
172 command_item.register_count = (*sensor)->register_count;
190 ESP_LOGV(TAG,
"%zu modbus commands already in queue", this->
command_queue_.size());
192 ESP_LOGV(TAG,
"Updating modbus component");
196 ESP_LOGVV(TAG,
"Updating range 0x%X", r.start_address);
205 ESP_LOGW(TAG,
"No sensors registered");
212 uint8_t buffer_offset = 0;
230 ESP_LOGV(TAG,
"Started new range");
250 ESP_LOGV(TAG,
"Re-use previous register - change to register: 0x%X %d offset=%u", curr->
start_address,
259 curr->
offset += buffer_offset;
267 ESP_LOGV(TAG,
"Extend range - change to register: 0x%X %d offset=%u", curr->
start_address,
310 "ModbusController:\n"
312 " Max Command Retries: %d\n"
313 " Offline Skip Updates: %d\n",
316#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
317 ESP_LOGCONFIG(TAG,
"sensormap");
319 ESP_LOGCONFIG(TAG,
" Sensor type=%u start=0x%X offset=0x%X count=%d size=%zu",
320 static_cast<uint8_t
>(it->register_type), it->start_address, it->offset, it->register_count,
321 it->get_register_size());
323 ESP_LOGCONFIG(TAG,
"ranges");
325 ESP_LOGCONFIG(TAG,
" Range type=%u start=0x%X count=%d skip_updates=%d",
static_cast<uint8_t
>(it.register_type),
326 it.start_address, it.register_count, it.skip_updates);
346 const std::vector<uint8_t> &
data) {
352 ESP_LOGV(TAG,
"sensors");
354 ESP_LOGV(TAG,
" Sensor start=0x%X count=%d size=%zu offset=%d", it->start_address, it->register_count,
355 it->get_register_size(), it->offset);
361 std::function<
void(
ModbusRegisterType register_type, uint16_t start_address,
const std::vector<uint8_t> &
data)>
375 uint16_t register_count) {
383 const std::vector<uint8_t> &
data) {
390 uint16_t start_address, uint16_t register_count,
391 const std::vector<uint16_t> &values) {
399 const std::vector<uint8_t> &
data) {
402 for (
auto v : values) {
404 cmd.
payload.push_back(decoded_value[0]);
405 cmd.
payload.push_back(decoded_value[1]);
419 const std::vector<uint8_t> &
data) {
422 cmd.
payload.push_back(value ? 0xFF : 0);
428 const std::vector<bool> &values) {
436 const std::vector<uint8_t> &
data) {
442 for (
auto coil : values) {
444 bitmask |= (1 << bitcounter);
447 if (bitcounter % 8 == 0) {
448 cmd.
payload.push_back(bitmask);
453 if (bitcounter % 8) {
454 cmd.
payload.push_back(bitmask);
468 const std::vector<uint8_t> &
data) {
473 cmd.
payload.push_back(decoded_value[0]);
474 cmd.
payload.push_back(decoded_value[1]);
480 std::function<
void(
ModbusRegisterType register_type, uint16_t start_address,
const std::vector<uint8_t> &
data)>
485 if (handler ==
nullptr) {
487 ESP_LOGI(TAG,
"Custom Command sent");
499 std::function<
void(
ModbusRegisterType register_type, uint16_t start_address,
const std::vector<uint8_t> &
data)>
504 if (handler ==
nullptr) {
506 ESP_LOGI(TAG,
"Custom Command sent");
511 for (
auto v : values) {
512 cmd.
payload.push_back((v >> 8) & 0xFF);
513 cmd.
payload.push_back(v & 0xFF);
522 this->payload.empty() ?
nullptr : &this->payload[0]);
void send_raw(const std::vector< uint8_t > &payload)
void send(uint8_t function, uint16_t start_address, uint16_t number_of_entities, uint8_t payload_len=0, const uint8_t *payload=nullptr)
bool ready_for_immediate_send()
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.
bool is_equal(const ModbusCommandItem &other)
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.
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.
uint8_t send_count_
How many times this command has been sent.
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.
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
ModbusRegisterType register_type
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.
uint16_t register_address
ModbusController * modbusdevice
ModbusFunctionCode function_code
std::function< void(ModbusRegisterType register_type, uint16_t start_address, const std::vector< uint8_t > &data)> on_data_func
std::vector< uint8_t > payload
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
std::queue< std::unique_ptr< ModbusCommandItem > > incoming_queue_
modbus response data waiting to get processed
uint16_t command_throttle_
min time in ms between sending modbus commands
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 fro...
bool allow_duplicate_commands_
if duplicate commands can be sent
CallbackManager< void(int, int)> command_sent_callback_
Command sent callback.
std::vector< RegisterRange > register_ranges_
Continuous range of modbus registers.
uint32_t last_command_timestamp_
when was the last send operation
CallbackManager< void(int, int)> offline_callback_
Server offline callback.
void dump_sensors_()
dump the parsed sensormap for diagnostics
SensorSet sensorset_
Collection of all sensors for this component.
uint8_t max_cmd_retries_
How many times we will retry a command if we get no response.
bool send_next_command_()
send the next modbus command from the send queue
std::list< std::unique_ptr< ModbusCommandItem > > command_queue_
Hold the pending requests to be sent.
void on_modbus_error(uint8_t function_code, uint8_t exception_code) override
called when a modbus error response was received
void process_modbus_data_(const ModbusCommandItem *response)
parse incoming modbus data
void update_range_(RegisterRange &r)
submit the read command for the address range to the send queue
bool module_offline_
if module didn't respond the last command
size_t create_register_ranges_()
parse sensormap_ and create range of sequential addresses
uint16_t offline_skip_updates_
how many updates to skip if module is offline
void dump_config() override
void on_modbus_data(const std::vector< uint8_t > &data) override
called when a modbus response was parsed without errors
void queue_command(const ModbusCommandItem &command)
queues a modbus command in the send queue
SensorSet find_sensors_(ModbusRegisterType register_type, uint16_t start_address) const
CallbackManager< void(int, int)> online_callback_
Server online callback.
ModbusRegisterType register_type
virtual size_t get_register_size() const
const LogString * message
ModbusFunctionCode modbus_register_read_function(ModbusRegisterType reg_type)
T get_data(const std::vector< uint8_t > &data, size_t buffer_offset)
Extract data from modbus response buffer.
std::set< SensorItem *, SensorItemsComparator > SensorSet
const std::vector< uint8_t > & data
@ WRITE_MULTIPLE_REGISTERS
constexpr std::array< uint8_t, sizeof(T)> decode_value(T val)
Decode a value into its constituent bytes (from most to least significant).
uint32_t IRAM_ATTR HOT millis()
ModbusRegisterType register_type
uint16_t skip_updates_counter