6 namespace modbus_controller {
8 static const char *
const TAG =
"modbus_controller";
25 if (command->send_countdown < 1) {
27 ESP_LOGW(TAG,
"Modbus device=%d set offline", this->
address_);
39 "Modbus command to device=%d register=0x%02X countdown=%d no response received - removed from send queue",
40 this->
address_, command->register_address, command->send_countdown);
43 ESP_LOGV(TAG,
"Sending next modbus command to device %d register 0x%02X count %d", this->
address_,
44 command->register_address, command->register_count);
48 if (!command->on_data_func) {
59 if (current_command !=
nullptr) {
61 ESP_LOGW(TAG,
"Modbus device=%d back online", this->
address_);
66 r.skip_updates_counter = 0;
73 current_command->payload = data;
75 ESP_LOGV(TAG,
"Modbus response queued");
82 ESP_LOGV(TAG,
"Process modbus response for address 0x%X size: %zu", response->
register_address,
88 ESP_LOGE(TAG,
"Modbus error function code: 0x%X exception: %d ", function_code, exception_code);
91 if (current_command !=
nullptr) {
93 "Modbus error - last command: function code=0x%X register address = 0x%X " 96 function_code, current_command->register_address, current_command->register_count,
97 current_command->payload.size());
103 uint16_t number_of_registers) {
105 "Received read holding/input registers for device 0x%X. FC: 0x%X. Start address: 0x%X. Number of registers: " 107 this->
address_, function_code, start_address, number_of_registers);
109 std::vector<uint16_t> sixteen_bit_response;
110 for (uint16_t current_address = start_address; current_address < start_address + number_of_registers;) {
113 if (server_register->address == current_address) {
114 float value = server_register->read_lambda();
116 ESP_LOGD(TAG,
"Matched register. Address: 0x%02X. Value type: %zu. Register count: %u. Value: %0.1f.",
117 server_register->address, static_cast<uint8_t>(server_register->value_type),
118 server_register->register_count, value);
119 std::vector<uint16_t> payload =
float_to_payload(value, server_register->value_type);
120 sixteen_bit_response.insert(sixteen_bit_response.end(), payload.cbegin(), payload.cend());
121 current_address += server_register->register_count;
128 ESP_LOGW(TAG,
"Could not match any register to address %02X. Sending exception response.", current_address);
129 std::vector<uint8_t> error_response;
130 error_response.push_back(this->
address_);
131 error_response.push_back(0x81);
132 error_response.push_back(0x02);
138 std::vector<uint8_t> response;
139 for (
auto v : sixteen_bit_response) {
141 response.push_back(decoded_value[0]);
142 response.push_back(decoded_value[1]);
145 this->
send(function_code, start_address, number_of_registers, response.size(), response.data());
154 ESP_LOGE(TAG,
"No matching range for sensor found - start_address : 0x%X", start_address);
156 return reg_it->sensors;
163 const std::vector<uint8_t> &data) {
164 ESP_LOGV(TAG,
"data for register address : 0x%X : ", start_address);
168 for (
auto *sensor : sensors) {
169 sensor->parse_and_publish(data);
177 if (item->is_equal(command)) {
178 ESP_LOGW(TAG,
"Duplicate modbus command found: type=0x%x address=%u count=%u",
182 item->payload = command.
payload;
186 command_queue_.push_back(make_unique<ModbusCommandItem>(command));
196 if (!sensors.empty()) {
197 auto sensor = sensors.cbegin();
199 this, (*sensor)->custom_data,
200 [
this](
ModbusRegisterType register_type, uint16_t start_address,
const std::vector<uint8_t> &data) {
201 this->on_register_data(ModbusRegisterType::CUSTOM, start_address, data);
203 command_item.register_address = (*sensor)->start_address;
204 command_item.register_count = (*sensor)->register_count;
222 ESP_LOGV(TAG,
"%zu modbus commands already in queue",
command_queue_.size());
224 ESP_LOGV(TAG,
"Updating modbus component");
228 ESP_LOGVV(TAG,
"Updating range 0x%X", r.start_address);
237 ESP_LOGW(TAG,
"No sensors registered");
244 uint8_t buffer_offset = 0;
262 ESP_LOGV(TAG,
"Started new range");
282 ESP_LOGV(TAG,
"Re-use previous register - change to register: 0x%X %d offset=%u", curr->
start_address,
291 curr->
offset += buffer_offset;
299 ESP_LOGV(TAG,
"Extend range - change to register: 0x%X %d offset=%u", curr->
start_address,
341 ESP_LOGCONFIG(TAG,
"ModbusController:");
342 ESP_LOGCONFIG(TAG,
" Address: 0x%02X", this->
address_);
343 #if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE 344 ESP_LOGCONFIG(TAG,
"sensormap");
346 ESP_LOGCONFIG(TAG,
" Sensor type=%zu start=0x%X offset=0x%X count=%d size=%d",
347 static_cast<uint8_t>(it->register_type), it->start_address, it->offset, it->register_count,
348 it->get_register_size());
350 ESP_LOGCONFIG(TAG,
"ranges");
352 ESP_LOGCONFIG(TAG,
" Range type=%zu start=0x%X count=%d skip_updates=%d", static_cast<uint8_t>(it.register_type),
353 it.start_address, it.register_count, it.skip_updates);
355 ESP_LOGCONFIG(TAG,
"server registers");
357 ESP_LOGCONFIG(TAG,
" Address=0x%02X value_type=%zu register_count=%u", r->address,
358 static_cast<uint8_t>(r->value_type), r->register_count);
367 if (message !=
nullptr)
378 const std::vector<uint8_t> &data) {
379 ESP_LOGV(TAG,
"Command ACK 0x%X %d ", get_data<uint16_t>(data, 0), get_data<int16_t>(data, 1));
383 ESP_LOGV(TAG,
"sensors");
385 ESP_LOGV(TAG,
" Sensor start=0x%X count=%d size=%d offset=%d", it->start_address, it->register_count,
386 it->get_register_size(), it->offset);
392 std::function<
void(
ModbusRegisterType register_type, uint16_t start_address,
const std::vector<uint8_t> &data)>
406 uint16_t register_count) {
414 const std::vector<uint8_t> &data) {
421 uint16_t start_address, uint16_t register_count,
422 const std::vector<uint16_t> &values) {
430 const std::vector<uint8_t> &data) {
433 for (
auto v : values) {
435 cmd.
payload.push_back(decoded_value[0]);
436 cmd.
payload.push_back(decoded_value[1]);
450 const std::vector<uint8_t> &data) {
453 cmd.
payload.push_back(value ? 0xFF : 0);
459 const std::vector<bool> &values) {
467 const std::vector<uint8_t> &data) {
473 for (
auto coil : values) {
475 bitmask |= (1 << bitcounter);
478 if (bitcounter % 8 == 0) {
479 cmd.
payload.push_back(bitmask);
484 if (bitcounter % 8) {
485 cmd.
payload.push_back(bitmask);
499 const std::vector<uint8_t> &data) {
504 cmd.
payload.push_back(decoded_value[0]);
505 cmd.
payload.push_back(decoded_value[1]);
511 std::function<
void(
ModbusRegisterType register_type, uint16_t start_address,
const std::vector<uint8_t> &data)>
516 if (handler ==
nullptr) {
518 ESP_LOGI(TAG,
"Custom Command sent");
530 std::function<
void(
ModbusRegisterType register_type, uint16_t start_address,
const std::vector<uint8_t> &data)>
535 if (handler ==
nullptr) {
537 ESP_LOGI(TAG,
"Custom Command sent");
542 for (
auto v : values) {
543 cmd.
payload.push_back((v >> 8) & 0xFF);
544 cmd.
payload.push_back(v & 0xFF);
552 modbusdevice->
send(uint8_t(this->function_code), this->register_address, this->register_count, this->payload.size(),
553 this->payload.empty() ? nullptr : &this->payload[0]);
555 modbusdevice->
send_raw(this->payload);
557 ESP_LOGV(TAG,
"Command sent %d 0x%X %d", uint8_t(this->function_code), this->register_address, this->register_count);
566 ? this->payload == other.
payload 572 switch (value_type) {
575 data.push_back(value & 0xFFFF);
580 data.push_back((value & 0xFFFF0000) >> 16);
581 data.push_back(value & 0xFFFF);
586 data.push_back(value & 0xFFFF);
587 data.push_back((value & 0xFFFF0000) >> 16);
591 data.push_back((value & 0xFFFF000000000000) >> 48);
592 data.push_back((value & 0xFFFF00000000) >> 32);
593 data.push_back((value & 0xFFFF0000) >> 16);
594 data.push_back(value & 0xFFFF);
598 data.push_back(value & 0xFFFF);
599 data.push_back((value & 0xFFFF0000) >> 16);
600 data.push_back((value & 0xFFFF00000000) >> 32);
601 data.push_back((value & 0xFFFF000000000000) >> 48);
604 ESP_LOGE(TAG,
"Invalid data type for modbus number to payload conversation: %d",
605 static_cast<uint16_t>(value_type));
614 switch (sensor_value_type) {
620 value = get_data<uint32_t>(data, offset);
625 value = get_data<uint32_t>(data, offset);
626 value =
static_cast<uint32_t
>(value & 0xFFFF) << 16 | (value & 0xFFFF0000) >> 16;
637 value = get_data<uint32_t>(data, offset);
640 uint32_t sign_bit = (value & 0x8000) << 16;
642 static_cast<int32_t>(((value & 0x7FFF) << 16 | (value & 0xFFFF0000) >> 16) | sign_bit), bitmask);
647 value = get_data<uint64_t>(data, offset);
652 uint64_t tmp = get_data<uint64_t>(data, offset);
653 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
bool module_offline_
if module didn't respond the last command
std::vector< ServerRegister * > server_registers_
Collection of all server registers for this component.
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
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 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
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.