作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的脚本已经完成了 99%,但遇到了问题
我正在尝试在 python 中显示从 GSM 调制解调器接收到的文本消息。
到目前为止,我可以看到收到了一条文本,但无法显示该消息。
我知道 self.ser.readlines() 作为列表返回
import serial
import time
class TextMessage:
def connectPhone(self):
self.ser = serial.Serial('COM14', 460800, timeout=5) #for mine this was ttyUSB0 but could be ttyUSB1 etc. good idea to runs ls usb and find out that way
time.sleep(1)
def read(self):
self.ser.write('ATZ\r')
time.sleep(1)
self.ser.write('AT+CMGF=1\r')# put in textmode
time.sleep(1)
self.ser.write('''AT+CMGL="ALL"''' + '''\r''') #fetch all sms's
read = self.ser.readlines()
for msg in read:
if "+CMGL" in msg: #+CMGL looks for all SMS messages
print msg
def disconnectPhone(self):
self.ser.close()
sms = TextMessage()
sms.connectPhone()
sms.read()
sms.disconnectPhone()
raw_input("Press anykey to exit")
这里是输出
AT+CMGL="ALL"
+CMGL: 0,"REC READ","+61xxxxxxxxx",,"16/04/30,19:53:38+38"
+CMGL: 1,"REC READ","+61xxxxxxxxx",,"16/05/02,14:47:53+38"
+CMGL: 2,"REC READ","+61xxxxxxxxx",,"16/05/02,21:27:58+38"
我想要的输出是
+CMGL: 0,"REC READ","+61xxxxxxxxx",,"16/04/30,19:53:38+38"
Test back
+CMGL: 1,"REC READ","+61xxxxxxxxx",,"16/05/02,14:47:53+38"
Im a message
+CMGL: 2,"REC READ","+61xxxxxxxxx",,"16/05/02,21:27:58+38"
TEST TEST TEST
有人可以帮忙吗?
输出if removed
ATZ
OK
AT+CMGF=1
Ok
AT+GCML="ALL"
+CMGL: 0,"REC READ","+61xxxxxxxxx",,"16/04/30,19:53:38+38"
Test back
+CMGL: 1,"REC READ","+61xxxxxxxxx",,"16/05/02,14:47:53+38"
Im a message
+CMGL: 2,"REC READ","+61xxxxxxxxx",,"16/05/02,21:27:58+38"
TEST TEST TEST
OK
最佳答案
希望对你有帮助
#!/usr/bin/env python2.7
import serial
import time
class TextMessage:
def connectPhone(self):
self.ser = serial.Serial('/dev/ttyUSB2', 9600, timeout=5) #for mine this was ttyUSB0 but could be ttyUSB1 etc. good idea to runs ls usb and find out that way
time.sleep(1)
def read(self):
self.ser.write('ATZ\r')
time.sleep(1)
self.ser.write('AT+CMGF=1\r')# put in textmode
time.sleep(1)
self.ser.write('''AT+CMGL="ALL"''' + '''\r''') #fetch all sms's
read = self.ser.readlines()
i=0
for msg in read:
if "+CMGL" in msg: #+CMGL looks for all SMS messages
bvb = 'AT+CMGR={}'.format(str(i)) + '\r'
self.ser.write(bvb) #fetch all sms's
ead = self.ser.readlines()
for x in ead:
print (x)
print (i)
i=i+1
def disconnectPhone(self):
self.ser.close()
sms = TextMessage()
sms.connectPhone()
sms.read()
sms.disconnectPhone()
raw_input("Press anykey to exit")
关于Python 通过 AT 读取短信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36984118/
我是一名优秀的程序员,十分优秀!