14

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 the Eddie command set is listed on the second and third pages of this PDF. Here is a backup image in the case where the PDF is inaccessible.

General command form:

Input:              <cmd>[<WS><param1>...<WS><paramN>]<CR>
Response (Success): [<param1>...<WS><paramN>]<CR>
Response (Failure): ERROR[<SP>-<SP><verbose_reason>]<CR> 

As you can see all responses end with a \r. I need to tell pySerial to stop.

What I have now:

def sendAndReceive(self, content):
  logger.info('Sending {0}'.format(content))
  self.ser.write(content + '\r')
  self.ser.flush();
  response = self.ser.readline() # Currently stops reading on timeout...
  if self.isErr(response):
    logger.error(response)
    return None
  else:
    return response
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132

5 Answers5

22

I'm having the same issue and implemented my own readline() function which I copied and modified from the serialutil.py file found in the pyserial package.

The serial connection is part of the class this function belongs to and is saved in attribute 'self.ser'

def _readline(self):
    eol = b'\r'
    leneol = len(eol)
    line = bytearray()
    while True:
        c = self.ser.read(1)
        if c:
            line += c
            if line[-leneol:] == eol:
                break
        else:
            break
    return bytes(line)

This is a safer, nicer and faster option than waiting for the timeout.

EDIT: I came across this post when trying to get the io.TextIOWrapper method to work (thanks zmo). So instead of using the custom readline function as mentioned above you could use:

self.ser = serial.Serial(port=self.port,
                         baudrate=9600,
                         bytesize=serial.EIGHTBITS,
                         parity=serial.PARITY_NONE,
                         stopbits=serial.STOPBITS_ONE,
                         timeout=1)
self.ser_io = io.TextIOWrapper(io.BufferedRWPair(self.ser, self.ser, 1),  
                               newline = '\r',
                               line_buffering = True)
self.ser_io.write("ID\r")
self_id = self.ser_io.readline()

Make sure to pass the argument 1 to the BufferedRWPair, otherwise it will not pass the data to the TextIOWrapper after every byte causing the serial connection to timeout again.

When setting line_buffering to True you no longer have to call the flush function after every write (if the write is terminated with a newline character).

EDIT: The TextIOWrapper method works in practice for small command strings, but its behavior is undefined and can lead to errors when transmitting more than a couple bytes. The safest thing to do really is to implement your own version of readline.

Community
  • 1
  • 1
lou
  • 1,740
  • 1
  • 13
  • 13
  • This is so nice, thank you. The TextIOWrapper approach has always given me headaches, I don't know why I never thought of implementing such a simple custom readline function. It works perfectly! – Dan McAnulty Apr 06 '23 at 00:31
11

From pyserial 3.2.1 (default from debian Stretch) read_until is available. if you would like to change cartridge from default ('\n') to '\r', simply do:

import serial
ser=serial.Serial('COM5',9600)
ser.write(b'command\r')  # sending command
ser.read_until(b'\r')   # read until '\r' appears

b'\r' could be changed to whatever you will be using as carriadge return.

Antony Hatchkins
  • 31,947
  • 10
  • 111
  • 111
Chenming Zhang
  • 2,446
  • 2
  • 26
  • 34
  • This a short and the best answer. Y=I tested using serial_for_url('socket://.... using '\r\n' as EOL and it works. Thanks Zhang – edison Jan 17 '20 at 18:06
  • For some reason ser.read_until('\r') didn't work for me. However ser.read_until(serial.serialutil.CR) did work. – Onderbetaald Jun 04 '21 at 08:38
  • 2
    This is the best answer. @Onderbetaald you need to specify it as a byte: b'\r' – rdh Aug 25 '21 at 02:50
  • The only answer that works for me. Spent some time figuring what's going on. b'\r' is definitely a must on python3. Figured it out myself, then found out that @rdh has also reported that. – Antony Hatchkins Feb 28 '22 at 07:30
  • 1
    btw `CR` suggested by @Onderbetaald is defined as `CR = to_bytes([13])` which is exactly the same thing as `b'\r'` – Antony Hatchkins Feb 28 '22 at 07:37
5

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")
shrewmouse
  • 5,338
  • 3
  • 38
  • 43
zmo
  • 24,463
  • 4
  • 54
  • 90
  • 4
    the io module docs say: `Warning: BufferedRWPair does not attempt to synchronize accesses to its underlying raw streams. You should not pass it the same object as reader and writer; use BufferedRandom instead.` So I'm not sure this is an entirely Kosher solution. Works though! – Chris Billington Aug 26 '13 at 17:36
1

reading 10 data from port 3 with board rate 38400, The data is separated with \n character when comes in the incoming data

import serial as self
ser=self.Serial("COM3", 38400)  
buffer = []
count = 0.0
c = "\0"
while count < 10:
    c = "\0"
    if ser.inWaiting():
        while True:
            val = ser.read(1)
            if "\n" in val:
                break
            else:
                c += val
        buffer.append(c) # stores all data received into a list   
        count += 1
print buffer
sajin
  • 11
  • 1
-3

It seems that the timeout occurs because readline() waits for an '\n' character to come from the serial device, which it never sends.

According to the pyserial documentation, you can specify the end of line character:

response = self.ser.readline(eol='\r')

Does that work?

rodion
  • 6,087
  • 4
  • 24
  • 29
  • 3
    The signature for the function was changed for 2.6. I have stated this above. `readline(size=None, eol='\n')` as changed to `readline(size=None)` – Mr. Polywhirl May 10 '13 at 18:00