How to send sms using AT command with GSM GPRS Modem

Here is a quick tutorial to demonstrate How to send sms using AT command with GSM GPRS Modem. designed in visual wed developer 2010 with ajax and vb code behind.

In APP_Code folder create a file named vinsms.vb




Imports Microsoft.VisualBasic
Imports System.IO.Ports
Imports System.Threading

Public Class vinsms
    ' vinod kotiya , www.vinodkotiya.com
  
     Shared readNow As New AutoResetEvent(False)
    Shared port As SerialPort


    Public Shared Function sendSMS(ByVal cell As String, ByVal message As String, ByVal portname As String) As String
        Dim log As String = ""
        Try
            '"Enter the port name your phone is connected to 

            port = EstablishConnection(portname)
            Dim recievedData As String = ExecuteCommand("AT", 300)
            log = log + "
"
+ recievedData
            recievedData = ExecuteCommand(" AT+CREG?", 300)
            log = log + "
"
+ recievedData
            If recievedData.Contains("0,2") Then
                Return log + "
No network"
            End If

            recievedData = ExecuteCommand("AT+CMGF=1", 300)
            log = log + "
"
+ recievedData
            '"Enter the phone number you want to send message to:   
            Dim phoneNumber As [String] = cell '"+919411103810"
            Dim command As [String] = "AT+CMGS=""" & phoneNumber & """"

            '"Enter the message you want to send 
            recievedData = ExecuteCommand(command, 300)
            log = log + "
"
+ recievedData


            '    command = message & Char.ConvertFromUtf32(26) & vbCr
            command = message & vbCrLf & Chr(26)
            'command = "AT+CREG?"
            recievedData = ExecuteCommand(command, 300)
            log = log + "
"
+ recievedData
            If recievedData.Contains("OK") Then 'vbCr & vbLf & "OK" & vbCr & vbLf
                recievedData = "Message sent successfully"
            End If
            If recievedData.Contains("ERROR") Then
                Dim recievedError As String = recievedData
                recievedError = recievedError.Trim()
                recievedData = "Following error occured while sending the message " & recievedError
                log = log + "
"
+ recievedData
            End If


        Catch ex As Exception
            log = log + "
"
+ "Error Message: " & ex.Message.Trim()

        Finally
            ' If port IsNot Nothing Then
            port.Close()
            RemoveHandler port.DataReceived, New SerialDataReceivedEventHandler(AddressOf DataReceived)
            port = Nothing
            log = log + "
Port has been closed succesfully"
            'End If

        End Try
        Return log
    End Function

    Private Shared Function ExecuteCommand(ByVal command As String, ByVal timeout As Integer) As String
        port.DiscardInBuffer()
        port.DiscardOutBuffer()
        readNow.Reset()
        port.Write(command & vbCr)
        Dim recieved As String = receive(timeout)
        Return recieved
    End Function
    Private Shared Function receive(ByVal timeout As Integer) As String
        Dim buffer As String = String.Empty
        Do
            If readNow.WaitOne(timeout, False) Then
                Dim t As String = port.ReadExisting()
                buffer += t
            End If
        Loop While Not buffer.EndsWith(vbCr & vbLf & "OK" & vbCr & vbLf) AndAlso Not buffer.EndsWith(vbCr & vbLf & "> ") AndAlso Not buffer.Contains("ERROR")
        'Loop While Not buffer.Contains("OK") AndAlso Not buffer.EndsWith(">") AndAlso Not buffer.Contains("ERROR")
        Return buffer
    End Function

    Private Shared Function EstablishConnection(ByVal portName As String) As SerialPort
        Dim port As New SerialPort()
        port.PortName = portName
        port.BaudRate = 115200
        port.DataBits = 8
        port.StopBits = StopBits.One
        port.Parity = Parity.None
        port.ReadTimeout = 300
        port.WriteTimeout = 300
        ' port.Encoding = Encoding.GetEncoding("iso-8859-1")
        AddHandler port.DataReceived, New SerialDataReceivedEventHandler(AddressOf DataReceived)
        port.Open()
        port.DtrEnable = True
        port.RtsEnable = True
        Return port
    End Function

    Private Shared Sub DataReceived(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs)
        If e.EventType = SerialData.Chars Then
            readNow.[Set]()
        End If
    End Sub
  
    '    2. Using the HyperTerminal

    'Hint :: By developing your AT commands using HyperTerminal, it will be easier for you to develop your actual program codes in VB, C, Java or other platforms.

    'Go to START\Programs\Accessories\Communications\HyperTerminal (Win 2000) to create a new connection, eg. "My USB GSM Modem". Suggested settings ::

    ' - COM Port :: As indicated in the T-Modem Control Tool
    ' - Bits per second :: 115200 ( or slower )
    ' - Data Bits : 8
    ' - Parity : None
    ' - Stop Bits : 1
    ' - Flow Control : Hardware

    'You are now ready to start working with AT commands. Type in "AT" and you should get a "OK", else you have not setup your HyperTerminal correctly. Check your port settings and also make sure your GSM modem is properly connected and the drivers installed.

    '3. Initial setup AT commands

    'We are ready now to start working with AT commands to setup and check the status of the GSM modem.

    'AT       Returns a "OK" to confirm that modem is working
    'AT+CPIN="xxxx"         To enter the PIN for your SIM ( if enabled )
    'AT+CREG? A "0,1" means logged in reply confirms your modem is connected to GSM network
    'AT+CSQ   Indicates the signal strength, 31.99 is maximum.
    '4. Sending SMS using AT commands

    'We suggest try sending a few SMS using the Control Tool above to make sure your GSM modem can send SMS before proceeding. Let's look at the AT commands involved ..

    'AT+CMGF=1       To format SMS as a TEXT message
    'AT+CSCA="+xxxxx"       Set your SMS center's number. Check with your provider.
    'To send a SMS, the AT command to use is AT+CMGS ..

    'AT+CMGS="+yyyyy"
    '> Your SMS text message here

    'The "+yyyyy" is your receipent's mobile number. Next, we will look at receiving SMS via AT commands.

    '5. Receiving SMS using AT commands

    'The GSM modem can be configured to response in different ways when it receives a SMS.

    'a) Immediate - when a SMS is received, the SMS's details are immediately sent to the host computer (DTE) via the +CMT command

    'AT+CMGF=1       To format SMS as a TEXT message
    'AT+CNMI=1,2,0,0,0      Set how the modem will response when a SMS is received
    'When a new SMS is received by the GSM modem, the DTE will receive the following ..
    '    Example: Receive SMS

    'A SMS will be stored in the GSM modem / module and being send via RS232 to the peripherals. The  peripherals have to send commands to the GSM unit to receive SMS and to erase SMS from the  SIM card in order to clean memory.

    '+CMTI:"SM",x    X stands for the memory number of received SMS
    'AT+CMGR=X[Enter]    Read SMS on memory number X
    'AT+CMGD=X[Enter]    Erase SMS on memory number X
    '+CMT :  "+61xxxxxxxx" , , "04/08/30,23:20:00+40"    'This the text SMS message sent to the modem

    'Your computer (DTE) will have to continuously monitor the COM serial port, read and parse the message.

    'b) Notification - when a SMS is recieved, the host computer ( DTE ) will be notified of the new message. The computer will then have to read the message from the indicated memory location and clear the memory location.

    'AT+CMGF=1       To format SMS as a TEXT message
    'AT+CNMI=1,1,0,0,0      Set how the modem will response when a SMS is received
    'When a new SMS is received by the GSM modem, the DTE will receive the following ..

    '+CMTI: "SM",3   Notification sent to the computer. Location 3 in SIM memory
    'AT+CMGR=3      AT command to send read the received SMS from modem
    'The modem will then send to the computer details of the received SMS from the specified memory location ( eg. 3 ) ..

    '+CMGR: "REC READ","+61xxxxxx",,"04/08/28,22:26:29+40"
    'This is the new SMS received by the GSM modem

    'After reading and parsing the new SMS message, the computer (DTE) should send a AT command to clear the memory location in the GSM modem ..

    'AT+CMGD=3    To clear the SMS receive memory location in the GSM modem

    'If the computer tries to read a empty/cleared memory location, a +CMS ERROR : 321 will be sent to the computer.
    '    Important Commands
    'Note: The answers for networks and field strength can be delayed for several seconds.
    'ATZ;E[Enter]
    'Echo OFF
    'ATZ;E1[Enter]
    'Echo ON
    'AT+CSQ[Enter]
    'Show field strength. Field strength in dBm = -112 dBm+(2*X).  When X gets bigger, then the field strength is higher. -104 dBm is the lowest value for a voice  call. A data calls willfaulty because the noise is to high.
    'AT+CREG?[Enter]
    'Answer 0,x (X=2=log off, X=1=log in, X=0=don´t know) please  refer to manual for further infos
    'AT+COPS?[Enter]
    'Shows which GSM operators is active.
    '0,2,26201= T-Mobile  availiable
    'AT+COPS=?[Enter]
    'Shows all available networks

    '    Error       Description
    'CMS ERROR: 1    Unassigned number
    'CMS ERROR: 8    Operator determined barring
    'CMS ERROR: 10   Call bared
    'CMS ERROR: 21   Short message transfer rejected
    'CMS ERROR: 27   Destination out of service
    'CMS ERROR: 28   Unindentified subscriber
    'CMS ERROR: 29   Facility rejected
    'CMS ERROR: 30   Unknown subscriber
    'CMS ERROR: 38   Network out of order
    'CMS ERROR: 41   Temporary failure
    'CMS ERROR: 42   Congestion
    'CMS ERROR: 47   Recources unavailable
    'CMS ERROR: 50   Requested facility not subscribed
    'CMS ERROR: 69   Requested facility not implemented
    'CMS ERROR: 81   Invalid short message transfer reference value
    'CMS ERROR: 95   Invalid message unspecified
    'CMS ERROR: 96   Invalid mandatory information
    'CMS ERROR: 97   Message type non existent or not implemented
    'CMS ERROR: 98   Message not compatible with short message protocol
    'CMS ERROR: 99   Information element non-existent or not implemente
    'CMS ERROR: 111  Protocol error, unspecified
    'CMS ERROR: 127  Internetworking , unspecified
    'CMS ERROR: 128  Telematic internetworking not supported
    'CMS ERROR: 129  Short message type 0 not supported
    'CMS ERROR: 130  Cannot replace short message
    'CMS ERROR: 143  Unspecified TP-PID error
    'CMS ERROR: 144  Data code scheme not supported
    'CMS ERROR: 145  Message class not supported
    'CMS ERROR: 159  Unspecified TP-DCS error
    'CMS ERROR: 160  Command cannot be actioned
    'CMS ERROR: 161  Command unsupported
    'CMS ERROR: 175  Unspecified TP-Command error
    'CMS ERROR: 176  TPDU not supported
    'CMS ERROR: 192  SC busy
    'CMS ERROR: 193  No SC subscription
    'CMS ERROR: 194  SC System failure
    'CMS ERROR: 195  Invalid SME address
    'CMS ERROR: 196  Destination SME barred
    'CMS ERROR: 197  SM Rejected-Duplicate SM
    'CMS ERROR: 198  TP-VPF not supported
    'CMS ERROR: 199  TP-VP not supported
    'CMS ERROR: 208  D0 SIM SMS Storage full
    'CMS ERROR: 209  No SMS Storage capability in SIM
    'CMS ERROR: 210  Error in MS
    'CMS ERROR: 211  Memory capacity exceeded
    'CMS ERROR: 212  Sim application toolkit busy
    'CMS ERROR: 213  SIM data download error
    'CMS ERROR: 255  Unspecified error cause
    'CMS ERROR: 300  ME Failure
    'CMS ERROR: 301  SMS service of ME reserved
    'CMS ERROR: 302  Operation not allowed
    'CMS ERROR: 303  Operation not supported
    'CMS ERROR: 304  Invalid PDU mode parameter
    'CMS ERROR: 305  Invalid Text mode parameter
    'CMS ERROR: 310  SIM not inserted
    'CMS ERROR: 311  SIM PIN required
    'CMS ERROR: 312  PH-SIM PIN required
    'CMS ERROR: 313  SIM failure
    'CMS ERROR: 314  SIM busy
    'CMS ERROR: 315  SIM wrong
    'CMS ERROR: 316  SIM PUK required
    'CMS ERROR: 317  SIM PIN2 required
    'CMS ERROR: 318  SIM PUK2 required
    'CMS ERROR: 320  Memory failure
    'CMS ERROR: 321  Invalid memory index
    'CMS ERROR: 322  Memory full
    'CMS ERROR: 330  SMSC address unknown
    'CMS ERROR: 331  No network service
    'CMS ERROR: 332  Network timeout
    'CMS ERROR: 340  No +CNMA expected
    'CMS ERROR: 500  Unknown error
    'CMS ERROR: 512  User abort
    'CMS ERROR: 513  Unable to store
    'CMS ERROR: 514  Invalid Status
    'CMS ERROR: 515  Device busy or Invalid Character in string
    'CMS ERROR: 516  Invalid length
    'CMS ERROR: 517  Invalid character in PDU
    'CMS ERROR: 518  Invalid parameter
    'CMS ERROR: 519  Invalid length or character
    'CMS ERROR: 520  Invalid character in text
    'CMS ERROR: 521  Timer expired
    'CMS ERROR: 522  Operation temporary not allowed
    'CMS ERROR: 532  SIM not ready
    'CMS ERROR: 534  Cell Broadcast error unknown
    'CMS ERROR: 535  Protocol stack busy
    'CMS ERROR: 538  Invalid parameter
End Class

In your sms.aspx page use this

 Imports vinsms


Partial Class sms
    Inherits System.Web.UI.Page

    ' if error comes then kill the asp.net development server. may be accessing the com port





    Protected Sub btnSend_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSend.Click
        'Dim SMSEngine As New vinsms("COM3")
        'SMSEngine.Open()
        'lblStatus.Text = "starting
"
        'lblStatus.Text = SMSEngine.SendSMS(txtSMS.Text, "+919411103810")
        'SMSEngine.Close()
        Dim temp As String = sendSMS(txtCell.Text, txtSMS.Text, "COM3")
        lblStatus.Text = temp
        If temp.Contains("error") Then
            lblStatus.Text = lblStatus.Text + " Unable to send message to " + txtCell.Text
        Else
            lblStatus.Text = lblStatus.Text + " Message sent succesfully to " + txtCell.Text
        End If
    End Sub
End Class

And here is your sms.aspx page

              <%@ Page Language="VB" AutoEventWireup="false" CodeFile="sms.aspx.vb" Inherits="sms" %>

<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="asp" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>

    <form id="form1" runat="server">
    <div>
     <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
          
 </asp:ToolkitScriptManager>
    </div>
    <div>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
      
      
            <asp:TextBox ID="txtCell" runat="server">+919411103810</asp:TextBox>
            <br />
      
      
            <asp:TextBox ID="txtSMS" runat="server" Height="41px" Width="284px"></asp:TextBox>
            <asp:Button ID="btnSend" runat="server" Text="Send SMS" />
            <br />
            <asp:Label ID="lblStatus" runat="server" Text="l"></asp:Label>
      
      
        </ContentTemplate>
        </asp:UpdatePanel>
  
    </div>
    </form>
</body>
</html>


By Vinod Kotiya

Comments

ad said…
can i use those code in my way of sending message from web application to windows application.tq
Vinod Kotiya said…
Yes.. it the same code will work nice for win app...
monu said…
Thanks for the code sir, but i am trying to send more then 1 sms on single click of send button.Please try to resolve issue it will great help for me.
Vinod Kotiya said…
refer this http://vinodkotiya.blogspot.in/2010/08/how-to-send-bulk-or-group-sms-using-at.html
Honda Parts said…
First, sr about my English.
I wrote a little program, use GSM modem ( modem 3G in Vietnam ) send SMS.
And this is my code.
Imports System.IO
Imports System.IO.Ports
Imports System.Threading
Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Try
            Dim ports As String() = SerialPort.GetPortNames
            Dim port As String
            For Each port In ports
                ComboBox1.Items.Add(port)
            Next port
            ComboBox1.SelectedIndex = 0
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Try
            With SerialPort1
                .PortName = ComboBox1.Text
                .BaudRate = 115200
                .Parity = Parity.None
                .StopBits = StopBits.One
                .DataBits = 8
                .Handshake = Handshake.RequestToSend
                .DtrEnable = True
                .RtsEnable = True
                .NewLine = vbCrLf
                .Open()
            End With
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Try
            If SerialPort1.IsOpen Then
                With SerialPort1
                    .Write("AT" & vbCrLf)
                    .Write("AT+CMGF=1" & vbCrLf)
                    .Write("AT+CMGS=" & Chr(34) & TextBox1.Text & Chr(34) & vbCrLf)
                    .Write(RichTextBox1.Text & Chr(26))
                    MsgBox("Messge sent!")
                End With
            Else
                MsgBox("Error on the port selected")
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
End Class.

It work, but only send to 1 receiver.
So, please help, I want send to 30 - 50 receiver, and time delay between sms is 10seconds.
Thank you very much. 
Pinx.... said…
Here is your solution

Imports System.IO
Imports System.IO.Ports
Imports System.Threading
Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Try
            Dim ports As String() = SerialPort.GetPortNames
            Dim port As String
            For Each port In ports
                ComboBox1.Items.Add(port)
            Next port
            ComboBox1.SelectedIndex = 0
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Try

            With SerialPort1
                .PortName = ComboBox1.Text
                .BaudRate = 115200
                .Parity = Parity.None
                .StopBits = StopBits.One
                .DataBits = 8
                .Handshake = Handshake.RequestToSend
                .DtrEnable = True
                .RtsEnable = True
                .NewLine = vbCrLf
                .Open()
            End With
''' loop through all the numbers comma seperated in texbox1
dim listofpersons = textbox1.Text.Split(",")
for each number in listofpersons
Try
            If SerialPort1.IsOpen Then
                With SerialPort1
                    .Write("AT" & vbCrLf)
                    .Write("AT+CMGF=1" & vbCrLf)
                    .Write("AT+CMGS=" & Chr(34) & number & Chr(34) & vbCrLf)
                    .Write(RichTextBox1.Text & Chr(26))
                    MsgBox("Messge sent!")
                End With
            Else
                MsgBox("Error on the port selected")
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
next

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
       
    End Sub
End Class.
Honda Parts said…
Thank you very much Pinx
I have some trouble :

1. I have textbox1 (input number and comma )
I have label1
I want label1 show total comma in textbox1.

2. How do I import an excel colum to textbox1

Thank you.

Popular Posts