![]() |
Languages »
C# »
General
Intermediate
License: The Code Project Open License (CPOL)
Send and Read SMS through GSM Modem using AT CommandsBy Syeda Anila NusratIn 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
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
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:
In general, there are two ways to send SMS messages from a computer / PC to a mobile phone:
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 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.
After successful connection of GSM /GPRS modem with PC, you are ready to run this application. Download the attached project and run the application.
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;
}
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);
}
}
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;
}
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);
}
}
I'm not using any third party library or anything else in this project.
General
News
Question
Answer
Joke
Rant
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 |