A button, when clicked, sends an sms to the number entered in NumTxt textbox, and sends the text entered in SMSTxt textbox. Port name entered in texbox ComPort Here's the event handler of the button click event.
using System.IO.Ports;
private void button1_Click(object sender, EventArgs e)
{
try
{
int mSpeed = 1;
serialport.PortName = ComPort.Text;
serialport.BaudRate = 96000;
serialport.Parity = Parity.None;
serialport.DataBits = 8;
serialport.StopBits = StopBits.One;
serialport.Handshake = Handshake.XOnXOff;
serialport.DtrEnable = true;
serialport.RtsEnable = true;
serialport.NewLine = Environment.NewLine;
Console.WriteLine("1a");
try
{
serialport.Open();
}
catch (Exception)
{
MessageBox.Show("Try another Port." +
Environment.NewLine + "Phone not detected or The requested resource is in
use.", "CONNECTION ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
Console.WriteLine("2a");
serialport.WriteLine("AT+CMGF=1" + Environment.NewLine);
System.Threading.Thread.Sleep(200);
serialport.WriteLine("AT+CSCS=GSM" + Environment.NewLine);
System.Threading.Thread.Sleep(200);
serialport.WriteLine("AT+CMGS=" + (char)34 + NumTxt.Text
+ (char)34 + Environment.NewLine);
System.Threading.Thread.Sleep(200);
serialport.WriteLine(SMSTxt.Text + (char)26);
System.Threading.Thread.Sleep(mSpeed);
serialport.Close();
}
catch (Exception)
{
if (serialport.IsOpen)
serialport.Close();
MessageBox.Show("Couldn't send the SMS.", "CONNECTION ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
I was able to send the sms using this very code yesterday, but i do not know why it doesn't work any more.. no exceptions thrown. When I use the software that comes with gsm modem, I am able to send sms. But not through C# code. If anyone can pin point the mistake in the above code, I will be very grateful.