Flush buffer after each command
This commit is contained in:
+16
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user