0

I am learning AT Command in c#.

public static void Main(string[] args)
    {

        String command = "AT";
        SerialPort serialPort = new SerialPort
                                    {
                                        PortName = "COM4",
                                        BaudRate = 9600,
                                        DataBits = 8,
                                        Parity = Parity.None,
                                        ReadTimeout = 300,
                                        WriteTimeout = 300,
                                        StopBits = StopBits.One,
                                        Handshake = Handshake.None
                                    };
        serialPort.Open();
        serialPort.WriteLine(command + "\r");
        String outPut = serialPort.ReadExisting();

        Console.WriteLine(outPut);


    }

I am sure about the PortName. What I am doing wrong in the program??

Thanks in advance :)

SM Farhad Ali
  • 1,063
  • 2
  • 12
  • 28
  • Check this out: http://stackoverflow.com/questions/636758/serial-port-readline-vs-readexisting-or-how-to-read-the-data-from-serial-port-pr – Chris Haas Mar 06 '13 at 19:43

1 Answers1

1

Hard to tell if it's your only problem but SerialPort.ReadExisting() only reads the data that is immediately available (ie in the stream and buffer).

Your program writes data to the modem, and calls ReadExisting() right away. ReadExisting will return immediately with no data available, since the modem has had no time to respond.

Joachim Isaksson
  • 176,943
  • 25
  • 281
  • 294
  • I have modified my code like below. `SerialPort serialPort = new SerialPort {//previus properties RtsEnable = true, DtrEnable = true }; serialPort.Open(); Thread.Sleep(100); serialPort.WriteLine("AT" + "\r"); Thread.Sleep(100); String outPut = serialPort.ReadLine(); Thread.Sleep(100); Console.WriteLine(outPut); serialPort.Close();` – SM Farhad Ali Mar 09 '13 at 19:46
  • First time the programs output is ok when I plugin the modem in the PC but after that it shows **The operation has timed out** Any idea?? What I am doing wrong +Joachim Isaksson – SM Farhad Ali Mar 09 '13 at 19:52