title: Not reading complete Serial Port data in C#
body: <p>I'm using .Net framework 4 and create a serial port GUI application in C#. In some cases, the SerialPort object (port) doesn't receive all the data (I compare it with a Mixed Signal Oscilloscope)</p>
  
  <p>For example, if the connected device sends:</p>
  
  <p>0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09</p>
  
  <p>I can receive:</p>
  
  <p>0x00 0x01 0x02 0x03 <strong>0x04 0x08</strong> 0x09 </p>
  
  <p>I tried different codes having the same issue:</p>
  
  <ul>
  <li><p>Use the data received event to fill a buffer:</p>
  
  <pre><code>    private void dataReceived(object sender, SerialDataReceivedEventArgs e) {
          while (port.BytesToRead &gt; 0){
              byte[] newBytes = new byte[port.BytesToRead];
              int LengthRead = port.Read(newBytes, 0, newBytes.Length);
              Array.Resize(ref newBytes, LengthRead);
              System.Buffer.BlockCopy(newBytes, 0, buffer, positionInBuffer, newBytes.Length);
              positionInBuffer += newBytes.Length;
          }
      }
  </code></pre></li>
  <li><p>Looping for an expected number of bytes, in this case causing the TimeoutException:</p>
  
  <pre><code>        while (port.BytesToRead &lt; expectedSize)
      {
          System.Threading.Thread.Sleep(10);
          waitingLoop++;
          if (waitingLoop &gt; TimeOut)             // wait for a 1s timeout
              throw new TimeoutException();
      }
  
      newBytes = new byte[expectedSize];
      LengthRead = port.Read(newBytes, 0, newBytes.Length);
      if (LengthRead != newBytes.Length)
          throw new TimeoutException(); // or any exception, doesn't matter...
  </code></pre></li>
  </ul>
  
  <p>I tried to change the ReadBufferSize size, the timeout info,... but nothing works.
  Any ideas?</p>