How to Send and Receive SMS Using GSM Modem

March 7, 2019 | Author: Ade Darma | Category: Short Message Service, Modem, Database Index, Databases, Server (Computing)
Share Embed Donate


Short Description

How to Send and Receive SMS Using GSM Modem...

Description

How To Send and Receive SMS using GSM Modem By Ranjan.D, 10 Sep 2007 4.88 (84 votes) 

Download source files - 295.1 KB

Introduction SMS client and server is an application software which is used for sending and receiving messages(SMS). It listens for incoming messages to arrive, processes the message if it's in a valid format. Note the processing of arrived messages depends on the application which will be discussed later. I am going to explain the following things: 1. Communication Port Settings 2. Receive Incoming Message 3. Send Messages

4. Read All Messages (Sent by the users) 5. Delete Messages (One or All) I have used the GSMComm Library for Sending and Receiving SMS. You require a GSM modem or phone for sending an SMS.

Using the code 1) Communication Port Settings

CommSetting class is used for storing comm port settings: Collapse | Copy Code public class CommSetting { public static int Comm_Port=0; public static Int64 Comm_BaudRate=0; public static Int64 Comm_TimeOut=0; public static GsmCommMain comm; public CommSetting() {  //   // TODO: Add constructor logic here  //  } }

Comm is an object of type GsmCommMain which is required for sending and receiving messages. We have to set theComm port, Baud rate and time out for our comm object of type GsmCommMain. Then try to open with the above settings. We can test the Comm port settings by clicking on the Test button after selecting the Comm port, baud rate and Time out. Sometimes if the comm port is unable to open, you will get a message "No phone connected". This is mainly due to Baud rate settings. Change the baud rate and check again by clicking the Test button until you get a message "Successfully connected to the phone."

Before creating a GSMComm object with settings, we need to validate the port number, baud rate and Timeout. The EnterNewSettings() does validation, returns true if valid, and will invoke SetData(port,baud,timeout)for comm setting. The following block of code will try to connect. If any problem occurs "Phone not connected" message appears and you can either retry by clicking on the Retry button or else Cancel. Collapse | Copy Code GsmCommMain comm = new GsmCommMain(port, baudRate, timeout); try { comm.Open(); while (!comm.IsConnected()) { Cursor.Current = Cursors.Default; if (MessageBox.Show( this, "No phone connected." , "Connection setup" , MessageBoxButtons.RetryCancel, MessageBoxIcon.Exclamation) == DialogResult.Cancel) { comm.Close(); return; } Cursor.Current = Cursors.WaitCursor; }  // Close Comm port connection (Since it's just for testing  // connection) comm.Close(); } catch(Exception ex) { MessageBox.Show(this, "Connection error: " + ex.Message, "Connection setup" , MessageBoxButtons.OK, MessageBoxIcon.Warning); return; }  // display message if connection is a success. MessageBox.Show( this, "Successfully connected to the phone." , "Connection setup" , MessageBoxButtons.OK, MessageBoxIcon.Information);

2) Receive Incoming Message

We are going to register the following events for GSMComm object comm. 1. PhoneConnected This event is invoked when you try to open the Comm port. The event handler for Phone connected iscomm_PhoneConnected which will invoke OnPhoneConnectionChange( bool connected) with the help of Delegate ConnectedHandler. 2. MessageReceived This event is invoked when a message arrives at the GSM phone. We will register withMessageReceivedEventHandler. When the incoming message arrives, the comm_MessageReceivedmethod will be invoked which in turn calls the MessageReceived() method in order to process the unread message. GSMComm object comm has a method ReadMessages which will be used for reading messages. It accepts the following parameters phone status (All, ReceivedRead , ReceivedUnread , StoredSent, andStoredUnsent ) and storage type: SIM memory or Phone memory. Collapse | Copy Code private void MessageReceived() { Cursor.Current = Cursors.WaitCursor; string storage = GetMessageStorage(); DecodedShortMessage[] messages = CommSetting.comm.ReadMessages (PhoneMessageStatus.ReceivedUnread, storage); foreach(DecodedShortMessage message in messages) { Output(string.Format("Message status = {0}, Location = {1}/{2}" , StatusToString(message.Status), message.Storage, message.Index)); ShowMessage(message.Data); Output(""); } Output(string.Format("{0,9} messages read." , messages.Length.ToString())); Output(""); }

The above code will read all unread messages from SIM memory. The method ShowMessage is used for displaying the read message. The message may be a status report, stored message sent/un sent, or a received message.

3) Send Message

You can send an SMS by keying in the destination phone number and text message. If you want to send a message in your native language (Unicode), you need to check in Send as Unicode(UCS2).GSMComm object comm has a SendMessage method which will be used for sending SMS to any phone. Create a PDU for sending messages. We can create a PDU in straight forward version as: Collapse | Copy Code SmsSubmitPdu pdu = new SmsSubmitPdu (txt_message.Text,txt_destination_numbers.Text, "");

An extended version of PDU is used when you are sending a message in Unicode. Collapse | Copy Code try {  // Send an SMS message SmsSubmitPdu pdu;

bool alert = chkAlert.Checked; bool unicode = chkUnicode.Checked; if (!alert && !unicode) {  // The straightforward version pdu = new SmsSubmitPdu (txt_message.Text, txt_destination_numbers.Text, ""); } else {  // The extended version with dcs byte dcs; if (!alert && unicode) dcs = DataCodingScheme.NoClass_16Bit; else if (alert && !unicode) dcs = DataCodingScheme.Class0_7Bit; else if (alert && unicode) dcs = DataCodingScheme.Class0_16Bit; else dcs = DataCodingScheme.NoClass_7Bit; pdu = new SmsSubmitPdu (txt_message.Text, txt_destination_numbers.Text, "", dcs); }  // Send the same message multiple times if this is set int times = chkMultipleTimes.Checked ? int.Parse(txtSendTimes.Text) : 1;  // Send the message the specified number of times for (int i=0;i
View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF