I am also working in python 2.6 and i want to send SMS to a mobile while my cell phone i.e. Nokia E-72 is connected to PC via Data Cable.

The mobile is connected through a serial port and the code prompts the correct port as well and there is no error in the code but still the message is not being sent...

Please help me for the issue.

My code is as follows:

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()

Recommended Answers

All 4 Replies

Use code blocks (select code and hit TAB key), see Formatting Help in corner of reply box.

I'm not familiar with AT commands, but, what if you use a longer time.sleep()? because those commands are very fast and phone needs more time to respond.

here is my code:

    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')     # smessage format

       # phone.write(b'AT+CMGW=1\r')   # write message to memory
       # phone.write(b'AT+CMSS=1\r')   # send message from storage

        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()

    # print phone.readall(str)

    finally:
        phone.close()

and i did try different sleep times but even than sms was not sent.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.