简体   繁体   中英

Python sending sms via usb serial huawei dongle

I'm currently sending sms in python using this code, but sometimes it is unstable. It went straight to "send successfully" but the other party did not receive any message. Is there any other ways to send sms through the dongle? Much appreciated.

This is the code.

class TextMessage:

    def __init__(self, recipient="XXXXXXXX", message="TextMessage.content not set."):
        self.recipient = recipient
        self.content = message

    def setRecipient(self, number):
        self.recipient = number

    def setContent(self, message):
        self.content = message

    def connectPhone(self):
        self.ser = serial.Serial('/dev/ttyUSBSMS', 460800, timeout=5)
        time.sleep(1)

    def sendMessage(self):
        self.ser.write('ATZ\r')
        time.sleep(1)
        self.ser.write('AT+CMGF=1\r')
        time.sleep(1)
        self.ser.write('''AT+CMGS="''' + self.recipient + '''"\r''')
        time.sleep(1)
        self.ser.write(self.content + "\r")
        time.sleep(1)
        self.ser.write(chr(26))
        time.sleep(1)

    def disconnectPhone(self):
        self.ser.close()


sms = TextMessage("XXXXXXXX","This is the message to send.")
sms.connectPhone()
sms.sendMessage()
sms.disconnectPhone()
print "sent successfully"

好的python gsm调制解调器库: https : //github.com/faucamp/python-gsmmodem

You say that sometimes it is unstable, so I assume that it does sometimes send correctly. On that basis, I imagine that the code is fine. The answer at Python sending many sms via usb serial uses slightly different arguments, which may be worth investigating.

Unless you have a commercial plan, SMS is not guaranteed to arrive in any particular hurry. You can send two messages a minute apart, and have the second one arrive immediately and the first one arrive 5 hours later. Have you waited for a 24 hour period to see if the messages ever arrive?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM