AT commands and modems using serial port

Feb 12 2010 8:43 AM
Hi, I wrote some code for two modems: one, must send some string and another must answer the call and receive the string. My receiver modem is a D-Link voice-modem which has COM port, and the sender modem is a NetForce usb2.0 data-modem. Two modems are connected to two different computers and each one has a phone line. I first run the receiver's code and then the sender's code. Here's the code for each of them and log files:

The receiver modem's code:

<pre>
        SerialPort sp = new SerialPort("COM1");

        private void Form1_Load(object sender, EventArgs e)
        {
            sp.NewLine = "\r\n";
            sp.Parity = Parity.None;
            sp.DataBits = 8;
            sp.StopBits = StopBits.One;
            sp.DtrEnable = true;
            sp.WriteBufferSize = 1024;

            sp.Open();
            sp.WriteLine("ATE0");            
            sp.BaseStream.Flush();            
            sp.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(sp_DataReceived);
        }

        void sp_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            string str;           
            str = sp.ReadLine();
            if (str == "RING")
            {
                sp.WriteLine("ATA");
                sp.BaseStream.Flush();
            }
            System.IO.File.AppendAllText("c:\\log_receiver.txt", str + "\r\n");
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            sp.Close();
        }
</pre>

and its log file(log_receiver.txt):

OK

RING

CONNECT 9600

And sender's code:

<pre>
        SerialPort sp = new SerialPort("COM8");

        private void Form1_Load(object sender, EventArgs e)
        {
            sp.NewLine = "\r\n";
            sp.Parity = Parity.None;
            sp.DataBits = 8;
            sp.StopBits = StopBits.One;
            sp.DtrEnable = true;
            sp.WriteBufferSize = 1024;

            sp.Open();
            sp.WriteLine("ATE0");
            sp.WriteLine("ATDT6632");      
            sp.BaseStream.Flush();
            sp.WriteLine("My string to be sent");
            sp.BaseStream.Flush();
            sp.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(sp_DataReceived);
        }

        void sp_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            string str;            
            str = sp.ReadLine();
            File.AppendAllText("C:\\log_sender.txt", str + "\r\n");
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            sp.Close();
        }
</pre>

and the sender's log file(log_sender.txt);
ATE0
OK

NO CARRIER

I have read all the commands of the Hayes AT commands, but there were no tutorial about how to use the commands(I know that using commands may differ in some modems). I wanted to know that where's the problem of my code and does anyone have an idea about using AT commands in order to send and receive data between modems?

Answers (2)