Hi
how can i use AT-Commands in C# ?
i try with this code but it`s not work !
can any body help to me ?
thanks

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 Sending_SMS
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SerialPort sp = new SerialPort();
            sp.PortName = "COM42";
            sp.Open();
            sp.Write("ATD09364191752");
        }
    }
}

You need to keep the serial port at the form level, otherwise the port is closed and disposed at the end of the button1_Click method.
You also need to capture the incoming data (usually an "OK" from a Modem.)
The following (untested) code should help.

namespace Sending_SMS
{
    public partial class Form1 : Form
    {
        SerialPort sp = null; //<---- serial port at form level

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // initialise port in form load
            sp = new SerialPort();
            sp.PortName = "COM42";  
            // also set up baud rate etc here.

            // attach event handler to capture data received 
            sp.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived);
        }

        void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            // do something with the data here.
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // use error handling to catch any exceptions
            try 
	    {	        
                // open port if not already open
                // Note: exception occurs if Open when already open.
                if (!sp.IsOpen)
                {
                    sp.Open();
                }
                // send data to port
                sp.Write("ATD09364191752");
	    }
	    catch (Exception ex)
	    {
                // report exception to user
                MessageBox.Show(string.Format("Exception : {0}", ex.Message), "Port Error");
            }
        }

    }
}

Any questions just ask.

Edited 6 Years Ago by nick.crane: n/a

Comments
Correct!

Hello,

You should add a carriage return character "<CR>" to the end of the AT command.

I developed a state-driven serial port programming language in C# and it really makes it easy to communicate with the serial port. It is freely available on sourceforge.

For example; if you want to dial a phone number as in your example.

state Init
  send("ATD09364191752<CR>");
end state

state Idle
  recv();
end state

Please visit the project forum for more information about how to send SMS over phone modem

Screen shots
Project homepage

Thank you,

Orhan

Edited 6 Years Ago by albay: n/a

Comments
Absolutely needed .

Can anyone send me code for receiving sms from pc connected to mobile or read sms from mobile's inbox to PC application.
It is really important please reply as soon as possible. SNIP

Edited 4 Years Ago by happygeek: email deleted, keep answers on DaniWeb please

Comments
No...
This article has been dead for over six months. Start a new discussion instead.