Social Icons

Pages

Tuesday, July 10, 2012

How to Send SMS using GSM Modem in C#

This article explains in the simple way to send the SMS using GSM Modem in C#.

1.  First, connect the GSM Modem and find the COM port. To do this
  • Click on the Start button in the bottom left corner of your Screen. (shortcut Win+R)
  • Then type "devmgmt.msc", and hit Enter.
  • If Windows asks for your permission to continue, click on Continue.
  • When the device manager shows up, Find the entry that says "Ports (COM & LPT)" and click on the (+) beside it to expand it.
  • The COM port number is shown in parentheses beside the name.
 2.  Create a new Windows forms application and add three textboxes for Mobile number(txtmobile),Port number(txtport) and message(txtmessage).

3.  Add the following namespace.
  using System.IO.Ports;
 4.  Then add a button control(butSend) to your form and write the code in the button_click event.
          SerialPort serialport = new SerialPort();
           try
           {
               int mSpeed = 1;
               serialport.PortName = txtport.Text;
               serialport.BaudRate = 96000;
               serialport.Parity = Parity.None;
               serialport.DataBits = 8;
               serialport.StopBits = StopBits.One;
               serialport.Handshake = Handshake.XOnXOff;
               serialport.DtrEnable = true;
               serialport.RtsEnable = true;
               serialport.NewLine = Environment.NewLine;
             try
                    {
                        serialport.Open();
                    }
                    catch (Exception)
                    {
               MessageBox.Show("Try another Port."+ Environment.NewLine +"Phone not detected or The requested resource is in use.","CONNECTION ERROR",   MessageBoxButtons.OK,MessageBoxIcon.Error);
                     return;
                    }

               serialport.WriteLine("AT+CMGF=1" + Environment.NewLine);
               System.Threading.Thread.Sleep(200);
               serialport.WriteLine("AT+CSCS=GSM" + Environment.NewLine);
               System.Threading.Thread.Sleep(200);
               serialport.WriteLine("AT+CMGS=" + (char)34 + txtmobile.Text
               + (char)34 + Environment.NewLine);
               System.Threading.Thread.Sleep(200);
               serialport.WriteLine(txtmessage. Text + (char)26);
               System.Threading.Thread.Sleep(mSpeed);
               serialport.Close();
           }
           catch (Exception)
           {
               if (serialport.IsOpen)
               serialport.Close(); 
            MessageBox.Show("Couldn't send the SMS.","CONNECTION ERROR",MessageBoxButtons.OK,MessageBoxIcon.Error);
           }
 5.  Run the program
      Enter the
Mobile number,COM Port and message and click on send buton to send the SMS.

Some useful articles:
1. http://codeglobe.blogspot.co.uk/2009/02/sending-sms-in-cnet-using-gsm-modem-and.html
2. http://www.codeproject.com/Articles/20420/How-To-Send-and-Receive-SMS-using-GSM-Modem

11 comments:

  1. How can read from GSM Modem under click event

    ReplyDelete
    Replies
    1. Hi,
      First connect the GSM modem and follow step 1 to identify the port number
      then you can read GSM modem using “using System.IO.Ports;” namespace

      Delete
  2. Can you post for read sms from GSM Modem.

    ReplyDelete
    Replies
    1. Hi Syedur Rahman,
      Sorry. I don’t have time to write an article immediately. Please go through the following link.
      http://www.codeproject.com/Articles/38705/Send-and-Read-SMS-through-a-GSM-Modem-using-AT-Com
      http://www.codeproject.com/Articles/20420/How-To-Send-and-Receive-SMS-using-GSM-Modem

      Delete
  3. Hi,
    I write this code for read sms from GSM modem. I can not read sms

    protected void ReadSMS_Click(object sender, EventArgs e)
    {
    openport();//Port Related function


    // Select SIM storage
    serialport.WriteLine("AT+CPMS=" + Environment.NewLine);
    System.Threading.Thread.Sleep(200);
    // Read the messages
    txtreadmessage.Text = serialport.ReadExisting();
    serialport.WriteLine("AT+CMGL=" + Environment.NewLine);
    System.Threading.Thread.Sleep(200);
    serialport.Close();
    }


    Can you tell where i wrong in this code or post sample code for read. I huge googling but still now i could not success.

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. hii mathy.... how to read the gsm modem response/reply by programatically ???

    ReplyDelete
  6. i am facing a problem ' The Resource is in use' how to resolve this problem ....
    my code is
    port.PortName = p_strPortName; //COM1
    port.BaudRate = p_uBaudRate; //9600
    port.DataBits = 8; //8
    port.StopBits = StopBits.One; //1
    port.Parity = Parity.None; //None
    port.ReadTimeout = 300; //300
    port.WriteTimeout = 300; //300
    port.Encoding = Encoding.GetEncoding("iso-8859-1");
    SystemEvents.PowerModeChanged += new PowerModeChangedEventHandler(SystemEvents_PowerModeChanged);
    port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
    port.Open();
    port.DtrEnable = true;
    port.RtsEnable = true;
    port.Handshake = Handshake.None;

    ReplyDelete
    Replies
    1. Hi mayank geete,
      The modem should not be in other process while using for SMS.
      Remove the modem, plug in again & try to send the SMS again.

      Delete
  7. can we use this code for MVc web application ?

    ReplyDelete
  8. Bulk Sms Sender By Ad Technology
    One Time Payment Life Time Use
    Send Sms From Your Pc With Gsm Modem
    Auto Send Random From Your Numbers List
    Easy To Use For Multiply Sending
    Also You Can Send With your Mobile Phone (No need To Have Modem)
    Auto Save Sending Done Or Failed Information
    Show Real Time Information
    Auto Send With Time And Date
    https://www.youtube.com/watch?v=cQtME6mpACQ

    ReplyDelete