I want to read two lines from AT Command's output and do INSERT INTO table VALUES in python. The output of AT is always in two rows but it's a sequence, unlike open file and read it until the next limiter, but we have to wait.
anyway this is my code:
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
it will show the output:
AT+CMGL="ALL"
+CMGL: 2,"REC READ","+60xxxxxxxxx",,"15/02/05,14:13:47+28"
mydata43414242453564567578689789done
OK
now, how do i process this "sequence" output and ignoring the AT+CMGL="ALL" and "OK" and put them to database using "insert into" in one query. i don't have problem with accessing database using python. and i'm okay with regex. maybe someone could help me logically or script. thank you
i got it. it seems that i have to put it all those lines inside a list and sort it, and reduce those 2 lines to 1 line. check it out
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)
then from here we can start to process the data from list value one by one
for s in split_data:
'''your code here'''