【发布时间】:2012-04-11 05:27:48
【问题描述】:
我正在使用 Python 2.6,我想在我的手机(即诺基亚 E-72)通过数据线连接到 PC 时向手机发送短信。
手机通过串口连接,代码也提示正确的端口,代码没有错误,但仍然没有发送消息。
我的代码如下:
import serial
import time
phone = serial.Serial()
phone.baudrate = 38400
phone.bytesize = 8
phone.stopbits = 1
phone.xonxoff = 0
phone.rtscts = 0
phone.timeout = 0
phone.port = 4 #try different ports here, if this doesn't work.
phone.parity=serial.PARITY_NONE
phone.open()
print phone.portstr
recipient = "+923219409998"
message = "We did it!"
try:
time.sleep(0.5)
phone.write(b'ATZ\r')
time.sleep(0.5)
phone.write(b'AT+CMGF=1\r')
time.sleep(0.5)
phone.write(b'AT+CMGS="' + recipient.encode() + b'"\r')
time.sleep(0.5)
phone.write(message.encode() + b"\r")
time.sleep(0.5)
phone.write(bytes([26]))
time.sleep(0.5)
phone.readall()
finally:
phone.close()
【问题讨论】:
-
请在任何人帮助您找到问题之前修复代码格式。
-
不确定手机是否允许,顺便说一句,您确定它是串行连接吗?
-
是的,它是一个串行连接。你能告诉我你为什么问这个吗?
-
phone.write(bytes([26]))做了什么,为什么?另外,你确定你的\r是正确的吗? -
phone.write(bytes([26])) 将消息写入串行端口,是的 \r 是 AT 命令的一部分,该命令将消息设置为移动 SMS 的格式。跨度>
标签: python sms serial-port