Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: io.TextIOWrapper illegal newline

I'm trying to set the newline character for SiRF binary messages, but the IO wrapper doesn't seem to accept the newline chars.

Code:

import serial
import io

port = serial.Serial(port='/dev/ttyUSB0', baudrate=4800, timeout=2)
sio = io.TextIOWrapper(io.BufferedRWPair(port, port), newline='\xb0\xb3')

Output:

>>> 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: �
>>>

Note: It does accept '\x0d'

like image 594
Eric Fossum Avatar asked Apr 25 '26 12:04

Eric Fossum


1 Answers

You can not just use any character as the newline. From the io.TextIOWrapper() documentation:

newline controls how line endings are handled. It can be None, '', '\n', '\r', and '\r\n'.

You'll have to handle those bytes manually instead of a newline, directly.

like image 117
Martijn Pieters Avatar answered Apr 27 '26 01:04

Martijn Pieters