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


i sending commands eddie using pyserial. need specify carriage-return in readline, pyserial 2.6 got rid of it... there workaround?

here eddie command set listed on second , third pages of pdf. here backup image in case pdf 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 can see responses end \r. need tell pyserial stop.

what have now:

def sendandreceive(self, content):   logger.info('sending {0}'.format(content))   self.ser.write(content + '\r')   self.ser.flush();   response = self.ser.readline() # stops reading on timeout...   if self.iserr(response):     logger.error(response)     return none   else:     return response 

i'm having same issue , implemented own readline() function copied , modified serialutil.py file found in pyserial package.

the serial connection part of class function belongs , 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 safer, nicer , faster option waiting timeout.

edit: came across this post when trying io.textiowrapper method work (thanks zmo). instead of using custom readline function mentioned above 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 pass argument 1 bufferedrwpair, otherwise not pass data textiowrapper after every byte causing serial connection timeout again.

when setting line_buffering true no longer have call flush function after every write (if write terminated newline character).

edit: textiowrapper method works in practice small command strings, behavior undefined , can lead errors when transmitting more couple bytes. safest thing implement own version of readline.


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -