mirror of
https://github.com/esphome/esphome.git
synced 2026-02-08 00:31:58 +00:00
[modbus] Batch UART reads to reduce loop overhead
This commit is contained in:
@@ -19,16 +19,27 @@ void Modbus::setup() {
|
|||||||
void Modbus::loop() {
|
void Modbus::loop() {
|
||||||
const uint32_t now = App.get_loop_component_start_time();
|
const uint32_t now = App.get_loop_component_start_time();
|
||||||
|
|
||||||
while (this->available()) {
|
int avail = this->available();
|
||||||
uint8_t byte;
|
if (avail > 0) {
|
||||||
this->read_byte(&byte);
|
// Read all available bytes in batches to reduce UART call overhead.
|
||||||
if (this->parse_modbus_byte_(byte)) {
|
uint8_t buf[64];
|
||||||
this->last_modbus_byte_ = now;
|
while (avail > 0) {
|
||||||
} else {
|
size_t to_read = std::min(static_cast<size_t>(avail), sizeof(buf));
|
||||||
size_t at = this->rx_buffer_.size();
|
if (!this->read_array(buf, to_read)) {
|
||||||
if (at > 0) {
|
break;
|
||||||
ESP_LOGV(TAG, "Clearing buffer of %d bytes - parse failed", at);
|
}
|
||||||
this->rx_buffer_.clear();
|
avail -= to_read;
|
||||||
|
|
||||||
|
for (size_t i = 0; i < to_read; i++) {
|
||||||
|
if (this->parse_modbus_byte_(buf[i])) {
|
||||||
|
this->last_modbus_byte_ = now;
|
||||||
|
} else {
|
||||||
|
size_t at = this->rx_buffer_.size();
|
||||||
|
if (at > 0) {
|
||||||
|
ESP_LOGV(TAG, "Clearing buffer of %d bytes - parse failed", at);
|
||||||
|
this->rx_buffer_.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user