Click here to Skip to main content
14,977,497 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I am sending sms using AT commands through the following c# code. Can someone check this simple code and suggest me why it is not working.
I can send my sms from hyperterminal using
AT+CSCA="msg centre no";
AT+CMGS="mobile no"

This the code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;

namespace condemo
{
class Program
{
static void Main(string[] args)
{
SerialPort e = new SerialPort();
e.PortName = "COM4";
e.BaudRate = 460800;
e.Parity = Parity.None;
e.DataBits = 8;
e.StopBits = StopBits.One;

if (!(e.IsOpen))
{
try
{
e.Open();
}
catch (Exception m)
{
Console.WriteLine(m.Message);
}
}

e.Write("AT" + (char)13);

e.Write("AT+CPIN?" + (char)13);

e.Write("AT+CMGF=1"+ (char)13);

e.WriteLine("AT+CSCA=" + "9886005444" + (char)13);
e.WriteLine("AT+CMGS" + "9742899065" + (char)13);
e.WriteLine("message"; + (char)13);
e.Write(""+(char)26);
Console.Read();

}
}
}
The above code is not working. I am not getting any errors. Can someone check it out.
Posted
Updated 10-Apr-11 7:47am
v3
Comments
Per Söderlund 14-Apr-11 7:14am
   
Questions about sending sms from CSharp is posted alot, I just had to vote 5 because you are trying to make it work before asking.

Check your speed: 460800 seems too high. There is a tutorial here[^] that suggests 230400 or less. What did you use in Hyperterminal?
   
Comments
punyavathi 1-Feb-12 3:36am
   
using System;
using System.Threading;
using System.ComponentModel;
using System.IO.Ports;


public class SMSCOMMS
{
private SerialPort SMSPort;
private Thread SMSThread;
private Thread ReadThread;
public static bool _Continue = false;
public static bool _ContSMS = false;
private bool _Wait = false;
public static bool _ReadPort = false;
public delegate void SendingEventHandler(bool Done);
public event SendingEventHandler Sending;
public delegate void DataReceivedEventHandler(string Message);
public event DataReceivedEventHandler DataReceived;

public SMSCOMMS(ref string COMMPORT)
{
SMSPort = new SerialPort();
SMSPort.PortName = COMMPORT;
SMSPort.BaudRate = 9600;
SMSPort.Parity = Parity.None;
SMSPort.DataBits = 8;
SMSPort.StopBits = StopBits.One;
SMSPort.Handshake = Handshake.RequestToSend;
SMSPort.DtrEnable = true;
SMSPort.RtsEnable = true;
SMSPort.NewLine = System.Environment.NewLine;
ReadThread = new Thread(
new System.Threading.ThreadStart(ReadPort));
}

public bool SendSMS(string CellNumber, string SMSMessage)
{
string MyMessage = null;
//Check if Message Length <= 160
if (SMSMessage.Length <= 160)
MyMessage = SMSMessage;
else
MyMessage = SMSMessage.Substring(0, 160);

SMSPort.WriteLine("AT+CMGS=" + CellNumber + "r");
_ContSMS = false;
SMSPort.WriteLine(
MyMessage + System.Environment.NewLine + (char)(26));
_Continue = false;
if (Sending != null)
Sending(false);

return false;
}

private void ReadPort()
{
string SerialIn = null;
byte[] RXBuffer = new byte[SMSPort.ReadBufferSize + 1];
string SMSMessage = null;
int Strpos = 0;
string TmpStr = null;
while (SMSPort.IsOpen == true)
{
if ((SMSPort.BytesToRead != 0) & (SMSPort.IsOpen == true))
{
while (SMSPort.BytesToRead != 0)
{
SMSPort.Read(RXBuffer, 0, SMSPort.ReadBufferSize);
SerialIn =
SerialIn + System.Text.Encoding.ASCII.GetString(
RXBuffer);
if (SerialIn.Contains(">") == true)
{
_ContSMS = true;
}
if (SerialIn.Contains("+CMGS:") == true)
{
_Continue = true;
if (Sending != null)
Sending(true);
_Wait = false;
SerialIn = string.Empty;
RXBuffer = new byte[SMSPort.ReadBufferSize + 1];
}
}
if (DataReceived != null)
DataReceived(SerialIn);
SerialIn = string.Empty;
RXBuffer = new byte[SMSPort.ReadBufferSize + 1];
}
}
}

public bool SendSMS(string CellNumber, string SMSMessage)
{
string MyMessage = null;
if (SMSMessage.Length <= 160)
{
MyMessage = SMSMessage;
}
else
{
MyMessage = SMSMessage.Substring(0, 160);
}

SMSPort.WriteLine("AT+CMGS=" + CellNumber + "r");
_ContSMS = false;
SMSPort.WriteLine(
MyMessage + System.Environment.NewLine + (char)(26));
_Continue = false;
Shambhoo kumar 8-Aug-13 5:24am
   
I m little bit confuse, can this code work without Internet connection or with internet connection.

Please Give me solution.
punyavathi 1-Feb-12 3:37am
   
if (Sending != null)
Sending(false);

return false;
}

public void Open()
{

SMSPort.Open();
ReadThread.Start();

}

public void Close()
{

SMSPort.Close();

}

}
punyavathi 1-Feb-12 3:38am
   
