def return_data(self, command=None): """Read until exit status is returned. Returns: data: List of right-stripped strings containing output of the command. Raises: AtCommandError: If an error is returned by the modem. """ data = [] while 1: # Read in one line of input. try: input_line = self.readline().decode().rstrip() except serial.serialutil.SerialException: time.sleep(.2) continue # Check for errors and raise exception with specific error code. errors.check_for_errors(input_line) if input_line == 'OK' and self.inWaiting() == 0: # Final 'OK\r\n' return data # Append only related data (starting with "command" contents). if command: if input_line.startswith(command): prefix_length = len(command)+2 data.append(input_line[prefix_length:]) else: # Append only non-empty data. if input_line: data.append(input_line)
def send_at(self, cmd, suffix, prefixed=True): """Send serial text to the modem. Arguments: self -- serial port to send to, text -- text value to send, prefixed -- boolean determining weather to strip the AT command prefix from each output line. Returns: List of strings. """ self.write(('AT%s%s\r' % (cmd, suffix)).encode()) # Read in the echoed text. # Check for errors and raise exception with specific error code. input_line = self.readline().decode() errors.check_for_errors(input_line) # Return the result. if prefixed: # If the text being sent is an AT command, only relevant context # answer (starting with '+command:' value) will be returned by #return_data(). Otherwise any string will be returned. return self.return_data(cmd) else: return self.return_data()