Click here to Skip to main content
6,822,123 members and growing! (17,724 online)
Email Password   helpLost your password?
Languages » C# » General     Intermediate License: The Code Project Open License (CPOL)

Send and Read SMS through GSM Modem using AT Commands

By Syeda Anila Nusrat

In this article, you can Send and Read SMS through GSM Modem using AT Commands and without using any third party library.
C# (C#3.0), Windows (WinXP), .NET (.NET3.0), Visual-Studio (VS2005), WinForms, Dev
Revision:2 (See All)
Posted:4 Aug 2009
Views:41,455
Bookmarked:62 times
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
41 votes for this article.
Popularity: 7.76 Rating: 4.81 out of 5

1

2

3
5 votes, 12.2%
4
36 votes, 87.8%
5

Introduction

There are many different kinds of SMS applications in the market today and many others are being developed. Applications in which SMS messaging can be utilized are virtually unlimited. Some common examples of SMS applications are given below:

  • Person-to-person text messaging is the most commonly used SMS application and it is what the SMS technology was originally designed for.
  • Many content providers make use of SMS text messages to send information such as news, weather report and financial data to their subscribers.
  • SMS messages can carry binary data and so SMS can be used as the transport medium of wireless downloads. Objects such as ringtones, wallpapers, pictures and operator logos can be encoded in SMS messages.
  • SMS is a very suitable technology for delivering alerts and notifications of important events.
  • SMS messaging can be used as a marketing tool.

In general, there are two ways to send SMS messages from a computer / PC to a mobile phone:

  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 article, I will explain the first way to send, read and delete SMS using AT commands. But before starting, I would like to explain a little bit about AT Commands.

AT Commands

AT commands are instructions used to control a modem. AT is the abbreviation of ATtention. Every command line starts with "AT" or "at". That's why modem commands are called AT commands. There are two types of AT commands.

  1. Basic commands are AT commands that do not start with "+". For example, D (Dial), A (Answer), H (Hook control) and O (Return to online data state) are basic commands.
  2. Extended commands are AT commands that start with "+". All GSM AT commands are extended commands. For example, +CMGS (Send SMS message), +CMGL (List SMS messages) and +CMGR (Read SMS messages) are extended commands.

How to Test GSM Modem Connectivity Using Hyper Terminal

  • First, find the best GSM modem that suits the needs. I tested this application with Wavecom FASTRACK M1206.
  • Understand the AT Command set required to communicate with the modem.
  • Connect the modem to the computer according to the setup guide specified in the manual provided with the GSM modem.
  • Put a valid SIM card into the mobile phone or GSM/GPRS modem.
  • Connect your mobile phone or GSM/GPRS modem to a computer and set up the corresponding wireless modem driver.
  • Run MS HyperTerminal by selecting Start -> Programs -> Accessories -> Communications -> HyperTerminal.
  • In the Connection Description dialog box, enter a name and choose an icon you like for the connection. Then click the OK button.
  • In the Connect To dialog box, choose the COM port that your mobile phone or GSM/GPRS modem is connecting to in the Connect using combo box. For example, choose COM1 if your mobile phone or GSM/GPRS modem is connecting to the COM1 port. Then click the OK button.
  • The Properties dialog box comes out. Enter the correct port settings for your mobile phone or GSM/GPRS modem. Then click the OK button.
  • To find the correct port settings that should be used with your mobile phone or GSM/GPRS modem, consult the manual of your mobile phone or GSM/GPRS modem.
  • Type "AT" in the main window. A response "OK" should be returned from the mobile phone or GSM/GPRS modem
  • If “OK” returns, it means your mobile phone or GSM/GPRS modem is connected successfully.

After successful connection of GSM /GPRS modem with PC, you are ready to run this application. Download the attached project and run the application.

Sending SMS through GSM Modem using AT Commands

Port Settings

In this tab, you will have to do port settings which will be the same as you did in hyper terminal and then click the OK button. If modem is connected successfully, a message box will appear with the message “Modem is connected”.

