Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 \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
like image 694
Mr. Polywhirl Avatar asked May 09 '13 20:05

Mr. Polywhirl


1 Answers

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.

like image 52
Chenming Zhang Avatar answered Nov 15 '22 19:11

Chenming Zhang