title: How do I use COMMTIMEOUTS to wait until bytes are available but read more than one byte?
body: <p>I have a C++ serial port class that has a none blocking and a blocking mode for read operations.  For blocking mode:</p>
  
  <pre><code>COMMTIMEOUTS cto;
  GetCommTimeouts(m_hFile,&amp;cto);
  // Set the new timeouts
  cto.ReadIntervalTimeout = 0;
  cto.ReadTotalTimeoutConstant = 0;
  cto.ReadTotalTimeoutMultiplier = 0;
  SetCommTimeouts(m_hFile,&amp;cto)
  </code></pre>
  
  <p>For non blocking mode:</p>
  
  <pre><code>COMMTIMEOUTS cto;
  GetCommTimeouts(m_hFile,&amp;cto);
  // Set the new timeouts
  cto.ReadIntervalTimeout = MAXDWORD;
  cto.ReadTotalTimeoutConstant = 0;
  cto.ReadTotalTimeoutMultiplier = 0;
  SetCommTimeouts(m_hFile,&amp;cto)
  </code></pre>
  
  <p>I would like to add another mode that waits for any number of bytes and read them.</p>
  
  <p>From MSDN <a href="http://msdn.microsoft.com/en-us/library/aa363190%28v=vs.85%29.aspx" rel="nofollow">COMMTIMEOUTS structure</a>:</p>
  
  <p>If an application sets <strong>ReadIntervalTimeout</strong> and <strong>ReadTotalTimeoutMultiplier</strong> to <strong>MAXDWORD</strong> and sets <strong>ReadTotalTimeoutConstant</strong> to a value greater than zero and less than <strong>MAXDWORD</strong>, one of the following occurs when the ReadFile function is called:</p>
  
  <ul>
  <li>If there are any bytes in the input buffer, ReadFile returns immediately with the bytes in the buffer.</li>
  <li>If there are no bytes in the input buffer, ReadFile waits until a
  byte arrives and then returns immediately.</li>
  <li>If no bytes arrive within the time specified by
  ReadTotalTimeoutConstant, ReadFile times out.</li>
  </ul>
  
  <p>This looks in code like this:</p>
  
  <pre><code>COMMTIMEOUTS cto;
  GetCommTimeouts(m_hFile,&amp;cto);
  // Set the new timeouts
  cto.ReadIntervalTimeout = 100;
  cto.ReadTotalTimeoutConstant = MAXDWORD;
  cto.ReadTotalTimeoutMultiplier = MAXDWORD;
  SetCommTimeouts(m_hFile,&amp;cto)
  </code></pre>
  
  <p>But this returns emidiately on the first byte.  This is a problem since I am reading the port in a loop and the handling of a byte is so fast that the next time I read the port, only another byte is available.  The end result is that I am reading one byte at a time in a loop and using 100% of the core running that thread.</p>
  
  <p>I would like to use the <code>cto.ReadIntervalTimeout</code> like in the MSDN documentation but still wait until at least one byte is available.  Does anyone have an idea?</p>
  
  <p>Thanks.</p>