python控制modem的at指令_解析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.

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)

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

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.

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

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

Please help.

解决方案

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'

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值