pyserial - How to read the last line sent from a serial device

前端 未结 10 1811
再見小時候
再見小時候 2020-12-02 07:12

I have an Arduino connected to my computer running a loop, sending a value over the serial port back to the computer every 100 ms.

I want to make a Python scr

10条回答
  •  执念已碎
    2020-12-02 07:58

    You will need a loop to read everything sent, with the last call to readline() blocking until the timeout. So:

    def readLastLine(ser):
        last_data=''
        while True:
            data=ser.readline()
            if data!='':
                last_data=data
            else:
                return last_data
    

提交回复
热议问题