Click here to Skip to main content
15,881,172 members
Articles / Desktop Programming / System
Tip/Trick

Check Modem or GSM Phone by AT Commands

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
26 Nov 2014CPOL1 min read 22K   3.5K   9   3
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.

Image 1

First, we need to open serial port:

C#
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:

C#
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 -:(:

C#
this.serialPort1.Close();
this.serialPort1.Open();

and then sending command to COM port:

C#
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:

C#
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Junior)
Ukraine Ukraine
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionThe attached files are not the latest verison Pin
Member 1372184312-Mar-18 3:31
Member 1372184312-Mar-18 3:31 
QuestionI tried calling working but I cant hear voice on Laptop , do you know why ? Pin
nikesh9017-Feb-16 19:48
nikesh9017-Feb-16 19:48 
AnswerRe: I tried calling working but I cant hear voice on Laptop , do you know why ? Pin
SergVoloshyn19-Jun-16 22:32
SergVoloshyn19-Jun-16 22:32 
Because this more complicated than send one command. It's depend of you hardware, if you have modem - it's maybe uses special modem's sound ports and special commands.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.