Developing SMS Services in C#
This following project has been sticking my back since my engineering from past 4 years.Ever since then the same cookie was served in different plates. i meant to say one functionality was implemented for different applications.
In this post i will be implementing the project for a college domain.Say there is a database that stores the student details like attendance,marks,profile,etc,.Our application will receive a message sent from cell phone and query the database to retrieve the result and send it back to the respective user.
Now to receive & send a message we will be using a GSM modem(something similar to a cell phone) now the modem looks something like this
the black slot on the PCB(printed circuit board) is to insert a sim card.
"A picture is worth thousand words " so trying to put the whole process in nut shell.....
I know this is not worth thousand words, forgive me will try to picturize better next time.
Lets start CODING!!
To communicate with the modem we were to use COM object called MSComm in the first place,well now .NET framework has made things lot simpler by providing a class SerialPort which resides in System.IO.Ports namespace.This class exposes write and read function so that we can send commands or data to the device connected through the serial port.
In our case we will be communicating with the device using AT(attention) commands.There are hundreds of commands that we are least bothered about in this post. we will be using only the following few commands
AT+CMGF=<1>||<0>
This command is used to set mode of the modem ie. there are 2 modes PDU and Text mode.In PDU mode all the message contents are in hexa-decimal format and Text mode in text as simple as it is.
so in our project we will makes thing readable and use AT+CMGF=1.
AT+CMGR=<index>
This command retrieves the message from the specified index.
eg: AT+CMGR=1 will get first message from the sim memory.
AT+CMGD=<index>
one last command....
AT+CMGS=<cell phone number><CR><SMS body><Ctrl-Z>
This command is used to send message to the specified number.
eg:
AT+CMGS=+919916242328 now press Enter key (carriage return) this will cause to show a prompt after which we will enter the text message that is to be sent, to exit the message body press ctrl-Z.
Let me get back to the SerialPort class i was talking about earlier.Now to communicate with the modem we need to know its language and that would be the AT commands, and also a medium that would be the serial port buffer.So if we want to
Read Message
public static string ReadMessage(int index)
{
using (SerialPort sp = new SerialPort(_portNumber))
{
sp.Open();
sp.Write("AT+CMGR=" + index + "\r");
Thread.Sleep(2000);
return sp.ReadExisting();
}
}
Send Message
public static string SendMessage(string phoneNumber, string message)
{
using (SerialPort sp = new SerialPort(_portNumber))
{
sp.Open();
sp.Write("AT+CMGS=\"" + phoneNumber + "\"\r" + message + char.ConvertFromUtf32(26));
Thread.Sleep(2000);
return sp.ReadExisting();
}
}
Delete Message
public static string DeleteMessage(int index)
{
using (SerialPort sp = new SerialPort(_portNumber))
{
sp.Open();
sp.Write("AT+CMGD=" + index + "\r");
Thread.Sleep(2000);
return sp.ReadExisting();
}
}
With all the above SMS reading,sending,deleting functions we will able to get the keyword sent by the user and retrieve the appropriate data the user asked for, in our case keyword may be 4NM04IS069 Attendance(<registration number><space>Attendance) and the in reply to the query the user will get the attendance of the registered student.
Now choose a plate and serve the cookie. ;-)
Nothing is invented in here all we had to do was Discover!!
Find the complete class in here
Feel free to correct me.Comments are welcome.
Credits: Rakesh Shetty,Nithinraj K, Shreepathi A R
In this post i will be implementing the project for a college domain.Say there is a database that stores the student details like attendance,marks,profile,etc,.Our application will receive a message sent from cell phone and query the database to retrieve the result and send it back to the respective user.
Now to receive & send a message we will be using a GSM modem(something similar to a cell phone) now the modem looks something like this
the black slot on the PCB(printed circuit board) is to insert a sim card.
"A picture is worth thousand words " so trying to put the whole process in nut shell.....
I know this is not worth thousand words, forgive me will try to picturize better next time.
Lets start CODING!!
To communicate with the modem we were to use COM object called MSComm in the first place,well now .NET framework has made things lot simpler by providing a class SerialPort which resides in System.IO.Ports namespace.This class exposes write and read function so that we can send commands or data to the device connected through the serial port.
In our case we will be communicating with the device using AT(attention) commands.There are hundreds of commands that we are least bothered about in this post. we will be using only the following few commands
AT+CMGF=<1>||<0>
This command is used to set mode of the modem ie. there are 2 modes PDU and Text mode.In PDU mode all the message contents are in hexa-decimal format and Text mode in text as simple as it is.
so in our project we will makes thing readable and use AT+CMGF=1.
AT+CMGR=<index>
This command retrieves the message from the specified index.
eg: AT+CMGR=1 will get first message from the sim memory.
AT+CMGD=<index>
This command deletes the message from the specified index.
eg: AT+CMGD=1 will delete first message from the sim memory.
one last command....
AT+CMGS=<cell phone number><CR><SMS body><Ctrl-Z>
This command is used to send message to the specified number.
eg:
AT+CMGS=+919916242328 now press Enter key (carriage return) this will cause to show a prompt after which we will enter the text message that is to be sent, to exit the message body press ctrl-Z.
Let me get back to the SerialPort class i was talking about earlier.Now to communicate with the modem we need to know its language and that would be the AT commands, and also a medium that would be the serial port buffer.So if we want to
Read Message
public static string ReadMessage(int index)
{
using (SerialPort sp = new SerialPort(_portNumber))
{
sp.Open();
sp.Write("AT+CMGR=" + index + "\r");
Thread.Sleep(2000);
return sp.ReadExisting();
}
}
Send Message
public static string SendMessage(string phoneNumber, string message)
{
using (SerialPort sp = new SerialPort(_portNumber))
{
sp.Open();
sp.Write("AT+CMGS=\"" + phoneNumber + "\"\r" + message + char.ConvertFromUtf32(26));
Thread.Sleep(2000);
return sp.ReadExisting();
}
}
Delete Message
public static string DeleteMessage(int index)
{
using (SerialPort sp = new SerialPort(_portNumber))
{
sp.Open();
sp.Write("AT+CMGD=" + index + "\r");
Thread.Sleep(2000);
return sp.ReadExisting();
}
}
With all the above SMS reading,sending,deleting functions we will able to get the keyword sent by the user and retrieve the appropriate data the user asked for, in our case keyword may be 4NM04IS069 Attendance(<registration number><space>Attendance) and the in reply to the query the user will get the attendance of the registered student.
Now choose a plate and serve the cookie. ;-)
Nothing is invented in here all we had to do was Discover!!
Find the complete class in here
Feel free to correct me.Comments are welcome.
Credits: Rakesh Shetty,Nithinraj K, Shreepathi A R
Comments
Post a Comment