mirror of
https://github.com/esphome/esphome.git
synced 2026-10-19 12:42:50 +00:00
[components] Drop stale UART receive bytes with discard_available (#21030)
Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
This commit is contained in:
co-authored by
pre-commit-ci-lite[bot]
parent
c35d98b622
commit
619728b686
@@ -134,9 +134,7 @@ void BL0906::handle_actions_() {
|
||||
}
|
||||
}
|
||||
|
||||
while (this->available()) {
|
||||
this->read();
|
||||
}
|
||||
this->discard_available();
|
||||
|
||||
this->action_queue_.clear();
|
||||
}
|
||||
|
||||
@@ -115,9 +115,7 @@ bool CM1106Component::send_command_expect_ack_(const uint8_t *command, size_t co
|
||||
|
||||
bool CM1106Component::cm1106_write_command_(const uint8_t *command, size_t command_len, uint8_t *response,
|
||||
size_t response_len) {
|
||||
// Empty RX Buffer
|
||||
while (this->available())
|
||||
this->read();
|
||||
this->discard_available();
|
||||
this->write_array(command, command_len - 1);
|
||||
this->write_byte(cm1106_checksum(command, command_len));
|
||||
this->flush();
|
||||
|
||||
@@ -116,9 +116,7 @@ void CSE7761Component::write_(uint8_t reg, uint16_t data) {
|
||||
}
|
||||
|
||||
bool CSE7761Component::read_once_(uint8_t reg, uint8_t size, uint32_t *value) {
|
||||
while (this->available()) {
|
||||
this->read();
|
||||
}
|
||||
this->discard_available();
|
||||
|
||||
this->write_(reg, 0);
|
||||
|
||||
|
||||
@@ -40,11 +40,7 @@ void DFRobotSEN0465UARTComponent::loop() {
|
||||
|
||||
bool DFRobotSEN0465UARTComponent::write_frame(const uint8_t *frame) {
|
||||
// Drop anything left over, such as the answer to the mode change or readings sent before passive mode was set
|
||||
uint8_t byte;
|
||||
while (this->available() > 0) {
|
||||
if (!this->read_byte(&byte))
|
||||
break;
|
||||
}
|
||||
this->discard_available();
|
||||
this->rx_count_ = 0;
|
||||
this->write_array(frame, FRAME_SIZE);
|
||||
return true;
|
||||
|
||||
@@ -73,7 +73,7 @@ DlmsMeterComponent::DlmsMeterComponent(const uint32_t receive_timeout_ms, const
|
||||
}
|
||||
}
|
||||
|
||||
void DlmsMeterComponent::setup() { this->flush_rx_buffer_(); }
|
||||
void DlmsMeterComponent::setup() { this->discard_available(); }
|
||||
|
||||
void DlmsMeterComponent::dump_config() {
|
||||
ESP_LOGCONFIG(TAG, "DLMS Meter:");
|
||||
@@ -122,12 +122,6 @@ void DlmsMeterComponent::loop() {
|
||||
}
|
||||
}
|
||||
|
||||
void DlmsMeterComponent::flush_rx_buffer_() {
|
||||
while (this->available()) {
|
||||
this->read();
|
||||
}
|
||||
}
|
||||
|
||||
void DlmsMeterComponent::read_rx_buffer_() {
|
||||
int available = this->available();
|
||||
if (available == 0)
|
||||
@@ -137,7 +131,7 @@ void DlmsMeterComponent::read_rx_buffer_() {
|
||||
ESP_LOGW(TAG, "RX Buffer overflow. Frame too large! Dropping frame.");
|
||||
this->bytes_accumulated_ = 0;
|
||||
|
||||
this->flush_rx_buffer_();
|
||||
this->discard_available();
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -145,7 +139,7 @@ void DlmsMeterComponent::read_rx_buffer_() {
|
||||
if (!success) {
|
||||
ESP_LOGW(TAG, "UART read failed. Dropping frame.");
|
||||
this->bytes_accumulated_ = 0;
|
||||
this->flush_rx_buffer_();
|
||||
this->discard_available();
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -104,7 +104,6 @@ class DlmsMeterComponent final : public Component, public uart::UARTDevice {
|
||||
|
||||
protected:
|
||||
void read_rx_buffer_();
|
||||
void flush_rx_buffer_();
|
||||
void process_frame_();
|
||||
void on_data_(const dlms_parser::AxdrCapture &capture);
|
||||
|
||||
|
||||
@@ -362,8 +362,7 @@ void FingerprintGrowComponent::aura_led_control(uint8_t state, uint8_t speed, ui
|
||||
}
|
||||
|
||||
uint8_t FingerprintGrowComponent::transfer_(std::vector<uint8_t> &data_buffer) {
|
||||
while (this->available())
|
||||
this->read();
|
||||
this->discard_available();
|
||||
this->write((uint8_t) (START_CODE >> 8));
|
||||
this->write((uint8_t) (START_CODE & 0xFF));
|
||||
this->write(this->address_[0]);
|
||||
|
||||
@@ -18,9 +18,7 @@ void HC8Component::setup() {
|
||||
this->write_array(HC8_COMMAND_GET_PPM);
|
||||
this->flush();
|
||||
|
||||
// ensure the buffer is empty
|
||||
while (this->available())
|
||||
this->read();
|
||||
this->discard_available();
|
||||
}
|
||||
|
||||
void HC8Component::update() {
|
||||
@@ -36,8 +34,7 @@ void HC8Component::update() {
|
||||
this->status_clear_warning();
|
||||
}
|
||||
|
||||
while (this->available())
|
||||
this->read();
|
||||
this->discard_available();
|
||||
|
||||
this->write_array(HC8_COMMAND_GET_PPM);
|
||||
this->flush();
|
||||
|
||||
@@ -11,9 +11,7 @@ static constexpr uint32_t PAYLOAD_TIMEOUT_MS = 20;
|
||||
void HlkFm22xComponent::setup() {
|
||||
ESP_LOGCONFIG(TAG, "Setting up HLK-FM22X...");
|
||||
this->set_enrolling_(false);
|
||||
while (this->available() > 0) {
|
||||
this->read();
|
||||
}
|
||||
this->discard_available();
|
||||
this->defer([this]() { this->send_command_(HlkFm22xCommand::GET_STATUS); });
|
||||
}
|
||||
|
||||
@@ -85,8 +83,7 @@ void HlkFm22xComponent::send_command_(HlkFm22xCommand command, const uint8_t *da
|
||||
}
|
||||
this->wait_cycles_ = 0;
|
||||
this->active_command_ = command;
|
||||
while (this->available() > 0)
|
||||
this->read();
|
||||
this->discard_available();
|
||||
this->write((uint8_t) (START_CODE >> 8));
|
||||
this->write((uint8_t) (START_CODE & 0xFF));
|
||||
this->write((uint8_t) command);
|
||||
@@ -141,9 +138,7 @@ void HlkFm22xComponent::recv_command_() {
|
||||
if (millis() - start > PAYLOAD_TIMEOUT_MS) {
|
||||
ESP_LOGE(TAG, "Timeout waiting for payload (%u bytes)", length);
|
||||
// Drain any partial payload bytes to resync the parser
|
||||
while (this->available() > 0) {
|
||||
this->read();
|
||||
}
|
||||
this->discard_available();
|
||||
return;
|
||||
}
|
||||
delay(1);
|
||||
|
||||
@@ -38,9 +38,7 @@ void HydreonRGxxComponent::dump_config() {
|
||||
}
|
||||
|
||||
void HydreonRGxxComponent::setup() {
|
||||
while (this->available() != 0) {
|
||||
this->read();
|
||||
}
|
||||
this->discard_available();
|
||||
this->schedule_reboot_();
|
||||
}
|
||||
|
||||
|
||||
@@ -120,9 +120,7 @@ void MHZ19Component::range_set(MHZ19DetectionRange detection_range) {
|
||||
}
|
||||
|
||||
bool MHZ19Component::mhz19_write_command_(const uint8_t *command, uint8_t *response) {
|
||||
// Empty RX Buffer
|
||||
while (this->available())
|
||||
this->read();
|
||||
this->discard_available();
|
||||
this->write_array(command, MHZ19_REQUEST_LENGTH);
|
||||
this->write_byte(mhz19_checksum(command));
|
||||
this->flush();
|
||||
|
||||
@@ -99,11 +99,7 @@ void PylontechComponent::dump_config() {
|
||||
LOG_UPDATE_INTERVAL(this);
|
||||
}
|
||||
|
||||
void PylontechComponent::setup() {
|
||||
while (this->available() != 0) {
|
||||
this->read();
|
||||
}
|
||||
}
|
||||
void PylontechComponent::setup() { this->discard_available(); }
|
||||
|
||||
void PylontechComponent::update() {
|
||||
#ifdef PYLONTECH_CELL_BATTERY_COUNT
|
||||
|
||||
@@ -9,9 +9,7 @@ namespace esphome::pzem004t {
|
||||
ESPHOME_LOG_TAG(TAG, "pzem004t");
|
||||
|
||||
void PZEM004T::setup() {
|
||||
// Clear UART buffer
|
||||
while (this->available())
|
||||
this->read();
|
||||
this->discard_available();
|
||||
// Set module address
|
||||
this->write_state_(SET_ADDRESS);
|
||||
}
|
||||
@@ -19,8 +17,7 @@ void PZEM004T::setup() {
|
||||
void PZEM004T::loop() {
|
||||
const uint32_t now = App.get_loop_component_start_time();
|
||||
if (now - this->last_read_ > 500 && this->available() < 7) {
|
||||
while (this->available())
|
||||
this->read();
|
||||
this->discard_available();
|
||||
this->last_read_ = now;
|
||||
}
|
||||
|
||||
|
||||
@@ -483,10 +483,7 @@ void ShellyDimmer::reset_(bool boot0) {
|
||||
// Wait 50ms for the STM32 to reset.
|
||||
delay(50); // NOLINT
|
||||
|
||||
// Clear receive buffer.
|
||||
while (this->available()) {
|
||||
this->read();
|
||||
}
|
||||
this->discard_available();
|
||||
|
||||
this->pin_nrst_->digital_write(true);
|
||||
// Wait 50ms for the STM32 to boot.
|
||||
|
||||
@@ -32,8 +32,7 @@ void T6615Component::loop() {
|
||||
if (this->available() < 5) {
|
||||
if (this->command_ == T6615Command::GET_PPM && millis() - this->command_time_ > T6615_TIMEOUT) {
|
||||
/* command got eaten, clear the buffer and fire another */
|
||||
while (this->available())
|
||||
this->read();
|
||||
this->discard_available();
|
||||
this->send_ppm_command_();
|
||||
}
|
||||
return;
|
||||
@@ -48,9 +47,7 @@ void T6615Component::loop() {
|
||||
if (response_buffer[0] != T6615_MAGIC || response_buffer[1] != T6615_ADDR_HOST) {
|
||||
ESP_LOGW(TAG, "Got bad data from T6615! Magic was %02X and address was %02X", response_buffer[0],
|
||||
response_buffer[1]);
|
||||
/* make sure the buffer is empty */
|
||||
while (this->available())
|
||||
this->read();
|
||||
this->discard_available();
|
||||
/* try again to read the sensor */
|
||||
this->send_ppm_command_();
|
||||
this->status_set_warning();
|
||||
|
||||
@@ -446,8 +446,7 @@ bool TMC22XXStepper::write_register(uint8_t reg, uint32_t value) {
|
||||
0};
|
||||
datagram[7] = datagram_crc(datagram, 7);
|
||||
|
||||
while (this->available())
|
||||
this->read();
|
||||
this->discard_available();
|
||||
this->write_array(datagram, DATAGRAM_SIZE);
|
||||
this->flush();
|
||||
// On the single wire bus every byte sent is also received, drop that echo
|
||||
@@ -475,8 +474,7 @@ optional<uint32_t> TMC22XXStepper::read_register(uint8_t reg) {
|
||||
uint8_t request[READ_REQUEST_SIZE] = {SYNC_BYTE, this->address_, reg, 0};
|
||||
request[3] = datagram_crc(request, 3);
|
||||
|
||||
while (this->available())
|
||||
this->read();
|
||||
this->discard_available();
|
||||
this->write_array(request, READ_REQUEST_SIZE);
|
||||
this->flush();
|
||||
uint8_t reply[DATAGRAM_SIZE];
|
||||
|
||||
Reference in New Issue
Block a user