Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
455 views
in Technique[技术] by (71.8m points)

python - pySerial 2.6: specify end-of-line in readline()

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 . 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 + '
')
  self.ser.flush();
  response = self.ser.readline() # Currently stops reading on timeout...
  if self.isErr(response):
    logger.error(response)
    return None
  else:
    return response
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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'
'
    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 = '
',
                               line_buffering = True)
self.ser_io.write("ID
")
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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...