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

前端 未结 10 1807
再見小時候
再見小時候 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:04

    Too much complications

    What is the reason to split the bytes object by newline or by other array manipulations? I write the simplest method, which will solve your problem:

    import serial
    s = serial.Serial(31)
    s.write(bytes("ATI\r\n", "utf-8"));
    while True:
        last = ''
        for byte in s.read(s.inWaiting()): last += chr(byte)
        if len(last) > 0:
            # Do whatever you want with last
            print (bytes(last, "utf-8"))
            last = ''
    

提交回复
热议问题