title: What is the correct way to read a serial port using .NET framework?
body: <p>I've read a lot of questions here about how to read data from serial ports using the .NET SerialPort class but none of the recommended approaches have proven completely efficient for me.</p>
  <p>Here is the code I am using for now:</p>
  <pre><code>SerialPort port = new SerialPort(&quot;COM1&quot;);
  port.DataReceived += new SerialDataReceivedEventHandler(MyDataReceivedHandler);
  </code></pre>
  <p>And the event handler:</p>
  <pre><code>void MyDataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
  {
      int count = port.BytesToRead;
      byte[] ByteArray = new byte[count];
      port.Read(ByteArray, 0, count);
  }
  </code></pre>
  <p>But I am still sometimes missing data. I've tried different way of reading the data in the event handler but with no luck.</p>
  <p>As the .NET 4.5 brings new possibilities to do some asynchronous tasks, like with the <a href="http://msdn.microsoft.com/en-us/library/hh137813.aspx" rel="nofollow noreferrer">ReadAsync</a> method that seems to be useable on a SerialPort stream, I'm curious to see what would be the recommended approach to handle those cases.</p>