我正在尝试为 SiRF 二进制消息设置换行符,但 IO 包装器似乎不接受换行符。
代码:
import serial
import io
port = serial.Serial(port='/dev/ttyUSB0', baudrate=4800, timeout=2)
sio = io.TextIOWrapper(io.BufferedRWPair(port, port), newline='\xb0\xb3')
输出:
>>> sio = io.TextIOWrapper(io.BufferedRWPair(port, port, 1), newline='\xb3')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: illegal newline value: �
>>>
注意:它接受 '\x0d'
最佳答案
您不能只使用任何字符作为换行符。来自 io.TextIOWrapper()
documentation :
newline controls how line endings are handled. It can be
None
,''
,'\n'
,'\r'
, and'\r\n'
.
您必须手动处理这些字节,而不是直接处理换行符。
关于Python:io.TextIOWrapper 非法换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21000040/