please find th esolution above code...i send sms through c# application using HUWEAI E173 HSPA USB Stick modeam How please given solution
This is how I did it.
e = new SerialPort("COM4", 9600, Parity.None, 8, StopBits.One);
e.Handshake = Handshake.RequestToSend;
e.WriteLine("AT+CMGF=1"+Environment.NewLine);
e.WriteLine("AT+CMGS" + "phone number here" + Environment.NewLine);
w.WriteLine("Message here"+(char)26+Environment.NewLine);


The code above might contain some minor errors because i just wrote it by hand.
If i have more numbers to send to I have to close the port and open for each time I send a text message.
I havent looked into why it happens yet.My application is still being developed.

I believe your problem could be the newline command, I tried different commands using "\n","\t\n" and so on.
Nothing worked until i used Environment.NewLine.

Hope this helps.
   
v2
Comments
prajnavantha 11-Apr-11 14:32pm
   
I tried the above method which u sent but it also did not mork.
I then read the output from the serialport using e.readline() after each time i write a command to com4 and printed it in console but it did not work well.
i get the following
AT
OK
AT+CPIN?
+CPIN READY

and nothing after that.
each time ehen i run i get different outpt on the console. sometimes i get
AT
OK
AT+CMGF=1

I don't know y. can u tell me how to read the response from the port...?
Per Söderlund 13-Apr-11 2:01am
   
You can use

e.DataReceived += new SerialDataReceivedEventHandler(SP_DataReceived);

//there is also a e.ErrorReceived that you can use.

Since you get Answer OK on AT I think you already are set on the datareceived event.

You can go into your device properties in windows as well, there you have a chance to make diagnostics to check if the device is working.Since you get it to work In hyperterminal i doubt the device is having errors.

My code was containing errors as well, should be

e.WriteLine("AT+CMGS=" + '"' + "phone number here"+ '"' + Environment.NewLine);
prajnavantha 15-Apr-11 13:14pm
   
e.WriteLine("AT"+ (char)13);
string t = e.ReadExisting();
Console.WriteLine(t);
Console.WriteLine("ended first");
e.WriteLine("AT+CPIN?"+Environment.NewLine);
t = e.ReadExisting(); Console.WriteLine(t); e.WriteLine("AT+CMGF=1" +Environment.NewLine) ;
t = e.ReadExisting(); Console.WriteLine(t); e.WriteLine("AT+CMGS="+"9742899065" + Environment.NewLine); e.WriteLine("msg" + (char)26 + Environment.NewLine);
t = e.ReadExisting();
Console.WriteLine(t);
This the progress i made.... when i run this i get the following output
AT
OK
AT+CPIN?
+CPIN:READY
ok

AT+CMGF=1
OK

AT+CMGS="9742899065"
ERROR
I am getting an error in the last command. can someone please check it out and guide me...
Per Söderlund 16-Apr-11 15:22pm
   
Change e.WriteLine("AT+CMGS="+"9742899065" + Environment.NewLine);
into e.WriteLine("AT+CMGS="+'"'+"9742899065"+'"'+ Environment.NewLine);
prajnavantha 17-Apr-11 7:43am
   
Thanks... it worked....:)
Per Söderlund 17-Apr-11 12:34pm
   
No problem.
jiojo 19-Apr-12 6:56am
   
hi could you please post the complete and working code for sending sms and i will be thankful please it is urgent

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900