解析python中GSM调制解调器接收的消息参数 [英] Parsing message parameters received by a GSM modem in python

查看:245
本文介绍了解析python中GSM调制解调器接收的消息参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用python解析从GSM调制解调器接收到的消息.

I'm trying to parse messages that I receive from a GSM modem in python.

我有很多消息需要解析.我每两个小时左右收到新消息.

I have a lot of messages that I need to parse. I receive new messages every couple of hours or so.

这是我通过使用串行对象将数据从调制解调器读取到列表x中后收到的数据的示例.

Here's an example of the data the I receive after reading data from the modem by using a serial object into a list x.

AT+CMGL="ALL"


+CMGL: 1,"REC READ","+918884100421","","13/04/05,08:24:36+22"
here's message one 

+CMGL: 2,"REC READ","+918884100421","","13/04/05,09:40:38+22"
here's message two

+CMGL: 3,"REC READ","+918884100421","","13/04/05,09:41:04+22"
here's message three

+CMGL: 4,"REC READ","+918884100421","","13/04/05,10:04:18+22"
here's message four

+CMGL: 5,"REC READ","+918884100421","","13/04/05,10:04:32+22"
here's message five

.
.
.
.
.

还有更多信息,我在这里只列出了五条.

There are a lot more messages, I've just listed five here.

我的主要目的是为收到的每条消息提取消息的内容,例如此处是一个消息",依此类推.

My main intention is to extract the content of the message, for example "here's message one" and so on for every message that I receive.

这是我现在正在使用的代码.

Here's the code that I'm using right now.

def reading():
     print "Reading all the messages stored on SIM card"
     phone.write(b'AT+CMGL="ALL"\r')
     sleeps()
     x=phone.read(10000)
     sleeps()
     print x
     print "Now parsing the message!"
     k="".join(x)
     parse(k)
     k=""
def parse(k):
    m = re.search("\+CMGL: (\d+),""(.+)"",""(.+)"",(.*),""(.+)""\r\n(.+)\r\n",k)
    print "6="
    print m.group(6)

电话是我用来从GSM调制解调器读取的串行对象.

Phone is the serial object that I'm using to read from the GSM modem.

这里m.group(6)捕获了第一条消息"here's message one"的消息内容

Here m.group(6) is captures the message content of the first message "here's message one"

如何获取它以匹配所有消息的内容,而不仅仅是第一条消息.

How can I get it to match the content of all the messages, not just the first one.

我尝试设置多行标志,但这没有用.都没有使用re.findall()代替re.search().

I tried setting the multiline flag but that didn't work. Neither did using re.findall() instead of re.search().

而且re.search返回的match对象也是不可迭代的.

Also the match object returned by re.search isn't iterable.

请帮助.

推荐答案

由于我没有得到您的材料,所以我只做一个样本.

Since I don't get your material I just make a sample.

'\xef\xbb\xbfAT+CMGL="ALL"\n\n+CMGL: 1,"REC READ","+918884100421","","13/04/05,08:24:36+22"\nhere\'s message one \n\n+CMGL: 2,"REC READ","+918884100421","","13/04/05,09:40:38+22"\nhere\'s message two\n\n+CMGL: 3,"REC READ","+918884100421","","13/04/05,09:41:04+22"\nhere\'s message three\n\n+CMGL: 4,"REC READ","+918884100421","","13/04/05,10:04:18+22"\nhere\'s message four\n\n+CMGL: 5,"REC READ","+918884100421","","13/04/05,10:04:32+22"\nhere\'s message five\n'

这来自您使用''.join()提出的问题.然后,我使用您的正则表达式模式,只需将\r\n替换为\n,因为我使用的示例是使用\n的.我得到了结果.我不知道为什么findall与您不兼容.

This comes from your question using ''.join(). And then I use your regex pattern, just replace the \r\n with \n because the sample I use using \n. And I get the result. I don't know why the findall doesn't work with you.

def parse(x):
    res = []
    match = re.finditer("\+CMGL: (\d+),""(.+)"",""(.+)"",(.*),""(.+)""\n(.+)\n", x)
    for each in match:
        res.append(each.group(6))
    return res

我得到的结果是["here's message one ", "here's message two", "here's message three", "here's message four", "here's message five"]. finditer返回一个迭代器,并且findall也可以正常工作.

The result I get is ["here's message one ", "here's message two", "here's message three", "here's message four", "here's message five"]. finditer returns an iterator and findall also works OK.

 def parse(x):
        res = []
        match = re.findall("\+CMGL: (\d+),""(.+)"",""(.+)"",(.*),""(.+)""\n(.+)\n", x)
        for each in match:
            res.append(each[5])
        return res

这篇关于解析python中GSM调制解调器接收的消息参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