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

Dani AI

Generated

Two quick things are most likely blocking you: the way the end-of-message (Ctrl+Z) is sent under Python 2.6, and the fact your script never waits for modem prompts (it only uses fixed sleeps and timeout=0). In Python 2.6 bytes([26]) does not produce a single 0x1A character — use chr(26) or '\x1A' to send Ctrl+Z. Also set a nonzero timeout and read the modem response after each AT command instead of relying on arbitrary sleeps; that makes the logic robust (and avoids race conditions that suggested trying by increasing sleep).

Checklist and practical steps (fast wins)

  • Verify the COM port is the actual modem port (Device Manager on Windows or dmesg/ls /dev/tty* on Linux). Make sure Nokia PC Suite / other software isn’t holding the port.
  • Manually test with PuTTY / minicom: send AT -> expect OK; AT+CMGF=1 -> OK; AT+CMGS="+number" -> you should get a > prompt. If any of those fail, fix the modem/driver first.
  • Ensure SIM is ready (not PIN-locked) with AT+CPIN? and that the phone has network service. Some carriers or phone configs require explicit SMSC (AT+CSCA) or charset (AT+CSCS) changes.

Minimal Python 2.6 pattern to follow

import serial
s = serial.Serial('COM4', 115200, timeout=2)
s.write('AT\r'); print s.readline()
s.write('AT+CMGF=1\r'); print s.readline()
s.write('AT+CMGS="+923219409998"\r')
# wait for the '>' prompt from the modem
buf = ''
while '>' not in buf:
    buf += s.read(1)
s.write('We did it!\r' + chr(26))    # send message then Ctrl+Z
print s.read(200)
s.close()

Additional tips: try common baud rates (9600, 38400, 115200), check for error responses read from the modem, and prefer waiting for the modem prompt rather than longer sleeps. Mentioning : format your code in the thread so others can test snippets faster.

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.