title: Using SerialPort class to read response from connected device
body: <p>I'm using C#'s <code>SerialPort</code> class to try and send a <code>AT</code> command to a device and get a response back. I've verified it works correctly in HyperTerminal, if I send a command of <code>AT</code> it responds back with <code>OK</code>. However, in my console app, if I send <code>AT</code>, it replies back with an echo <code>AT</code>. The code is below, any insight into what I'm doing wrong in my receiving code would be greatly appreciated:</p>
  <pre><code>ComPort.DataReceived += new SerialDataReceivedEventHandler(ComPort_DataReceived);
  
  public void Open()
          {
              Console.WriteLine();
              //close port if already open.
              if (ComPort.IsOpen)
              {
                  ComPort.Close();
              }
              //setup port.
              ComPort.PortName = ConfigurationManager.AppSettings[&quot;PortName&quot;].ToString();
              ComPort.BaudRate = Convert.ToInt32(ConfigurationManager.AppSettings[&quot;BaudRate&quot;]);
              ComPort.Parity = Parity.None;
              ComPort.StopBits = StopBits.One;
              ComPort.DataBits = 8;
              ComPort.DtrEnable = true;
              ComPort.RtsEnable = true;
              if (Convert.ToBoolean(ConfigurationManager.AppSettings[&quot;HWFlowControlEnabled&quot;]))
              {
                  ComPort.Handshake = Handshake.RequestToSend;
              }
              //open port.
              Console.WriteLine(&quot;Opening port &quot; + ComPort.PortName + &quot;...&quot;);
              ComPort.Open();
              Console.WriteLine(&quot;Opened port &quot; + ComPort.PortName);
          }
  
  void ComPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
          {
              string message = ComPort.ReadExisting();
              Console.WriteLine(&quot;RECEIVED: &quot; + message);
              if (message.IndexOf(&quot;OK&quot;) &gt; -1)
              {
                  ReceivedOK = true;
              }
          }
  </code></pre>