【问题标题】:Receive SMS and Make call using AT command with SIM900使用 SIM900 使用 AT 命令接收短信和拨打电话
【发布时间】:2018-02-05 15:37:56
【问题描述】:

我在让我的 python 代码工作时遇到了一些困难。我有带有 SIM900 模块的 Rpi2。我可以拨打电话、接听电话、发送短信,甚至可以接收短信。但是把所有东西放在一起让我很头疼。我在谷歌上一遍又一遍地滚动,现在我看到甚至结果都是一样的,所以我处于进一步发展的 0 点。

所以我的议程是向 SimModule 发送短信。如果 SMS 会从已批准列表中的电话号码到达,Python 将读取传入的 SMS,并且将包含特定代码,它将调用特定号码:

短信示例

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

如果在短信中“def GetAllSMS”电话号码 +30000000000 将与文本 RV 一起存在,它将执行“def Call48”如果它是 RI,它将执行“def Call47”,现在我如何添加功能

我们将不胜感激。 提前Tnx。

【问题讨论】:

    标签: python sms at-command sim900


    【解决方案1】:

    正确解析调制解调器响应可能很棘手(例如,请参阅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
    

    此函数将读取所有 SMS 并将它们作为元组列表返回:[("+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()
    

    【讨论】:

    • 嗨,我知道我可能没有正确地改写我的问题。我需要能够阅读以下 +CMGL:2,"REC READ","+30000000000","","17/08/28,00:34:12+08" RI 如果 SMS 将包含 +30000000000并且 RI python 会向电话号码发出 ATD0000001 呼叫。如果 SMS 将包含 +30000000000 并且 RV python 将使 ATD0000002 呼叫电话号码。等等。如果不满足短信中的 RI、RV、MI、MV 和电话号码 +30000000000 的条件,则函数应该什么都不做
    • 抱歉,我从错误的地方开始尝试剥离而不是仅仅在 SMS 中寻找字符串。
    • 是的,您将使用该功能获取消息列表,然后对它们做任何您想做的事情。为了清楚起见,我用用例更新了答案。
    • 您好,我试过了,但代码不起作用。我没有得到答复。我已经修改了主要问题。如果你能帮忙。这个想法还是一样的,我刚刚又添加了两个“def Call49(self):”和 def Call48(self): 函数。如果我将它们作为 h.Call48 或 h.Call49 添加到脚本的末尾,这两个函数都可以工作。那么现在是否可以从 GetALLSMS 读取并执行如果在 SMS 电话号码和文本中将是?
    • 到底是什么问题? messages var 为空?您是唯一可以调试此代码的人,请尝试逐步打印vars。
    猜你喜欢
    • 2011-01-10
    • 1970-01-01
    • 2018-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多