Welcome to weblogs.com.pk Sign in | Join | Help

Sending SMS Using AT Commands

GSM Phones can be hooked with your PC and the drivers that usually come with them get it configured as Modem. Once setup, you can open the serial port and send AT commands to it and with it can send SMS!

A friend of mine asked for some utility to send SMSes in bulk, and I gave him the above info. He is hobbyist programmer and I think he can get it done…However I wrote a small such application a while ago, when .NET v2 was released with Serial ports support…I digged and found this!

class SmsEngine : IDisposable
{
    SerialPort serialPort;
 
    SmsEngine() { }
 
    public SmsEngine(string comPort)
    {
        this.serialPort = new SerialPort();
        this.serialPort.PortName = comPort;
        this.serialPort.BaudRate = 9600;
        this.serialPort.Parity = Parity.None;
        this.serialPort.DataBits = 8;
        this.serialPort.StopBits = StopBits.One;
        this.serialPort.Handshake = Handshake.RequestToSend;
        this.serialPort.DtrEnable = true;
        this.serialPort.RtsEnable = true;
        this.serialPort.NewLine = System.Environment.NewLine;
    }
 
    public bool SendSMS(string CellNumber, string SMSMessage)
    {
        string messageToSend = null;
        if (SMSMessage.Length <= 160)
        {
            messageToSend = SMSMessage;
        }
        else
        {
            messageToSend = SMSMessage.Substring(0, 160);
        }
        if (this.IsOpen == true)
        {
            this.serialPort.WriteLine(@"AT" + (char)(13));
            Thread.Sleep(200);
            this.serialPort.WriteLine("AT+CMGF=1" + (char)(13));
            Thread.Sleep(200);
            this.serialPort.WriteLine(@"AT+CMGS=""" + CellNumber + @"""" + (char)(13));
            Thread.Sleep(200);
            this.serialPort.WriteLine(SMSMessage + (char)(26));
            return true;
        }
        return false;
    }
 
    public void Open()
    {
        if (this.IsOpen == false)
        {
            this.serialPort.Open();
        }
    }
 
    public void Close()
    {
        if (this.IsOpen == true)
        {
            this.serialPort.Close();
        }
    }
 
    public bool IsOpen
    {
        get
        {
            return this.serialPort.IsOpen;
        }
    }
 
    public void Dispose()
    {
        if (this.IsOpen)
            this.Close();
    }
}

I am also attaching a small command line utility (requires Microsoft.NET v2) with a hope that it will work…I am not testing it this time, as I am unable to find my mobile phone’ cable right now!

Usage

You can also use this program as
SmsAt [COM] [NUMBER] [PathToSomeFileHavingMessage]
eg SmsAt COM3 03001234567 Message.txt
Published Saturday, May 2, 2009 9:02 PM by khurram

Attachment(s): SmsAt.zip

Comments

# re: Sending SMS Using AT Commands

Monday, May 4, 2009 3:32 PM by Haider

is there anyway around to brand the SIM number , e.g use mine name in replacement of 03112154556 for sending SMS?

# re: Sending SMS Using AT Commands

Monday, May 4, 2009 5:14 PM by khurram

Yes, you need to send PDU encoded message...Above instead of AT+CMGF=1, you will be using AT+CMGF=0 and then your message should be PDU encoded.

- http://www.codeproject.com/KB/mobile/smspdulib.aspx is one library to encode/decode PDU SMSes.

- http://www.scampers.org/steve/sms/samples.htm is another (code not available). http://www.codeproject.com/KB/cs/SMS.aspx is complete GUI using this library

# re: Sending SMS Using AT Commands

Saturday, May 30, 2009 12:02 PM by khurram

Mahesh, which mobile phone you are using? I tested it with Nokia N73, N80 and N95, and they all worked fine...I am also using a variant of this code with a GSM Modem Device (Not a mobile phone, just some device which exposes itself as GSM Modem)

I suggest that you also try the other code/software I linked, the one at CodeProject.com, it however uses a closed source library but that library exposes more capabilites like PDU Encoded SMS, Receiving SMS etc.

# re: Sending SMS Using AT Commands

Tuesday, June 2, 2009 5:22 PM by khurram

AT

AT+CMGF=1

AT+CMGL="ALL"

will list all SMS from SIM

You can choose the store with +CPMS, e-g

AT+CPMS="SM"

SM for SIM, ME for Memory, MT all Stores, BM for Incoming Cell broardcasts, SR for Status Reports. +CMGD is used to delete the message, e-g

AT+CMGD=1

will delete first message

Also Google/Bing other related one like

+CNMI (New Message Indication)

+CMGR (Read Message)

+CNMA (New Message Acknowledgement)

# re: Sending SMS Using AT Commands

Monday, July 27, 2009 2:30 PM by khurram

@Mahesh regarding masking see http://weblogs.com.pk/khurram/archive/2009/05/02/sending-sms-using-at-commands.aspx#160405

@Mahesh regarding receiving message, cant say you have the code; did it worked?

New Comments to this post are disabled