pySerial 2.6: specify end-of-line in readline()

后端 未结 5 547
独厮守ぢ
独厮守ぢ 2020-11-28 11:33

I am sending commands to Eddie using pySerial. I need to specify a carriage-return in my readline, but pySerial 2.6 got rid of it... Is there a workaround?

Here are

5条回答
  •  攒了一身酷
    2020-11-28 12:15

    from pyserial's documentation:

    (sic)

    Note:

    The eol parameter for readline() is no longer supported when pySerial is run with newer Python versions (V2.6+) where the module io is available. EOL

    To specify the EOL character for readline() or to use universal newline mode, it is advised to use io.TextIOWrapper:

    import serial
    import io
    ser = serial.serial_for_url('loop://', timeout=1)
    sio = io.TextIOWrapper(io.BufferedRWPair(ser, ser))
    
    sio.write(unicode("hello\n"))
    sio.flush() # it is buffering. required to get the data out *now*
    hello = sio.readline()
    print hello == unicode("hello\n")
    

提交回复
热议问题