mirror of
https://github.com/esphome/esphome.git
synced 2024-11-21 22:48:10 +01:00
Fixes modbus timing error (#7674)
This commit is contained in:
parent
0dab280440
commit
aa0e155e22
1 changed files with 24 additions and 12 deletions
|
@ -15,23 +15,33 @@ void Modbus::setup() {
|
||||||
void Modbus::loop() {
|
void Modbus::loop() {
|
||||||
const uint32_t now = millis();
|
const uint32_t now = millis();
|
||||||
|
|
||||||
if (now - this->last_modbus_byte_ > 50) {
|
|
||||||
this->rx_buffer_.clear();
|
|
||||||
this->last_modbus_byte_ = now;
|
|
||||||
}
|
|
||||||
// stop blocking new send commands after send_wait_time_ ms regardless if a response has been received since then
|
|
||||||
if (now - this->last_send_ > send_wait_time_) {
|
|
||||||
waiting_for_response = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
while (this->available()) {
|
while (this->available()) {
|
||||||
uint8_t byte;
|
uint8_t byte;
|
||||||
this->read_byte(&byte);
|
this->read_byte(&byte);
|
||||||
if (this->parse_modbus_byte_(byte)) {
|
if (this->parse_modbus_byte_(byte)) {
|
||||||
this->last_modbus_byte_ = now;
|
this->last_modbus_byte_ = now;
|
||||||
} else {
|
} 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (now - this->last_modbus_byte_ > 50) {
|
||||||
|
size_t at = this->rx_buffer_.size();
|
||||||
|
if (at > 0) {
|
||||||
|
ESP_LOGV(TAG, "Clearing buffer of %d bytes - timeout", at);
|
||||||
this->rx_buffer_.clear();
|
this->rx_buffer_.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// stop blocking new send commands after sent_wait_time_ ms after response received
|
||||||
|
if (now - this->last_send_ > send_wait_time_) {
|
||||||
|
if (waiting_for_response > 0)
|
||||||
|
ESP_LOGV(TAG, "Stop waiting for response from %d", waiting_for_response);
|
||||||
|
waiting_for_response = 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -39,7 +49,7 @@ bool Modbus::parse_modbus_byte_(uint8_t byte) {
|
||||||
size_t at = this->rx_buffer_.size();
|
size_t at = this->rx_buffer_.size();
|
||||||
this->rx_buffer_.push_back(byte);
|
this->rx_buffer_.push_back(byte);
|
||||||
const uint8_t *raw = &this->rx_buffer_[0];
|
const uint8_t *raw = &this->rx_buffer_[0];
|
||||||
ESP_LOGV(TAG, "Modbus received Byte %d (0X%x)", byte, byte);
|
ESP_LOGVV(TAG, "Modbus received Byte %d (0X%x)", byte, byte);
|
||||||
// Byte 0: modbus address (match all)
|
// Byte 0: modbus address (match all)
|
||||||
if (at == 0)
|
if (at == 0)
|
||||||
return true;
|
return true;
|
||||||
|
@ -144,8 +154,10 @@ bool Modbus::parse_modbus_byte_(uint8_t byte) {
|
||||||
ESP_LOGW(TAG, "Got Modbus frame from unknown address 0x%02X! ", address);
|
ESP_LOGW(TAG, "Got Modbus frame from unknown address 0x%02X! ", address);
|
||||||
}
|
}
|
||||||
|
|
||||||
// return false to reset buffer
|
// reset buffer
|
||||||
return false;
|
ESP_LOGV(TAG, "Clearing buffer of %d bytes - parse succeeded", at);
|
||||||
|
this->rx_buffer_.clear();
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Modbus::dump_config() {
|
void Modbus::dump_config() {
|
||||||
|
|
Loading…
Reference in a new issue