Imagine we have a thread reading a temperature sensor 10 times per second via USB serial port.
And, when a user presses a button, we can change the sensor mode, also via USB serial.
Problem: the change_sensor_mode()
serial write operation can happen in the middle of the polling thread "write/read_until" serial operation, and this causes serial data corruption.
How to handle this general multithreading problem with Python serial
?
Example:
import threading, serial, time
serial_port = serial.Serial(port="COM2", timeout=1.0)
def poll_temperature():
while True:
serial_port.write(b"GETTEMP\r\n")
recv = serial_port.read_until(b"END")
print(recv)
time.sleep(0.2)
def change_sensor_mode():
serial_port.write(b"CHANGEMODE2\r\n")
t1 = threading.Thread(target=poll_temperature)
t1.start()
time.sleep(4.12342) # simulate waiting for user input
change_sensor_mode()
I was thinking about adding a manual lock = True
/ lock = False
before/after the serial operation in poll_temperature
, and then the main thread should wait:
global lock
def change_sensor_mode():
elapsed = 0
t0 = time.time()
while lock and elapsed < 1.0:
time.sleep(0.010)
elapsed += 0.010
serial_port.write(b"CHANGEMODE2\r\n")
but there is surely a more standard way to achieve this. How to handle multiple threads writing to serial
?