From 0288b493190511da608b823735eb8e2b0da9ff38 Mon Sep 17 00:00:00 2001 From: clerie Date: Fri, 27 Dec 2024 12:59:30 +0100 Subject: [PATCH] Make sure there are no pending packages in buffer before sending one --- src/main.rs | 8 ++++++++ src/serial.rs | 5 +++++ 2 files changed, 13 insertions(+) diff --git a/src/main.rs b/src/main.rs index f6afd3c..3f92bd5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -90,6 +90,8 @@ async fn main() -> Result<()>{ let mut serial_interface = serial::SerialInterface::new(path, *baud_rate).context("Couldn't init serial interface")?; + serial_interface.drain_buffer(); + serial_interface.send(&request_current_state_packet).context("Failed to send improv packet")?; let result_bytes = serial_interface.recv_bytes().context("Couldn't receive any improv packet")?; @@ -111,6 +113,8 @@ async fn main() -> Result<()>{ let mut serial_interface = serial::SerialInterface::new(path, *baud_rate).context("Couldn't init serial interface")?; + serial_interface.drain_buffer(); + serial_interface.send(&send_wifi_settings_packet).context("Failed to send improv packet")?; let result_bytes = serial_interface.recv_bytes().context("Couldn't receive any improv packet")?; @@ -127,6 +131,8 @@ async fn main() -> Result<()>{ let mut serial_interface = serial::SerialInterface::new(path, *baud_rate).context("Couldn't init serial interface")?; + serial_interface.drain_buffer(); + serial_interface.send(&request_device_information_packet).context("Failed to send improv packet")?; let result_bytes = serial_interface.recv_bytes().context("Couldn't receive any improv packet")?; @@ -143,6 +149,8 @@ async fn main() -> Result<()>{ let mut serial_interface = serial::SerialInterface::new(path, *baud_rate).context("Couldn't init serial interface")?; + serial_interface.drain_buffer(); + serial_interface.send(&request_scanned_wifi_networks_packet).context("Failed to send improv packet")?; loop { diff --git a/src/serial.rs b/src/serial.rs index 09054ac..adb6f71 100644 --- a/src/serial.rs +++ b/src/serial.rs @@ -127,6 +127,11 @@ impl SerialInterface { Ok(()) } + pub fn drain_buffer(&mut self) { + let _ = self.fill_buffer(); + self.buffer = Vec::new(); + } + pub fn recv_bytes(&mut self) -> Result> { let retry_counter = 0;