Saturday, September 15, 2012

SMS Sending Using AT commands

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.

AT commands with a GSM/GPRS MODEM or mobile phone can be used to access following information and services:
  
1.      SMS services.
2.      MMS services.
3.      Data and Voice link over mobile network
4.      Information and configuration pertaining to mobile device or MODEM and SIM card.
5.      Fax services.

Below is a sample vb.net code using AT commands in sending SMS:

1. Create a form and name it frmsms.
















2. Add 2 textbox then name it txtnumber and txtmsg.

3. Add button and name it btnsend.

4. Doubleclick the frmsms form then paste the code below:

SerialPort1.PortName = "COM16"             'set gsm modem port
SerialPort1.BaudRate = 9600                     ' set gsm modem BaudRate
SerialPort1.Parity = Parity.None
SerialPort1.StopBits = StopBits.One
SerialPort1.DataBits = 8
SerialPort1.Handshake = Handshake.RequestToSend
SerialPort1.DtrEnable = True
SerialPort1.RtsEnable = True
SerialPort1.NewLine = vbCrLf
SerialPort1.Open()

Then find the Public Class frmsms line then paste this code above it:

Imports System.IO.Ports


5. Double click the btnsend then paste the code below:


 Dim message As String
 message = txtmsg.Text

If SerialPort1.IsOpen() Then
  SerialPort1.Write("AT" & vbCrLf)
  SerialPort1.Write("AT+CMGF=1" & vbCrLf)
  SerialPort1.Write("AT+CMGS=" & Chr(34) & txtnumber.Text & Chr(34) & vbCrLf)
  SerialPort1.Write(message & Chr(26))
  MsgBox("Sent")
Else
  MsgBox("Port not available")
End If


Here is the full code:

Imports System.IO.Ports

Public Class frmsms

    Dim SerialPort1 As New System.IO.Ports.SerialPort()

    Private Sub frmsms_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        SerialPort1.PortName = "COM16"             'set gsm modem port
        SerialPort1.BaudRate = 9600                     ' set gsm modem BaudRate
        SerialPort1.Parity = Parity.None
        SerialPort1.StopBits = StopBits.One
        SerialPort1.DataBits = 8
        SerialPort1.Handshake = Handshake.RequestToSend
        SerialPort1.DtrEnable = True
        SerialPort1.RtsEnable = True
        SerialPort1.NewLine = vbCrLf
        SerialPort1.Open()

    End Sub

    Private Sub btnsend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsend.Click

        Dim message As String
        message = txtmsg.Text

        If SerialPort1.IsOpen() Then
            SerialPort1.Write("AT" & vbCrLf)
            SerialPort1.Write("AT+CMGF=1" & vbCrLf)
            SerialPort1.Write("AT+CMGS=" & Chr(34) & txtnumber.Text & Chr(34) & vbCrLf)
            SerialPort1.Write(message & Chr(26))
            MsgBox("Sent")
        Else
            MsgBox("Port not available")
        End If

    End Sub

If you encountered error or you can't send sms, make sure that you have a device like GSM/GPRS MODEM or mobile phone connected to your pc. Also make sure that your PortName and BaudRate  is correct.

No comments:

Post a Comment