def readuntil(self, char, log=True, logsuccess=True, numfailures=0): self.readrequests+=1 if data_recording.DR_Play: return data_recording.get_data(data_recording.DR_Type_Read_Until) if False: # don't log this anymore self.logdata("Begin reading until 0x%02x" % (ord(char),), None) res='' while len(res)==0 or res[-1]!=char: if hasattr(self.ser, 'readuntil'): res2=self.ser.readuntil(char) b=-99999 else: b=self.ser.inWaiting() if b<1: b=1 res2=self.read(b,0) if len(res2)<1: if numfailures==0: if log: self.log("Timed out waiting for %02x, requested bytes %d - %d bytes read" % (ord(char), b, len(res))) self.logdata("Incomplete read was", res) self.readbytes+=len(res) raise CommTimeout(partial=res) else: numfailures-=1 self.log("Timed out - flushing and trying again") res=res+res2 self.readbytes+=len(res) if logsuccess or data_recording.DR_On: self.logdata("Read completed", res, data_recording.DR_Type_Read_Until) return res
def readatresponse(self, ignoreerror, log=True): """Read until OK, ERROR or a timeout""" self.readrequests+=1 if data_recording.DR_Play: return data_recording.get_data(data_recording.DR_Type_Read_ATResponse) res="" while True: b=self.ser.inWaiting() if b: res=res+self.read(b,0) if res.find("OK\r")>=0 or (res.find("ERROR\r")>=0 and not ignoreerror): break continue r=self.read(1,0) if len(r): res=res+r continue break while len(res)>0 and (res[0]=='\n' or res[0]=='\r'): res=res[1:] if len(res)==0: raise CommTimeout() self.readbytes+=len(res) if log or data_recording.DR_On: self.logdata("Reading remaining data", res, data_recording.DR_Type_Read_ATResponse) self.readahead=res return
def _read(self, numchars=1, log=True): self.readrequests += 1 if data_recording.DR_Play: return data_recording.get_data(data_recording.DR_Type_Read) try: res = self.ser.read(numchars) except Exception, e: if not self._isbrokendriver(e): raise res = self._brokendriverread(numchars)
def _read(self, numchars=1, log=True): self.readrequests+=1 if data_recording.DR_Play: return data_recording.get_data(data_recording.DR_Type_Read) try: res=self.ser.read(numchars) except Exception,e: if not self._isbrokendriver(e): raise res=self._brokendriverread(numchars)
def readatresponse(self, ignoreerror, log=True): """Read until OK, ERROR or a timeout""" self.readrequests += 1 if data_recording.DR_Play: res = data_recording.get_data( data_recording.DR_Type_Read_ATResponse) if res: self.readahead = res return else: raise CommTimeout() res = "" while True: b = self.ser.inWaiting() if b: res = res + self.read(b, 0) if res.find("OK\r") >= 0 or (res.find("ERROR\r") >= 0 and not ignoreerror): break continue r = self.read(1, 0) if len(r): res = res + r continue break while len(res) > 0 and (res[0] == '\n' or res[0] == '\r'): res = res[1:] if len(res) == 0: self.logdata("Reading remaining data", '', data_recording.DR_Type_Read_ATResponse) raise CommTimeout() self.readbytes += len(res) self.readahead = res if log or data_recording.DR_On: self.logdata("Reading remaining data", res, data_recording.DR_Type_Read_ATResponse) return
def readuntil(self, char, log=True, logsuccess=True, numfailures=0): # Keeps reading until it hits char self.readrequests += 1 if data_recording.DR_Play: return data_recording.get_data(data_recording.DR_Type_Read_Until) if False: # don't log this anymore self.logdata("Begin reading until 0x%02x" % (ord(char), ), None) # set numfailures to non-zero for retries on timeouts res = '' while len(res) == 0 or res[-1] != char: if hasattr(self.ser, 'readuntil'): # usb does it directly res2 = self.ser.readuntil(char) b = -99999 else: b = self.ser.inWaiting() if b < 1: b = 1 res2 = self.read(b, 0) if len(res2) < 1: if numfailures == 0: if log: self.log( "Timed out waiting for %02x, requested bytes %d - %d bytes read" % (ord(char), b, len(res))) self.logdata("Incomplete read was", res) self.readbytes += len(res) raise CommTimeout(partial=res) else: numfailures -= 1 self.log("Timed out - flushing and trying again") res = res + res2 self.readbytes += len(res) if logsuccess or data_recording.DR_On: self.logdata("Read completed", res, data_recording.DR_Type_Read_Until) return res
def readsome(self, log=True, numchars=None): self.readrequests += 1 if data_recording.DR_Play: return data_recording.get_data(data_recording.DR_Type_Read_Some) res = "" while True: if numchars is not None and len(res) >= numchars: break b = self.ser.inWaiting() if b: res = res + self.read(b, 0) continue r = self.read(1, 0) if len(r): res = res + r continue break if len(res) == 0: raise CommTimeout() self.readbytes += len(res) if log or data_recording.DR_On: self.logdata("Reading remaining data", res, data_recording.DR_Type_Read_Some) return res
def readsome(self, log=True, numchars=None): self.readrequests+=1 if data_recording.DR_Play: return data_recording.get_data(data_recording.DR_Type_Read_Some) res="" while True: if numchars is not None and len(res)>= numchars: break b=self.ser.inWaiting() if b: res=res+self.read(b,0) continue r=self.read(1,0) if len(r): res=res+r continue break if len(res)==0: raise CommTimeout() self.readbytes+=len(res) if log or data_recording.DR_On: self.logdata("Reading remaining data", res, data_recording.DR_Type_Read_Some) return res