pyserial when it is the end of the line stop the w

2019-09-14 18:41发布

问题:

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

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

It only works for 1 line.

回答1:

Wait for it to timeout without any data, then you know it is done.

For example:

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