6 namespace modbus_controller {
8 static const char *
const TAG =
"modbus_controller";
25 if (!command->should_retry(this->max_cmd_retries_)) {
27 ESP_LOGW(TAG,
"Modbus device=%d set offline", this->
address_);
37 ESP_LOGD(TAG,
"Modbus command to device=%d register=0x%02X no response received - removed from send queue",
38 this->
address_, command->register_address);
41 ESP_LOGV(TAG,
"Sending next modbus command to device %d register 0x%02X count %d", this->
address_,
42 command->register_address, command->register_count);
50 if (!command->on_data_func) {
61 if (current_command !=
nullptr) {
63 ESP_LOGW(TAG,
"Modbus device=%d back online", this->
address_);
68 r.skip_updates_counter = 0;
75 current_command->payload = data;
77 ESP_LOGV(TAG,
"Modbus response queued");
84 ESP_LOGV(TAG,
"Process modbus response for address 0x%X size: %zu", response->
register_address,
90 ESP_LOGE(TAG,
"Modbus error function code: 0x%X exception: %d ", function_code, exception_code);
93 if (current_command !=
nullptr) {
95 "Modbus error - last command: function code=0x%X register address = 0x%X " 98 function_code, current_command->register_address, current_command->register_count,
99 current_command->payload.size());
105 uint16_t number_of_registers) {
107 "Received read holding/input registers for device 0x%X. FC: 0x%X. Start address: 0x%X. Number of registers: " 109 this->
address_, function_code, start_address, number_of_registers);
111 std::vector<uint16_t> sixteen_bit_response;
112 for (uint16_t current_address = start_address; current_address < start_address + number_of_registers;) {
115 if (server_register->address == current_address) {
116 float value = server_register->read_lambda();
118 ESP_LOGD(TAG,
"Matched register. Address: 0x%02X. Value type: %zu. Register count: %u. Value: %0.1f.",
119 server_register->address, static_cast<uint8_t>(server_register->value_type),
120 server_register->register_count, value);
121 std::vector<uint16_t> payload =
float_to_payload(value, server_register->value_type);
122 sixteen_bit_response.insert(sixteen_bit_response.end(), payload.cbegin(), payload.cend());
123 current_address += server_register->register_count;
130 ESP_LOGW(TAG,
"Could not match any register to address %02X. Sending exception response.", current_address);
131 std::vector<uint8_t> error_response;
132 error_response.push_back(this->
address_);
133 error_response.push_back(0x81);
134 error_response.push_back(0x02);
140 std::vector<uint8_t> response;
141 for (
auto v : sixteen_bit_response) {
143 response.push_back(decoded_value[0]);
144 response.push_back(decoded_value[1]);
147 this->
send(function_code, start_address, number_of_registers, response.size(), response.data());
156 ESP_LOGE(TAG,
"No matching range for sensor found - start_address : 0x%X", start_address);
158 return reg_it->sensors;
165 const std::vector<uint8_t> &data) {
166 ESP_LOGV(TAG,
"data for register address : 0x%X : ", start_address);
170 for (
auto *
sensor : sensors) {
171 sensor->parse_and_publish(data);
180 if (item->is_equal(command)) {
181 ESP_LOGW(TAG,
"Duplicate modbus command found: type=0x%x address=%u count=%u",
185 item->payload = command.
payload;
190 this->
command_queue_.push_back(make_unique<ModbusCommandItem>(command));
200 if (!sensors.empty()) {
201 auto sensor = sensors.cbegin();
203 this, (*sensor)->custom_data,
204 [
this](
ModbusRegisterType register_type, uint16_t start_address,
const std::vector<uint8_t> &data) {
205 this->on_register_data(ModbusRegisterType::CUSTOM, start_address, data);
207 command_item.register_address = (*sensor)->start_address;
208 command_item.register_count = (*sensor)->register_count;
226 ESP_LOGV(TAG,
"%zu modbus commands already in queue", this->
command_queue_.size());
228 ESP_LOGV(TAG,
"Updating modbus component");
232 ESP_LOGVV(TAG,
"Updating range 0x%X", r.start_address);
241 ESP_LOGW(TAG,
"No sensors registered");
248 uint8_t buffer_offset = 0;
266 ESP_LOGV(TAG,
"Started new range");
286 ESP_LOGV(TAG,
"Re-use previous register - change to register: 0x%X %d offset=%u", curr->
start_address,
295 curr->
offset += buffer_offset;
303 ESP_LOGV(TAG,
"Extend range - change to register: 0x%X %d offset=%u", curr->
start_address,
345 ESP_LOGCONFIG(TAG,
"ModbusController:");
346 ESP_LOGCONFIG(TAG,
" Address: 0x%02X", this->
address_);
349 #if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE 350 ESP_LOGCONFIG(TAG,
"sensormap");
352 ESP_LOGCONFIG(TAG,
" Sensor type=%zu start=0x%X offset=0x%X count=%d size=%d",
353 static_cast<uint8_t>(it->register_type), it->start_address, it->offset, it->register_count,
354 it->get_register_size());
356 ESP_LOGCONFIG(TAG,
"ranges");
358 ESP_LOGCONFIG(TAG,
" Range type=%zu start=0x%X count=%d skip_updates=%d", static_cast<uint8_t>(it.register_type),
359 it.start_address, it.register_count, it.skip_updates);
361 ESP_LOGCONFIG(TAG,
"server registers");
363 ESP_LOGCONFIG(TAG,
" Address=0x%02X value_type=%zu register_count=%u", r->address,
364 static_cast<uint8_t>(r->value_type), r->register_count);
373 if (message !=
nullptr)
384 const std::vector<uint8_t> &data) {
385 ESP_LOGV(TAG,
"Command ACK 0x%X %d ", get_data<uint16_t>(data, 0), get_data<int16_t>(data, 1));
389 ESP_LOGV(TAG,
"sensors");
391 ESP_LOGV(TAG,
" Sensor start=0x%X count=%d size=%d offset=%d", it->start_address, it->register_count,
392 it->get_register_size(), it->offset);
398 std::function<
void(
ModbusRegisterType register_type, uint16_t start_address,
const std::vector<uint8_t> &data)>
412 uint16_t register_count) {
420 const std::vector<uint8_t> &data) {
427 uint16_t start_address, uint16_t register_count,
428 const std::vector<uint16_t> &values) {
436 const std::vector<uint8_t> &data) {
439 for (
auto v : values) {
441 cmd.
payload.push_back(decoded_value[0]);
442 cmd.
payload.push_back(decoded_value[1]);
456 const std::vector<uint8_t> &data) {
459 cmd.
payload.push_back(value ? 0xFF : 0);
465 const std::vector<bool> &values) {
473 const std::vector<uint8_t> &data) {
479 for (
auto coil : values) {
481 bitmask |= (1 << bitcounter);
484 if (bitcounter % 8 == 0) {
485 cmd.
payload.push_back(bitmask);
490 if (bitcounter % 8) {
491 cmd.
payload.push_back(bitmask);
505 const std::vector<uint8_t> &data) {
510 cmd.
payload.push_back(decoded_value[0]);
511 cmd.
payload.push_back(decoded_value[1]);
517 std::function<
void(
ModbusRegisterType register_type, uint16_t start_address,
const std::vector<uint8_t> &data)>
522 if (handler ==
nullptr) {
524 ESP_LOGI(TAG,
"Custom Command sent");
536 std::function<
void(
ModbusRegisterType register_type, uint16_t start_address,
const std::vector<uint8_t> &data)>
541 if (handler ==
nullptr) {
543 ESP_LOGI(TAG,
"Custom Command sent");
548 for (
auto v : values) {
549 cmd.
payload.push_back((v >> 8) & 0xFF);
550 cmd.
payload.push_back(v & 0xFF);
558 modbusdevice->
send(uint8_t(this->function_code), this->register_address, this->register_count, this->payload.size(),
559 this->payload.empty() ? nullptr : &this->payload[0]);
561 modbusdevice->
send_raw(this->payload);
564 ESP_LOGV(TAG,
"Command sent %d 0x%X %d send_count: %d", uint8_t(this->function_code), this->register_address,
565 this->register_count, this->send_count_);
573 ? this->payload == other.
payload 579 switch (value_type) {
582 data.push_back(value & 0xFFFF);
587 data.push_back((value & 0xFFFF0000) >> 16);
588 data.push_back(value & 0xFFFF);
593 data.push_back(value & 0xFFFF);
594 data.push_back((value & 0xFFFF0000) >> 16);
598 data.push_back((value & 0xFFFF000000000000) >> 48);
599 data.push_back((value & 0xFFFF00000000) >> 32);
600 data.push_back((value & 0xFFFF0000) >> 16);
601 data.push_back(value & 0xFFFF);
605 data.push_back(value & 0xFFFF);
606 data.push_back((value & 0xFFFF0000) >> 16);
607 data.push_back((value & 0xFFFF00000000) >> 32);
608 data.push_back((value & 0xFFFF000000000000) >> 48);
611 ESP_LOGE(TAG,
"Invalid data type for modbus number to payload conversation: %d",
612 static_cast<uint16_t>(value_type));
621 switch (sensor_value_type) {
627 value = get_data<uint32_t>(data, offset);
632 value = get_data<uint32_t>(data, offset);
633 value =
static_cast<uint32_t
>(value & 0xFFFF) << 16 | (value & 0xFFFF0000) >> 16;
644 value = get_data<uint32_t>(data, offset);
647 uint32_t sign_bit = (value & 0x8000) << 16;
649 static_cast<int32_t>(((value & 0x7FFF) << 16 | (value & 0xFFFF0000) >> 16) | sign_bit), bitmask);
654 value = get_data<uint64_t>(data, offset);
659 uint64_t tmp = get_data<uint64_t>(data, offset);
660 value = (tmp << 48) | (tmp >> 48) | ((tmp & 0xFFFF0000) << 16) | ((tmp >> 16) & 0xFFFF0000);
void queue_command(const ModbusCommandItem &command)
queues a modbus command in the send queue
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
uint8_t max_cmd_retries_
How many times we will retry a command if we get no response.
bool module_offline_
if module didn't respond the last command
std::vector< ServerRegister * > server_registers_
Collection of all server registers for this component.
CallbackManager< void(int, int)> command_sent_callback_
N mask_and_shift_by_rightbit(N data, uint32_t mask)
Extract bits from value and shift right according to the bitmask if the bitmask is 0x00F0 we want the...
void dump_config() override
ModbusRegisterType register_type
std::vector< uint16_t > float_to_payload(float value, SensorValueType value_type)
virtual size_t get_register_size() const
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...
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...
uint16_t command_throttle_
min time in ms between sending modbus commands
bool allow_duplicate_commands_
if duplicate commands can be sent
uint32_t last_command_timestamp_
when was the last send operation
std::vector< uint8_t > payload
ModbusRegisterType register_type
uint32_t IRAM_ATTR HOT millis()
bool waiting_for_response()
bool send_next_command_()
send the next modbus command from the send queue
SensorSet sensorset_
Collection of all sensors for this component.
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.
SensorSet find_sensors_(ModbusRegisterType register_type, uint16_t start_address) const
void add_on_command_sent_callback(std::function< void(int, int)> &&callback)
Set callback for commands.
void number_to_payload(std::vector< uint16_t > &data, int64_t value, SensorValueType value_type)
Convert float value to vector<uint16_t> suitable for sending.
void dump_sensors_()
dump the parsed sensormap for diagnostics
void send_raw(const std::vector< uint8_t > &payload)
constexpr14 std::array< uint8_t, sizeof(T)> decode_value(T val)
Decode a value into its constituent bytes (from most to least significant).
uint16_t skip_updates_counter
void on_modbus_data(const std::vector< uint8_t > &data) override
called when a modbus response was parsed without errors
void on_modbus_error(uint8_t function_code, uint8_t exception_code) override
called when a modbus error response was received
std::function< void(ModbusRegisterType register_type, uint16_t start_address, const std::vector< uint8_t > &data)> on_data_func
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 ...
void process_modbus_data_(const ModbusCommandItem *response)
parse incoming modbus data
std::list< std::unique_ptr< ModbusCommandItem > > command_queue_
Hold the pending requests to be sent.
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
ModbusFunctionCode modbus_register_read_function(ModbusRegisterType reg_type)
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...
int64_t payload_to_number(const std::vector< uint8_t > &data, SensorValueType sensor_value_type, uint8_t offset, uint32_t bitmask)
Convert vector<uint8_t> response payload to number.
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.
std::vector< RegisterRange > register_ranges_
Continuous range of modbus registers.
Implementation of SPI Controller mode.
uint16_t offline_skip_updates_
how many updates to skip if module is offline
void update_range_(RegisterRange &r)
submit the read command for the address range to the send queue
bool is_equal(const ModbusCommandItem &other)
void send(uint8_t function, uint16_t start_address, uint16_t number_of_entities, uint8_t payload_len=0, const uint8_t *payload=nullptr)
ModbusFunctionCode function_code
std::set< SensorItem *, SensorItemsComparator > SensorSet
std::queue< std::unique_ptr< ModbusCommandItem > > incoming_queue_
modbus response data waiting to get processed
size_t create_register_ranges_()
parse sensormap_ and create range of sequential addresses
ModbusRegisterType register_type
ModbusController * modbusdevice
uint16_t register_address
esphome::sensor::Sensor * sensor
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.