public SerialPort OpenPort(string p_strPortName,
	int p_uBaudRate, int p_uDataBits, int p_uReadTimeout, int p_uWriteTimeout)
        {
            receiveNow = new AutoResetEvent(false);
            SerialPort port = new SerialPort();

            try
            {
                port.PortName = p_strPortName;                 //COM1
                port.BaudRate = p_uBaudRate;                   //9600
                port.DataBits = p_uDataBits;                   //8
                port.StopBits = StopBits.One;                  //1
                port.Parity = Parity.None;                     //None
                port.ReadTimeout = p_uReadTimeout;             //300
                port.WriteTimeout = p_uWriteTimeout;           //300
                port.Encoding = Encoding.GetEncoding("iso-8859-1");
                port.DataReceived += new SerialDataReceivedEventHandler
						(port_DataReceived);
                port.Open();
                port.DtrEnable = true;
                port.RtsEnable = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return port;
        }		

Send SMS

In the second tab, you can send SMS:

public bool sendMsg(SerialPort port, string PhoneNo, string Message)
        {
            bool isSend = false;

            try
            {
                string recievedData = ExecCommand(port,"AT", 300, "No phone connected");
                recievedData = ExecCommand(port,"AT+CMGF=1", 300,
					"Failed to set message format.");
                String command = "AT+CMGS=\"" + PhoneNo + "\"";
                recievedData = ExecCommand(port,command, 300,
					"Failed to accept phoneNo");
                command = Message + char.ConvertFromUtf32(26) + "\r";
                recievedData = ExecCommand(port,command, 3000,
					"Failed to send message"); //3 seconds
                if (recievedData.EndsWith("\r\nOK\r\n"))
                {
                    isSend = true;
                }
                else if (recievedData.Contains("ERROR"))
                {
                    isSend = false;
                }
                return isSend;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }     

Read SMS

In the third tab, you can read SMS:

public ShortMessageCollection ReadSMS(SerialPort port)
        {
            // Set up the phone and read the messages
            ShortMessageCollection messages = null;
            try
            {
                #region Execute Command
                // Check connection
                ExecCommand(port,"AT", 300, "No phone connected");
                // Use message format "Text mode"
                ExecCommand(port,"AT+CMGF=1", 300, "Failed to set message format.");
                // Use character set "PCCP437"
                ExecCommand(port,"AT+CSCS=\"PCCP437\"", 300,
				"Failed to set character set.");
                // Select SIM storage
                ExecCommand(port,"AT+CPMS=\"SM\"", 300,
				"Failed to select message storage.");
                // Read the messages
                string input = ExecCommand(port,"AT+CMGL=\"ALL\"", 5000,
					"Failed to read the messages.");
                #endregion

                #region Parse messages
                messages = ParseMessages(input);
                #endregion
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

            if (messages != null)
                return messages;
            else
                return null;
        }

Delete SMS

In the fourth and last tab, you can count the number of SMS and delete SMS as well.

public bool DeleteMsg(SerialPort port , string p_strCommand)
        {
            bool isDeleted = false;
            try
            {
                #region Execute Command
                string recievedData = ExecCommand(port,"AT", 300, "No phone connected");
                recievedData = ExecCommand(port,"AT+CMGF=1", 300,
					"Failed to set message format.");
                String command = p_strCommand;
                recievedData = ExecCommand(port,command, 300, "Failed to delete message");
                #endregion

                if (recievedData.EndsWith("\r\nOK\r\n"))
                {
                    isDeleted = true;
                }
                if (recievedData.Contains("ERROR"))
                {
                    isDeleted = false;
                }
                return isDeleted;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }  

Points of Interest

I'm not using any third party library or anything else in this project.

History

  • 4th August, 2009: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Syeda Anila Nusrat


Member
My name is Syeda Anila Nusrat. I have completed BS(CS)in 2008 from Federal Urdu University of Arts Science and Technology. I m working as a software developer from Feb 2008.
Occupation: Software Developer
Company: Syndustria Pvt Ltd
Location: Pakistan Pakistan

Other popular C# articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 190 (Total in Forum: 190) (Refresh)FirstPrevNext
GeneralNo phone connected PinmemberMember 459244320:51 28 Jan '10  
GeneralRe: No phone connected PinmemberAnkur_Mundhra23:26 29 Jan '10  
GeneralHiii Alert when call or message recieved Pinmemberbhanuprakashchamakuri21:14 26 Jan '10  
GeneralRe: Hiii Alert when call or message recieved Pinmemberbhanuprakashchamakuri2:55 30 Jan '10  
GeneralRe: Hiii Alert when call or message recieved PinmemberSyeda Anila Nusrat3:13 1 Feb '10  
GeneralRead Sms PinmemberMember 442315423:47 20 Jan '10  
AnswerRe: Read Sms PinmemberAnkur_Mundhra19:12 21 Jan '10  
GeneralRe: Read Sms PinmemberSyeda Anila Nusrat7:09 24 Jan '10  
GeneralRe: Read Sms [modified] PinmemberMember 442315421:18 25 Jan '10  
GeneralRe: Read Sms PinmemberMember 442315420:24 27 Jan '10  
GeneralRe: Read Sms PinmemberSyeda Anila Nusrat3:00 1 Feb '10  
GeneralRe: Read Sms PinmemberMember 442315421:20 3 Feb '10  
GeneralRead A sms as PDU mode PinmemberMember 189408519:06 20 Jan '10  
AnswerRe: Read A sms as PDU mode PinmemberAnkur_Mundhra19:19 20 Jan '10  
GeneralRe: Read A sms as PDU mode PinmemberSyeda Anila Nusrat7:19 24 Jan '10  
GeneralProblem in connecting to samsung f270 Pinmembersamata kawankar4:37 16 Jan '10  
GeneralRe: Problem in connecting to samsung f270 PinmemberSyeda Anila Nusrat7:25 24 Jan '10  
GeneralRe: Problem in connecting to samsung f270 Pinmembersamata kawankar4:20 25 Jan '10  
GeneralRe: Problem in connecting to samsung f270 PinmemberSyeda Anila Nusrat18:02 26 Jan '10  
GeneralRe: Problem in connecting to samsung f270 Pinmembersamata kawankar2:12 27 Jan '10  
GeneralRe: Problem in connecting to samsung f270 PinmemberAnkur_Mundhra0:05 2 Feb '10  
GeneralVery good work ! PinmemberGerrit4444:31 15 Jan '10  
GeneralRe: Very good work ! PinmemberSyeda Anila Nusrat7:30 24 Jan '10  
GeneralFantastic Work PinmemberAnkur_Mundhra3:20 14 Jan '10  
GeneralRe: Fantastic Work PinmemberSyeda Anila Nusrat7:40 24 Jan '10  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads.

PermaLink | Privacy | Terms of Use
Last Updated: 4 Aug 2009
Editor: Deeksha Shenoy
Copyright 2009 by Syeda Anila Nusrat
Everything else Copyright © CodeProject, 1999-2010
Web21 | Advertise on the Code Project