title: Win32, Serial Port Read/Write and OVERLAPPED
body: <p>I'm trying to put together a proof of concept for reading/writing to a serial port and I am having problems with design/concept. I don't want to poll, so I'm trying to use Overlapped I/O. In this case, it's not a multi-threaded application - it just uses efficient waits.</p>
  
  <ol>
  <li>Open port (with OVERLAPPED)</li>
  <li>SetCommState (set port baud, parity, CTS/RTS, etc)</li>
  <li>SetCommMask (EV_BREAK, EV_CTS, EV_DSR,..., EV_RXCHAR, EV_RXFLAG, EV_TXEMPTY)</li>
  <li>Create OVERLAPPED structures and Events</li>
  <li>WriteFile (to port)</li>
  <li>WaitCommEvent (due to write)</li>
  <li>WaitCommEvent (due to read)</li>
  <li>ReadFile (from port)</li>
  <li>Check read buffer</li>
  </ol>
  
  <p>The MSDN example is kind of lame (<a href="http://msdn.microsoft.com/en-us/library/windows/desktop/aa363424%28v=vs.85%29.aspx" rel="noreferrer">Monitoring Communications Events</a>). I'm currently using two events, but the <code>ReadFile</code> step is failing with <code>ERROR_INVALID_PARAMETER</code>.</p>
  
  <p>In the steps above, do I need one or two overlapped structures? If one <code>OVERLAPPED</code> structure, are the steps <code>WriteFile</code>, <code>WaitCommEvent</code> (for write), reset event, <code>WaitCommEvent</code> (for read), <code>ReadFile</code>?</p>