0

I'm reading some data in a message that I receive from a GSM modem in Python.

I need to extract these data parameters before I write them into my MySQL database.

Before I do this, I parse the message in a couple of stages before I am ready to write it to the data base.

Reading and writing from the GSM modem is done by using a serial object phone. This serial object reads into x where data is stored temporarily till the next read/write occurs.

Here's the flow of control in the program:

def reading():
     print "Reading all the messages stored on SIM card"
     phone.write(b'AT+CMGL="ALL"\r') #Command to read all the messages
     sleeps()
     x=phone.read(100000000)
     sleeps()
     print x
     print "Now parsing the message!"
     k="".join(x)
     parse(k)
     k=""


def parse(k):
    match = re.finditer("\+CMGL: (\d+),""(.+)"",""(.+)"",(.*),""(.+)""\n(.+)\n", k)
    for each in match: 
        break_down(each.group(6))

Here there are multiple messages that are being read. each.group(6) contains the actual message content.

def break_down(s):
    c=s.count('<')
    if c==9:
            res = re.findall('< (.*?) >', s)
            for index in res:
                print index,item
    elif c==7 or c==3:
            temp=parsing(s)
            pprint(list(temp))
    else:
        flag=0
        c=s.count(':')
        if c==8:
            res=s.split(' : ')
            res=[item.strip() for item in s.split(':')]
            for index, item in enumerate(res):
                print index, item
            results = [float(x) for x in s.split(' ') if x.count('.') == 1]
            pprint(results)
            dbinsert(res[0],res[1],res[2],res[3],results[0],results[1],results[2],results[3],flag)
        if c==7:
            flag=1
            res=s.split(' : ')
            res=[item.strip() for item in s.split(':')]
            for index, item in enumerate(res):
                print index, item
            results = [float(x) for x in s.split(' ') if x.count('.') == 1]
            pprint(results)
            dbinsert(res[0],res[1],res[2],res[3],results[0],results[1],results[2],results[3],flag)
def parsing(s):
        for t in s.split('<'):
            for u in t.strip().split('>',1):
                if u.strip(): yield u.strip()


def dbinsert(a,b,c,d,e,f,g,h,flag):
 import MySQLdb
 db = MySQLdb.Connect(host="127.0.0.1", port=3306, user="root", passwd="root", db="ups")
 print "In dbinsert"
 cursor = db.cursor()
 try:
  if flag==0:
   sql = """INSERT INTO data(F1,
          F2, F3, F4, F5, F6, F7, F8)
          VALUES (a.value,b.value,c.value,d.value,e.value,f.value,g.value,h.value)"""
   cursor.execute(sql)
   db.commit()
  elif flag==1:
   sql = """INSERT INTO data(F1,
          F2, F3, F4, F5, F6, F7, F8)
          VALUES (a,b,c,,e,f,g,h)"""
   cursor.execute(sql)
   db.commit()
 except:
  db.rollback()
  db.close()

break_down(s) is able to extract the data exactly as required. However once this data is passed to dbinsert, the data is not getting written to the database. When I try to do this in isolation without the rest of the code with some dummy values, there is no problem. So it isn't a database connection problem.

What am I doing wrong?

Please help.

hlovdal
  • 26,565
  • 10
  • 94
  • 165
Anon
  • 845
  • 5
  • 30
  • 50
  • How can you tell that break_down is getting the right data? If you print the res and results array right before you insert them you see data? Instead of using dummy values can you try those same values that are printed? – bbayles Apr 16 '13 at 01:46
  • yes, that's what i meant when I tried that. It isn't working. – Anon Apr 16 '13 at 09:34
  • Do you see "in dbinsert" when you run it for real? – bbayles Apr 16 '13 at 12:26

1 Answers1

0

Your regexp parsing

re.finditer("\+CMGL: (\d+),""(.+)"",""(.+)"",(.*),""(.+)""\n(.+)\n", k)

seems too weak to be able to properly cover all possible case. From 27.005, the AT+CMGL syntax in text mode is (for SMS-SUBMIT/SMS-DELIVER which are normal sms messages):

+CMGL: <index>,<stat>,<oa/da>,[<alpha>],[<scts>][,<tooa/toda>,
<length>]<CR><LF><data>[<CR><LF>
+CMGL: <index>,<stat>,<da/oa>,[<alpha>],[<scts>][,<tooa/toda>,
<length>]<CR><LF><data>[...]]

Your regexp is expecting that there always will be at least three string parameters following the first numerical parameter, but notice that the whole <alpha> parameter is optional and might not be present.

I am not sure if that is your whole problem, but it is something you ought to fix in any case. If it were me I would abandon regexp for this and instead parse the line parameter for parameter in a specific function.

hlovdal
  • 26,565
  • 10
  • 94
  • 165