【发布时间】: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