2

How can I change the end-of-line (EOL, sometimes called TERMINATOR) character in current versions (>3.4) of PySerial? The short intro advises to use io.TextWrapper, but I have never used the io module and the example given in the short intro is pretty far from my use-case. Is there a simpler way? Something like Matlab's

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

I just want to be able to do readline() on a device that uses CR to indicate a newline.

Martin J.H.
  • 2,085
  • 1
  • 22
  • 37
  • [This question](https://stackoverflow.com/questions/16470903/pyserial-2-6-specify-end-of-line-in-readline?rq=1), although specifying an ancient 2.6 version in the title, is more than relevant here. – Martin J.H. Feb 14 '18 at 10:33
  • yeah, it looks like a duplicate now, only filtered to keep the up-to-date stuff. I'll mark it as such. – Jean-François Fabre Feb 14 '18 at 10:39

1 Answers1

1

Adapting the example from your link, adding newline parameter, as described in the docs:

>>> 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'.

modified sample:

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

now readline stops when encountering a \r char.

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
  • 1
    I read up a bit and feel a bit silly now for asking this question. I got tripped up by `serial_for_url('loop://', ...)`, which to me looked like some web stuff I don't need. I did not realize 'loop://' is just for tests, and that I can simply replace it with 'COM3' in my case. – Martin J.H. Feb 14 '18 at 10:31