本文介绍了pyserial,当它是行的结尾时,停止while循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
import serial
arduino = serial.Serial('COM12', 9600, timeout = .1)
arduino_data = [] # declare a list
while True:
data = arduino.readline()
if data:
arduino_data.append(data) # Append a data to your declared list
print arduino_data
我想知道从Arduino端不再有新的生产线了,我该如何跳出while循环?
I wonder when there is no more new line from Arduino side, how can i jump out the while loop?
while True:
data = arduino.readline()
if data:
arduino_data.append(data) # Append a data to your declared list
print arduino_data
break
仅适用于1行.
推荐答案
等待它超时而没有任何数据,那么您就知道它已经完成了.
Wait for it to timeout without any data, then you know it is done.
例如:
import serial
arduino = serial.Serial('COM12', 9600, timeout = .1)
while True:
data = arduino.readline()
if data:
arduino_data.append(data) # Append a data to your declared list
print(arduino_data)
else:
break
这篇关于pyserial,当它是行的结尾时,停止while循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!