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,10 +19,19 @@ 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];
|
||||||
|
while (avail > 0) {
|
||||||
|
size_t to_read = std::min(static_cast<size_t>(avail), sizeof(buf));
|
||||||
|
if (!this->read_array(buf, to_read)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
avail -= to_read;
|
||||||
|
|
||||||
|
for (size_t i = 0; i < to_read; i++) {
|
||||||
|
if (this->parse_modbus_byte_(buf[i])) {
|
||||||
this->last_modbus_byte_ = now;
|
this->last_modbus_byte_ = now;
|
||||||
} else {
|
} else {
|
||||||
size_t at = this->rx_buffer_.size();
|
size_t at = this->rx_buffer_.size();
|
||||||
@@ -32,6 +41,8 @@ void Modbus::loop() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (now - this->last_modbus_byte_ > 50) {
|
if (now - this->last_modbus_byte_ > 50) {
|
||||||
size_t at = this->rx_buffer_.size();
|
size_t at = this->rx_buffer_.size();
|
||||||
|
|||||||
Reference in New Issue
Block a user