title: Only one byte is read when reading just now opened SerialPort
body: <p>Strange issue.</p>
  
  <p>When I read from com-port with <a href="http://msdn.microsoft.com/en-us/library/ms143549.aspx" rel="nofollow"><code>SerialPort.Read()</code></a>, then if data arrive, <strong>only one byte is read on the first call</strong>, disregards of <code>count</code> parameter and number of bytes available within timeout. All further readings are ok, only very first has problem</p>
  
  <p>Using <a href="http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.discardinbuffer.aspx" rel="nofollow"><code>SerialPort.DiscardInBuffer()</code></a> or close/open (reopen) com-port will <strong>again</strong> cause the problem of the first reading.</p>
  
  <p>Here is some code:</p>
  
  <pre><code>var port = new SerialPort();
  port.PortName = "com2";
  port.BaudRate = 9600;
  port.WriteTimeout = 1000;
  port.ReadTimeout = 1000;
  port.Open();
  
  // ... send some data first
  
  var read = new byte[10];
  if (port.Read(read, 0, read.Length) != read.Length)
  {
      // always here at first reading no matter what
  }
  
  // ... send some data again
  
  if (port.Read(read, 0, read.Length) != read.Length)
  {
      // not here anymore (unless error)
  }
  </code></pre>
  
  <p>Issue doesn't appears if specified amount of data is already available to read when <code>Read()</code> is called.</p>
  
  <hr>
  
  <p>Some explanation.</p>
  
  <p><code>Read()</code> is synchronous reading. It will return when specified amount of data is received or timeout is expired. As it seems, <code>TimeoutException</code> will be only thrown if zero bytes are received. If timeout is expired before requested amount of data is read, function will return number of bytes read. Thus, return value has to be compared to requested to see if reading was ok.</p>
  
  <p>But very first call has a problem:</p>
  
  <blockquote>
    <p>If 0 bytes has arrived during timeout, then first call correctly waits for timeout and trigger TimeoutException. If at least 1 byte has arrived, then function immediately ends, ignoring timeout and requested number of bytes to read altogether.</p>
  </blockquote>
  
  <p>This is how I see it. Am I wrong? What should I do?</p>
  
  <hr>
  
  <p>Some more tests. Inserting <code>Thread.Sleep()</code> to ensure that when <code>Read()</code> is called, there will be all data available, will make problem to disappears.</p>
  
  <p>Logically, let's add sleep for only first call. Yay. What???? The problem now appears with <strong>second call</strong> and <strong>only second call</strong>. In other word problem appears with the <strong>first Read() what will not have all data available but only once</strong>.</p>