Send SMS via C# Code GSM Module

2018-01-03 16_55_12-Form1

The following is the code that you need to send SMS through C# code. Note, this code goes in to Send button click function handler.

 

private void btnSend_Click(object sender, EventArgs e)
{
try
{
SerialPort sp = new SerialPort();
sp.PortName = txtPort.Text;
sp.Open();
sp.WriteLine(“AT” + Environment.NewLine);
Thread.Sleep(100);
sp.WriteLine(“AT+CMFG=1” + Environment.NewLine);
Thread.Sleep(100);
sp.WriteLine(“AT+CSCS=\”GSM\”” + Environment.NewLine);
Thread.Sleep(100);
sp.WriteLine(“AT+CMGS=\”” + txtPhone.Text + “\”” + Environment.NewLine);
Thread.Sleep(100);
sp.WriteLine(txtMessage.Text + Environment.NewLine);
Thread.Sleep(100);
sp.Write(new byte[] { 26 }, 0, 1);
Thread.Sleep(100);
var response = sp.ReadExisting();

if (response.Contains(“ERROR”))
MessageBox.Show(“SMS Failed”);
else
MessageBox.Show(“SMS Sent successfully”);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

 

When I test this code, it does return successfully weather GSM Modem is connected or not so there is definitely something not right with it.

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s