从 AT 命令的输出中读取行(两行或更多行)并将其放入数据库

Read lines (two or more lines) from the output of AT command and put it to database

我想从 AT 命令的输出中读取两行并执行 INSERT INTO table VALUES in python。 AT 的输出总是两行,但它是一个序列,不像打开文件并读取它直到下一个限制器,但我们必须等待。

无论如何这是我的代码:

class SMSWait(self):
     def run(self):
         self.open()
         while self.ser.isOpen():
              time.sleep(1)
              SMSRead = 'AT+CMGL="ALL"\r\n'
              self.SendCommand(SMSRead, getline=True)

              while self.ser.inWaiting() > 0:
                   **data = self.ser.readall()
                   print data**
     def open(self):
         self.ser = serial.Serial('/dev/ttyUSB0', baudrate=115200, timeout=.1, rtscts=0)
         self.SendCommand('AT\r')
         self.SendCommand('AT+CMGF=1\r')
         self.ser.flushInput()
         self.ser.flushOutput()
     def SendCommand(self,command, getline=True):
         self.ser.write(command)
         data = ''
         if getline:
             data = self.ReadLine()
             data = filter(None, data)
         return data

     def ReadLine(self):
         data = self.ser.readline()
         return data

它将显示输出:

AT+CMGL="ALL"

+CMGL: 2,"REC READ","+60xxxxxxxxx",,"15/02/05,14:13:47+28"
mydata43414242453564567578689789done

OK

现在,我如何处理这个 "sequence" 输出并忽略 AT+CMGL="ALL" 和 "OK" 并在一个查询中使用 "insert into" 将它们放入数据库.使用 python 访问数据库没有问题。我对正则表达式没问题。也许有人可以在逻辑上或脚本上帮助我。谢谢

我明白了。看来我必须将所有这些行放在一个列表中并对其进行排序,然后将这 2 行减少到 1 行。看看

class SMSWait(self):
 def run(self):
     self.open()
     while 1:
         SMSRead = 'AT+CMGL="ALL"\r\n'
         self.SendCommand(SMSRead, getline=True)
         data = self.ser.readall()
         time.sleep(2)
         datalist.append(data)
         if any('+CMGL:' in d for d in datalist):
             if 'AT' in datalist: datalist.remove['AT']
             if any('\r\n' in d for d in datalist): datalist = [d.replace('\r\n', '') for d in datalist]
             if any('\r' in d for d in datalist): datalist   = [d.replace('\r', '') for d in datalist]
             if any('\n' in d for d in datalist): datalist   = [d.replace('\n', '') for d in datalist]
             if any('OK' in d for d in datalist): datalist   = [d.replace('OK', '') for d in datalist]
             datalist = filter(None, datalist)
             split_data = datalist[0].split('+CMGL: ')
             split_data = filter(None, split_data)

然后从这里我们可以开始一个一个地处理列表值中的数据

             for s in split_data:
                 '''your code here'''