pyserial当它的行结束时停止while循环

时间:2017-03-23 02:38:54

标签: python arduino pyserial

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循环呢?

while True:
    data = arduino.readline()
    if data:
        arduino_data.append(data) # Append a data to your declared list
        print arduino_data
        break

它仅适用于1行。

1 个答案:

答案 0 :(得分:1)

等待它没有任何数据就超时,然后你知道它已经完成了。

例如:

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