如何更改 pySerial 中的行终止符(行尾字符,EOL)

How can I change the line terminator (end-of-line character, EOL) in pySerial

如何在当前版本 (>3.4) 的 PySerial 中更改行尾(EOL,有时称为 TERMINATOR)字符? short intro advises to use io.TextWrapper,但我从未使用过 io 模块,简短介绍中给出的示例与我的用例相去甚远。有更简单的方法吗?类似于 Matlab 的

s = serial('COM3');
s.Terminator = 'CR';
s.open()

我只想在使用 CR 指示换行符的设备上执行 readline()

调整您 link 中的示例,添加 newline 参数,如文档中所述:

>>> help(io.TextIOWrapper)
Help on class TextIOWrapper in module io:

class TextIOWrapper(_TextIOBase)
 ...
 |  
 |  newline controls how line endings are handled. It can be None, '',
 |  '\n', '\r', and '\r\n'.

修改后的样本:

import serial
import io
ser = serial.serial_for_url('loop://', timeout=1)
sio = io.TextIOWrapper(io.BufferedRWPair(ser, ser),newline="\r")

现在 readline 在遇到 \r 字符时停止。