In Today's article i will demonstrate you How to send and received sms in C# using AT Commands with Complete Source Code.
In This Tutorials I will not use any sms sending and Receiving API , but Learning this Post Completely Care fully INSHA Allah you will be able Develop your Own SMS sending and Receiving API or Classes according to your requirement.
In This tutorials i will use AT commands .AT commands are the Commands which communicates and Controls your Mobile or GSM Modem for Sending and Receiving SmS, you may also Get other Information like Signals Strengths Etc. So it is Recommended for you Before reading this Tutorials to go through AT commands. Following are the links which will help you develop the base :-
1) Introduction to At Commands
2) zoltrix
Now Lets Start,
In general, there are two ways to send SMS from a computer / PC to a mobile phone:
DOWNLOADS Source Code
DOWNLOAD SOURCE CODE For VS 2008 (Google docs)
DOWNLOAD SOURCE CODE For VS 2010 (Google docs)
OR
DOWNLOAD SOURCE CODE For VS 2008 (4 shared)
DOWNLOAD SOURCE CODE For VS 2010 (4 shared)
In This Tutorials I will not use any sms sending and Receiving API , but Learning this Post Completely Care fully INSHA Allah you will be able Develop your Own SMS sending and Receiving API or Classes according to your requirement.
In This tutorials i will use AT commands .AT commands are the Commands which communicates and Controls your Mobile or GSM Modem for Sending and Receiving SmS, you may also Get other Information like Signals Strengths Etc. So it is Recommended for you Before reading this Tutorials to go through AT commands. Following are the links which will help you develop the base :-
1) Introduction to At Commands
2) zoltrix
Now Lets Start,
In general, there are two ways to send SMS from a computer / PC to a mobile phone:
- Connect a mobile phone or GSM/GPRS modem to a computer . Then use the computer and AT commands to instruct the mobile phone or GSM/GPRS modem to send SMS messages.
- Connect the computer 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.
AT Commands (Little Introduction)
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:- Basic commands are AT commands that do not start with a "+". For example, D (Dial), A (Answer), H (Hook control), and O (Return to online data state) are the basic commands.
- Extended commands are AT commands that start with a "+". 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.
Operating Modes Of SMS
The SMS specification has defined two modes in which a GSM/GPRS modem or mobile phone can operate. They are called SMS text mode and SMS PDU mode. (PDU stands for Protocol Data Unit.) The mode that a GSM/GPRS modem or mobile phone is operating in determines the syntax of some SMS AT commands and the format of the responses returned after execution.In this article I am using SMS text mode .Port Settings
First you will have to do port settings which will be the same as you did in the hyper terminal and then click the OK button. If the 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
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
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; }
DOWNLOADS Source Code
DOWNLOAD SOURCE CODE For VS 2008 (Google docs)
DOWNLOAD SOURCE CODE For VS 2010 (Google docs)
OR
DOWNLOAD SOURCE CODE For VS 2008 (4 shared)
DOWNLOAD SOURCE CODE For VS 2010 (4 shared)
This code I downloaded .but it is hanging for connection itself. Sometimes it seems to be perfectly working.
ReplyDeleteI recommend to use sms/GSM modems not a mobile phone because there is a big Processing difference between mobile and PC processing speed.
ReplyDeleteChange the port number if the working seems unstable. For eg, change from COM4 to COM23 (make sure they aren't being used for other connections). It will work perfectly then.
ReplyDeleteThis code I downloaded . it sending sms work properly but reading and deleting of sms not work
ReplyDeleteI have test this code on SMS/GSM modems(Teltonika GSM modem,hawaii sms modem) and on LG-KG195 work perfect.
Delete