From 63da058f899963313b4eeb0c842fd763898f1b45 Mon Sep 17 00:00:00 2001 From: user Date: Tue, 14 Nov 2023 22:25:26 -0500 Subject: [PATCH] update --- components/victron/victron.cpp | 88 +++++++++++++++++----------------- components/victron/victron.h | 1 + 2 files changed, 44 insertions(+), 45 deletions(-) diff --git a/components/victron/victron.cpp b/components/victron/victron.cpp index c9c6bf9..e068f1c 100644 --- a/components/victron/victron.cpp +++ b/components/victron/victron.cpp @@ -93,61 +93,59 @@ void VictronComponent::loop() { ESP_LOGW(TAG, "Last transmission too long ago"); state_ = 0; } - if (!available()) return; last_transmission_ = now; - while (available()) { - uint8_t c; - read_byte(&c); - if (state_ == 0) { - if (c == '\r' || c == '\n') { - continue; - } - label_.clear(); - value_.clear(); - state_ = 1; + uint8_t c; + read_byte(&c); + if (state_ == 0) { + if (c == '\r' || c == '\n') { + return; } - if (state_ == 1) { - // Start of a ve.direct hex frame - if (c == ':') { - state_ = 3; - continue; - } - if (c == '\t') { - state_ = 2; - } else { - label_.push_back(c); - } - continue; + label_.clear(); + value_.clear(); + state_ = 1; + begin_frame_ = now; + } + if (state_ == 1) { + // Start of a ve.direct hex frame + if (c == ':') { + state_ = 3; + return; } - if (state_ == 2) { - if (label_ == "Checksum") { - state_ = 0; - // The checksum is used as end of frame indicator - if (now - this->last_publish_ >= this->throttle_) { - this->last_publish_ = now; - this->publishing_ = true; - } else { - this->publishing_ = false; - } - continue; - } - if (c == '\r' || c == '\n') { - if (this->publishing_) { - handle_value_(); - } - state_ = 0; + if (c == '\t') { + state_ = 2; + } else { + label_.push_back(c); + } + return; + } + if (state_ == 2) { + if (label_ == "Checksum") { + state_ = 0; + // The checksum is used as end of frame indicator + if (begin_frame_ - this->last_publish_ >= this->throttle_) { + this->last_publish_ = begin_frame_; + this->publishing_ = true; } else { - value_.push_back(c); + this->publishing_ = false; } + return; } - // Discard ve.direct hex frame - if (state_ == 3) { - if (c == '\r' || c == '\n') { - state_ = 0; + if (c == '\r' || c == '\n') { + if (this->publishing_) { + handle_value_(); } + state_ = 0; + } else { + value_.push_back(c); + } + } + // Discard ve.direct hex frame + if (state_ == 3) { + if (c == '\r' || c == '\n') { + state_ = 0; } } } diff --git a/components/victron/victron.h b/components/victron/victron.h index 9221006..fd74d7a 100644 --- a/components/victron/victron.h +++ b/components/victron/victron.h @@ -287,6 +287,7 @@ class VictronComponent : public uart::UARTDevice, public Component { int state_{0}; std::string label_; std::string value_; + uint32_t begin_frame_{0}; uint32_t last_transmission_{0}; uint32_t last_publish_{0}; uint32_t throttle_{0};