Click here to Skip to main content
15,902,198 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.IO.Ports;
namespace call_app
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                SerialPort sp = new SerialPort();
                sp.PortName = "COM3";
                sp.BaudRate = 9600;
                sp.Parity = Parity.None;
                sp.DataBits = 8;
                sp.StopBits = StopBits.One;
                sp.Handshake = Handshake.XOnXOff;
                sp.DtrEnable = true;
                sp.RtsEnable = true;
                sp.Open();

                if (!sp.IsOpen)
                {
                    MessageBox.Show("Serial port is not opened");
                    return;
                }

                sp.WriteLine("AT" + Environment.NewLine);
                sp.WriteLine("ATD=\"" + "7600684319;" + "\"" + Environment.NewLine);

            }
            catch(Exception errcatch)
            {
                MessageBox.Show(errcatch.Message);
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}
Posted
Comments
OriginalGriff 24-Apr-14 12:12pm    
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind.
Use the "Improve question" widget to edit your question and provide better information.
gggustafson 22-May-14 14:29pm    
Are you going to rate my solution?

1 solution


After reviewing your code, here are a few things that I think you might want to consider:



  1. Declare the serial port at the class level.
  2. Create, initialize, and open the serial port in the form load handler.
  3. Set the serial port properties based upon the modem manufacturer's recommendations, not on code copied from some other source.
  4. After writing to the serial port, wait the amount of time recommended by the modem manufacturer.
  5. Declare a handler for the DataReceived event. Unless you specifically inhibit modem success messages, you will be able to see your application's interactions with the modem.
  6. Declare a handler for the ErrorReceived event. Here you will be able to determine what went wrong.


Keeping those items in mind, here is a rework of your code:


C#
using System;
using System.IO.Ports;
using System.Threading;
using System.Windows.Forms;

namespace CallApp
    {

    // *************************************************** class Form1

    public partial class Form1 : Form
        {
                                        // declare at class level
        SerialPort serial_port = null;

        // ********************************************** OnFormClosed

        // insure that serial port is closed and disposed at the end 
        // of the application

        protected override void OnFormClosed ( FormClosedEventArgs e )
            {

            base.OnFormClosed ( e );

            if ( serial_port != null )
                {
                if ( serial_port.IsOpen )
                    {
                    serial_port.Close ( );
                    }
                serial_port.Dispose ( );
                serial_port = null;
                }
            }

        // ************************************************ Form1_Load

        // create, initialize, and open the serial port in the forms
        // load handler

        private void Form1_Load ( object sender, 
                                  EventArgs e )
            {

            serial_port = new SerialPort ( );
            serial_port.DataReceived += 
                new SerialDataReceivedEventHandler ( 
                    serial_port_DataReceived );
            serial_port.ErrorReceived += 
                new SerialErrorReceivedEventHandler ( 
                    serial_port_ErrorReceived );

            serial_port.PortName = "COM3";
                                        // you must set the following 
                                        // values based upon modem 
                                        // manufacturer recommendation

                                        // note that some may not be 
                                        // needed
            serial_port.BaudRate = 9600;
            serial_port.Parity = Parity.None;
            serial_port.DataBits = 8;
            serial_port.StopBits = StopBits.One;
            serial_port.Handshake = Handshake.XOnXOff;
            serial_port.DtrEnable = true;
            serial_port.RtsEnable = true;
            serial_port.NewLine = Environment.NewLine;
                                        // open the serial port here
            try 
                {
                serial_port.Open ( );
                }
            catch ( Exception ex )
                {
                throw new ApplicationException ( 
                    "Failed to open serial port" +
                    ex.Message + ex.StackTrace );
                }
            }

        // ***************************************************** Form1

        public Form1 ( )
            {

            InitializeComponent ( );
            }

        // ********************************************* button1_Click

        // handle the button1 Click event

        private void button1_Click ( object    sender, 
                                     EventArgs e )
            {

            try
                {
                if ( !serial_port.IsOpen )
                    {
                    MessageBox.Show ( "Serial port is not opened",
                                      "Serial Port Error" );
                    return;
                    }

                serial_port.WriteLine ( "AT" );
                Thread.Sleep ( 1000 );

                serial_port.WriteLine ( "ATD=\"" + 
                                        "7600684319;" + 
                                        "\"" );
                Thread.Sleep ( 1000 );
                }
            catch ( Exception ex )
                {
                MessageBox.Show ( ex.Message + ex.StackTrace );
                }
            }

        // ********************************** serial_port_DataReceived

        // handle the DataReceived event

        private void serial_port_DataReceived (
                                object                      sender,
                                SerialDataReceivedEventArgs e )
            {
            string      received_string = String.Empty;
            SerialPort  serial_port = ( SerialPort ) sender;

            received_string = serial_port.ReadExisting ( );
            MessageBox.Show ( received_string, 
                              "Recieved" );
            }

        // ********************************* serial_port_ErrorReceived

        // handle serial port errors here

        void serial_port_ErrorReceived ( 
                                    object sender, 
                                    SerialErrorReceivedEventArgs e )
            {
            SerialPort  serial_port = ( SerialPort ) sender;

            MessageBox.Show ( "Serial Port error",
                              "Error" );
            }

        } // class Form1

    } // namespace call_app


Even without a modem on COM3, the following log is displayed:


en-us
AT
OK
ATD="7600684319;"
NO DIALTONE


Hope that helps.

 
Share this answer
 

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