Flush buffer after each command

This commit is contained in:
2026-07-09 10:39:35 +08:00
parent 55c70ca2a8
commit 2a5174c5d1
+16
View File
@@ -116,6 +116,19 @@ def rotate_stand(ser):
await_completion(ser, "Waiting") 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): def await_completion(ser, s):
""" """
Blocks the execution of the script until a specific confirmation Blocks the execution of the script until a specific confirmation
@@ -132,6 +145,7 @@ def await_completion(ser, s):
while True: while True:
line_bytes = ser.readline() line_bytes = ser.readline()
line_str = line_bytes.decode("utf-8").strip() line_str = line_bytes.decode("utf-8").strip()
print(f"[SERIAL] {line_str}")
if not line_str: if not line_str:
raise SerialTimeoutError raise SerialTimeoutError
@@ -141,6 +155,8 @@ def await_completion(ser, s):
time.sleep(0.1) time.sleep(0.1)
flush_buffer(ser)
def main(): def main():
with serial.Serial(ARDUINO_PORT, BAUD_RATE, timeout=TIMEOUT) as ser: with serial.Serial(ARDUINO_PORT, BAUD_RATE, timeout=TIMEOUT) as ser: