Subscribe For Free Updates!

We'll not spam mate! We promise.

Apr 30, 2012

Send and Receive SMS Through AT commands

Views:

Send Sms Through AT commands
AT commands is use to communicate with GSM  Madame and Mobile Phone.There are a lot of libraries which has been made using AT commands and These libraries are not Free it charge money for Using it But Today You learn how to Send and receive sms through AT commands .

Using AT commands you're not restricted to a specific phone model, as long as the phone supports the commands you are using.

This program uses the text mode for sending the message, which is good for starting, but it may not be supported by every phone. If you want to use the PDU mode instead of the text mode - which is supported by all mobiles but is more complicated - see the PDU encoding sample for creating the required data.
Sample source code is available for VB6.
Details about the VB6 sample:
  • Shows how to use the MSComm ActiveX control
  • Shows how to use the SMS AT commands
  • Includes a trace output to see what's going on
  • Includes error handling for common errors
  • Is not optimized for performance

Download
VB6 projectVB6: SendSMS_AT_VB6_20041025.zip

Encoding SMS messages for PDU mode

This demonstrates how to create an SMS-SUBMIT PDU for a simple text message.
Download
VB6 projectVB6: EncodePDU_VB6_20040419.zip
C# projectC#: EncodePDU_CS_20040418.zip
Requires .NET Framework 1.1
Note: This program does only the coding, it does not actually send the message. For testing you can use a terminal program like HyperTerminal (included in Windows) to transfer the encoded message, as given in the following sample:

AT+CMGF=0<CR>
AT+CMGS=<actual PDU length><CR>
<encoded message><EOF>
<CR> = ASCII 13 = ENTER
<EOF> = ASCII 26 = CTRL+Z

Using AT commands to read SMS messages

Using AT commands you're not restricted to a specific phone model, as long as the phone supports the commands you are using.
This program uses the text mode for reading the messages, which is good for learning how it works, but it may not be supported by every phone. You may have to change character sets depending on the characters used in the messages to get the original text back.
Download
C# projectC#: ReadSMS_AT_CS20_20060718.zip
Requires .NET Framework 2.0

Basically there are two ways to send SMS.

  1. Connect a mobile phone or GSM/GPRS modem to a computer / PC. Then use the computer / PC and AT commands to instruct the mobile phone or GSM/GPRS modem to send SMS messages.
  2. Connect the computer / PC to the SMS center (SMSC) or SMS gateway of a wireless carrier or SMS service provider. Then send SMS messages using a protocol / interface supported by the SMSC or SMS gateway.
 In this tutorial we'll use first method. We can send SmS through AT commands, AT commands are instructions to Send/Receive SMS. (for more detail about AT Commands wait for my Next Post about AT commands).

Lets Start..
Step-1
Open Visual Studio, Create a New Project with the Name SMS. (In this tutorial I'm not going to show you how to create a new project, I'm skipping the detail about it.)

Step-2
In the solution Explorer, Right Click on the Project and Click on Add -> Add New Item and Add New Class with the name SmsClass.cs copy the following code in the class.

SmsClass.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.IO.Ports;
using System.Windows.Forms;
namespace SMS
{
    class SmsClass
    {
        SerialPort serialPort;
        public SmsClass(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 cellNo, string sms)
        {
            string messages = null;
            messages = sms;
            if (this.serialPort.IsOpen == true)
            {
                try
                {
                    this.serialPort.WriteLine("AT" + (char)(13));
                    Thread.Sleep(4);
                    this.serialPort.WriteLine("AT+CMGF=1" + (char)(13));
                    Thread.Sleep(5);
                    this.serialPort.WriteLine("AT+CMGS=\"" + cellNo + "\"");
                    Thread.Sleep(10);
                    this.serialPort.WriteLine(">" + messages + (char)(26));
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Source);
                }
                return true;
            }
            else
                return false;
        }

        public void Opens()
        {
            if (this.serialPort.IsOpen == false)
            {
                this.serialPort.Open();
            }
        }
        public void Closes()
        {
            if (this.serialPort.IsOpen == true)
            {
                this.serialPort.Close();
            }
        }
    }
}
Step-3
Now on the Form1, we'll use
  1. combo Box (cboPorts) which will show the available ports on the computer.
  2. Two TextBoxes One for Receivers Cell No. (txtphone) and other for Message body (txtMessage) with Multiline property set to True.
  3. Send Button (btnSend) to Send SMS.
 Step-4
Copy and Paste the following code in the Form1 Code.
Form1
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
namespace SMS
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            loadPorts();
        }
        private void loadPorts()
        {
            string[] ports = SerialPort.GetPortNames();
            foreach (string port in ports)
            {
                cboPorts.Items.Add(port);
            }
        }
        private void btnSend_Click(object sender, EventArgs e)
        {
            SmsClass sm = new SmsClass(cboPorts.Text);
            sm.Opens();
            sm.sendSms(txtPhone.Text, txtMessage.Text);
            sm.Closes();
            MessageBox.Show("Message Sent!");
        }

      
    }
}
Now , You Just need to connect your GSM modem/mobile to your PC through USB or Serial cable.
and Run the Project. From the main Window Select COM Port  on which your Modem/Phone is Connected to.

Note: This application is Tested using Nokia N82 By Me...

Download Source Code:
http://www.multiupload.com/1E2JGHS1ZF


Plese Feel Free to Socializer This Post
SOCIALIZE IT →
FOLLOW US →
SHARE IT →

4 comments:

  1. Sir, can I use these Codes for my Web Application?

    ReplyDelete
  2. YES YOU CAN this code for web application but your webserver must connected with GSM Deveice (or mobile phone)....To send and Receive sms

    ReplyDelete
  3. Hi sir,
    can i use vb6 codes for my application?and how can i connect my mobile to webserver?give details.thnxs

    ReplyDelete
  4. yes you can use vb6 codes for your application ...... And for connecting your mobile to web-server there are two possibilities
    1) create a web service which input a Cell number and Message and send sms.
    2) save your messages (which you want to send) in database table and create a windows service which continuously reading this table and if new records for sending sms is came then read information from this table and send SMS.

    ReplyDelete

Become a Fan

visual studio learn