pyserial when it is the end of the line stop the while loop

夙愿已清 提交于 2019-12-25 05:20:43

问题


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


来源:https://stackoverflow.com/questions/42966263/pyserial-when-it-is-the-end-of-the-line-stop-the-while-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!