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

前端 未结 10 1808
再見小時候
再見小時候 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 08:02

    These solutions will hog the CPU while waiting for characters.

    You should do at least one blocking call to read(1)

    while True:
        if '\n' in buffer: 
            pass # skip if a line already in buffer
        else:
            buffer += ser.read(1)  # this will block until one more char or timeout
        buffer += ser.read(ser.inWaiting()) # get remaining buffered chars
    

    ...and do the split thing as before.

提交回复
热议问题