使用SIM900的AT命令接收短信并拨打电话
问题描述:
我在使Python代码工作时遇到了一些困难。我有Rpi2 SIM900模块附加。我可以拨打电话,接听电话,发送短信,甚至可以收到短信。但把所有人放在一起让我头痛。我一直在滚动谷歌,现在我看到,即使结果都是相同的,所以我在进一步的0点。使用SIM900的AT命令接收短信并拨打电话
所以我的议程是,我会发送和SMS到SimModule。蟒蛇会读取传入的短信的,如果短信将从手机号从批准列表到达,将包含特定的代码,它将使特定号码进行呼叫:
短信的例子
RV -> make call to cell no: 49
RI -> make call to cell no: 48
MV -> make call to cell no: 47
MI -> make call to cell no: 46
我走到这一步,那我可以读短信用下面的代码
import serial
import time
import sys
class sim900(object):
def __init__(self):
self.open()
def open(self):
self.ser = serial.Serial('/dev/ttyAMA0', 115200, timeout=5)
def SendCommand(self,command, getline=True):
self.ser.write(command)
data = ''
if getline:
data=self.ReadLine()
return data
def ReadLine(self):
data = self.ser.readline()
# print data
return data
def GetAllSMS(self):
self.ser.flushInput()
self.ser.flushOutput()
command = 'AT+CMGL=\"ALL\"\r'#gets incoming sms that has not been read
print self.SendCommand(command,getline=True)
data = self.ser.readall()
print data
self.ser.close()
def Call49(self):
self.ser = serial.Serial('/dev/ttyAMA0', 115200, timeout=5)
self.ser.write('ATD49;\r')
time.sleep(1)
time.sleep(1)
self.ser.write(chr(26))
# responce = ser.read(50)
self.ser.close()
print "calling"
def Call48(self):
self.ser = serial.Serial('/dev/ttyAMA0', 115200, timeout=5)
self.ser.write('ATD48;\r')
time.sleep(1)
self.ser.write(chr(26))
# responce = ser.read(50)
self.ser.close()
print "calling"
h = sim900()
h.GetAllSMS()
我可以阅读短信(参见下文)
AT+CMGL="ALL"
AT+CMGL="ALL"
+CMGL: 1,"REC READ","+30000000000","","17/08/27,23:28:51+08"
RV
+CMGL: 2,"REC READ","+30000000000","","17/08/28,00:34:12+08"
RI
OK
如何,现在我在短信中,请新增功能“高清GetAllSMS”的电话号码300亿将与文本RV,它会执行“DEF Call48”这是否是RI将执行“DEF Call47”
存在一些帮助将不胜感激。 Tnx提前。
答
调制解调器响应的正确解析可能会非常棘手(例如,请参阅this answer)。如果您要使用消息传递功能以及语音呼叫和USSD请求,您最终可能会重新创建python-gsmmodem library :)但是,在解析SMS响应的特殊情况下,您可以使用此代码(改编自提到的python-gsmmodem) 。
import re
def GetAllSMS(self):
self.ser.flushInput()
self.ser.flushOutput()
result = self.SendCommand('AT+CMGL="ALL"\r\n')
msg_lines = []
msg_index = msg_status = number = msg_time = None
cmgl_re = re.compile('^\+CMGL: (\d+),"([^"]+)","([^"]+)",[^,]*,"([^"]+)"$')
# list of (number, time, text)
messages = []
for line in result:
cmgl_match = cmgl_re.match(line)
if cmgl_match:
# New message; save old one if applicable
if msg_index != None and len(msg_lines) > 0:
msg_text = '\n'.join(msg_lines)
msg_lines = []
messages.append((number, msg_time, msg_text))
msg_index, msg_status, number, msg_time = cmgl_match.groups()
msg_lines = []
else:
if line != 'OK':
msg_lines.append(line)
if msg_index != None and len(msg_lines) > 0:
msg_text = '\n'.join(msg_lines)
msg_lines = []
messages.append((number, msg_time, msg_text))
return messages
此功能将读取所有短信,并返回它们作为元组的列表:[("+30000000000", "17/08/28,00:34:12+08", "RI"), ("+30000000000", "17/08/28,00:34:12+08", "RV")
...遍历该列表,检查号码是否有效,并采取必要的行动:
for number, date, command in messages:
if number != "+30000000000":
continue
if command == 'RI':
self.call_1()
elif command == 'RV':
self.call_2()
HI我见我可能没有正确地修改我的问题。 我需要能够阅读以下 + CMGL:2,“REC READ”,“+ 30000000000”,“”,“17/08/28,00:34:12 + 08” RI 如果SMS将包含+30000000000和RI python将ATD0000001呼叫到电话号码。 如果短信将包含+30000000000和RV python将ATD0000002拨打电话号码。 等。如果短信中RI,RV,MI,MV和电话号码+30000000000的条件不符合,功能应该不起作用 – user3343073
对不起,我开始在错误的地方尝试剥离,而不是在SMS中查找字符串。 – user3343073
是的,你会得到消息列表使用该功能,然后做任何你想与他们。为了清楚起见,我使用用例更新了答案。 – 9dogs