This program uses the text mode for sending the message, which is good for starting, but it may not be supported by every phone. If you want to use the PDU mode instead of the text mode - which is supported by all mobiles but is more complicated - see the
PDU encoding sample for creating the required data.
Sample source code is available for VB6.
Details about the VB6 sample:
- Shows how to use the MSComm ActiveX control
- Shows how to use the SMS AT commands
- Includes a trace output to see what's going on
- Includes error handling for common errors
- Is not optimized for performance
Download |
 | VB6: SendSMS_AT_VB6_20041025.zip |
Encoding SMS messages for PDU modeThis demonstrates how to create an SMS-SUBMIT PDU for a simple text message.
Note: This program does only the coding, it does not actually send the message. For testing you can use a terminal program like HyperTerminal (included in Windows) to transfer the encoded message, as given in the following sample:
AT+CMGF=0<CR>
AT+CMGS=<actual PDU length><CR>
<encoded message><EOF> |
<CR> = ASCII 13 = ENTER
<EOF> = ASCII 26 = CTRL+Z | | | | | | | | |
Using AT commands to read SMS messages
Using AT commands you're not restricted to a specific phone model, as long as the phone supports the commands you are using.
This program uses the text mode for reading the messages, which is good for learning how it works, but it may not be supported by every phone. You may have to change character sets depending on the characters used in the messages to get the original text back.
Basically there are two ways to send SMS.
- Connect a mobile phone or GSM/GPRS modem to a computer / PC. Then use the computer / PC and AT commands to instruct the mobile phone or GSM/GPRS modem to send SMS messages.
- Connect the computer / PC 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.
In this tutorial we'll use first method. We can send SmS through AT commands, AT commands are
instructions to Send/Receive SMS. (for more detail about AT Commands wait for my Next Post about AT commands).
Lets Start..
Step-1
Open
Visual Studio, Create a New Project with the Name
SMS. (In this tutorial I'm not going to show you how to create a new project, I'm skipping the detail about it.)
Step-2
In the solution Explorer, Right Click on the Project and Click on Add -> Add New Item and Add New
Class with the name
SmsClass.cs copy the following code in the class.
SmsClass.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.IO.Ports;
using System.Windows.Forms;
namespace SMS
{
class SmsClass
{
SerialPort serialPort;
public SmsClass(string comPort)
{
this.serialPort = new SerialPort();
this.serialPort.PortName = comPort;
this.serialPort.BaudRate = 9600;
this.serialPort.Parity = Parity.None;
this.serialPort.DataBits = 8;
this.serialPort.StopBits = StopBits.One;
this.serialPort.Handshake = Handshake.RequestToSend;
this.serialPort.DtrEnable = true;
this.serialPort.RtsEnable = true;
this.serialPort.NewLine = System.Environment.NewLine;
}
public bool sendSms(string cellNo, string sms)
{
string messages = null;
messages = sms;
if (this.serialPort.IsOpen == true)
{
try
{
this.serialPort.WriteLine("AT" + (char)(13));
Thread.Sleep(4);
this.serialPort.WriteLine("AT+CMGF=1" + (char)(13));
Thread.Sleep(5);
this.serialPort.WriteLine("AT+CMGS=\"" + cellNo + "\"");
Thread.Sleep(10);
this.serialPort.WriteLine(">" + messages + (char)(26));
}
catch (Exception ex)
{
MessageBox.Show(ex.Source);
}
return true;
}
else
return false;
}
public void Opens()
{
if (this.serialPort.IsOpen == false)
{
this.serialPort.Open();
}
}
public void Closes()
{
if (this.serialPort.IsOpen == true)
{
this.serialPort.Close();
}
}
}
}
Step-3
Now on the Form1, we'll use
- combo Box (cboPorts) which will show the available ports on the computer.
- Two TextBoxes One for Receivers Cell No. (txtphone) and other for Message body (txtMessage) with Multiline property set to True.
- Send Button (btnSend) to Send SMS.
Step-4
Copy and Paste the following code in the Form1 Code.
Form1
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
namespace SMS
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
loadPorts();
}
private void loadPorts()
{
string[] ports = SerialPort.GetPortNames();
foreach (string port in ports)
{
cboPorts.Items.Add(port);
}
}
private void btnSend_Click(object sender, EventArgs e)
{
SmsClass sm = new SmsClass(cboPorts.Text);
sm.Opens();
sm.sendSms(txtPhone.Text, txtMessage.Text);
sm.Closes();
MessageBox.Show("Message Sent!");
}
}
}
Now , You Just need to
connect your GSM modem/mobile to your PC through
USB or Serial cable.
and Run the Project. From the main Window Select COM Port on which your Modem/Phone is Connected to.
Note: This application is Tested using Nokia N82 By Me...
Download
Source Code:
http://www.multiupload.com/1E2JGHS1ZF