From 2a5174c5d1bbcce59bf15f0820fd40295005d254 Mon Sep 17 00:00:00 2001 From: rsformoso Date: Thu, 9 Jul 2026 10:39:35 +0800 Subject: [PATCH] Flush buffer after each command --- run_scan.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/run_scan.py b/run_scan.py index eb93f2b..d2b59d1 100644 --- a/run_scan.py +++ b/run_scan.py @@ -116,6 +116,19 @@ def rotate_stand(ser): await_completion(ser, "Waiting") +def flush_buffer(ser): + """ + Reads all currently stored bytes from the serial port until the buffer is empty. + + Args: + ser (serial.Serial): The active serial connection to the Arduino. + """ + while ser.in_waiting > 0: + line_bytes = ser.readline().decode("utf-8").strip() + line_str = line_bytes.decode("utf-8").strip() + print(f"[SERIAL] {line_str}") + + def await_completion(ser, s): """ Blocks the execution of the script until a specific confirmation @@ -132,6 +145,7 @@ def await_completion(ser, s): while True: line_bytes = ser.readline() line_str = line_bytes.decode("utf-8").strip() + print(f"[SERIAL] {line_str}") if not line_str: raise SerialTimeoutError @@ -141,6 +155,8 @@ def await_completion(ser, s): time.sleep(0.1) + flush_buffer(ser) + def main(): with serial.Serial(ARDUINO_PORT, BAUD_RATE, timeout=TIMEOUT) as ser: