使用线程和队列。
这是一个例子:
from Queue import Queue
import threading
import serial
def readline(ser,output):
threadLock.acquire()# Get lock to synchronize threads
output.put(ser.readline())
threadLock.release()# Free lock to release next thread
# this will go on forever until you kill the program
if __name__=='__main__':
output = Queue()
with serial.Serial(com,baudrate=115200,timeout=1) as ser:
ser_thread = threading.Thread(target=readline, args=(ser,output,))
ser_thread.start()
while(True):
threadLock.acquire()#wait until lock is free
try:
data=output.get()
threadLock.release()# Free lock to release next thread
# do somthing with data