Successfully send sms with python

Cameron Simpson cs at zip.com.au
Tue Sep 22 19:20:32 EDT 2015


On 22Sep2015 04:19, Timon Rhynix <timgeek951 at gmail.com> wrote:
>Hello, I have used pyserial, sms0.4 and other libraries to send sms via huawei E1750 modem.
>The code runs well and no error is thrown but the text message is not sent/delivered to the number.
>One of my code is as follows:
>
>import serial
>import time
>
>class TextMessage:
>    def __init__(self, recipient="0123456789", 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):
>        conn = 'COM13'
>        self.ser = serial.Serial(conn, 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)
>        print "message sent!"
>
>    def disconnectPhone(self):
>        self.ser.close()
>
>When run it, the "message sent!" is printed but no message is sent/delivered.
>Please assist on what I am missing. Thank you

Two suggestions:

  as already suggested, you may need to send \r\n instead of just \r

  do you need to follow every .write() with a .flush()? if the Serial object is 
  a buffered stream that will be necessary

Cheers,
Cameron Simpson <cs at zip.com.au>

Motorcycling is indeed a delightful pastime.    - Honda Rider Training Film



More information about the Python-list mailing list