title: Serial I/O Overlapped/Non-Overlapped with Windows/Windows CE
body: <p>I'm sorry this isn't much of a question, but more of to help people having problems with these particular things.  The problem I'm working on requires the use of Serial I/O, but is primarily running under Windows CE 6.0.  However, I was recently asked if the application could also be made to work under Windows too, so I set about solving this problem.  I did spend quite a lot of time looking around to see if anyone had the answers I was looking for and it all came across as a lot of misinformation and things that were just basically wrong in some instances.  So having solved this problem, I thought I'd share my findings with everyone so anyone encountering these difficulties would have answers.</p>
  
  <p>Under Windows CE, OVERLAPPED I/O is <em>NOT</em> supported.  This means that bi-directional communication through the serial port can be quite troublesome.  The main problem being that when you are waiting on data from the serial port, you cannot send data because doing so will cause your main thread to block until the read operation completes or timeouts (depending on whether you've set timeouts up)</p>
  
  <p>Like most people doing serial I/O, I had a reader serial thread set up for reading the serial port, which used WaitCommEvent() with an EV_RXCHAR mask to wait for serial data.  Now this is where the difficulty arises with Windows and Windows CE.</p>
  
  <p>If I have a simple reader thread like this, as an example:-</p>
  
  <pre><code>UINT SimpleReaderThread(LPVOID thParam)
  {
      DWORD eMask;
      WaitCommEvent(thParam, &amp;eMask, NULL);
      MessageBox(NULL, TEXT("Thread Exited"), TEXT("Hello"), MB_OK);
  }
  </code></pre>
  
  <p>Obviously in the above example, I'm not reading the data from the serial port or anything and I'm assuming that thParam contains the opened handle to the comm port etc.  Now, the problem is under Windows when your thread executes and hits the WaitCommEvent(), your reader thread will go to sleep waiting for serial port data.  Okay, that's fine and as it should be, but... how do you end this thread and get the MessageBox() to appear?  Well, as it turns out, it's not actually that easy and is a fundamental difference between Windows CE and Windows in the way it does its Serial I/O.</p>
  
  <p>Under Windows CE, you can do a couple of things to make the WaitCommEvent() fall through, such as SetCommMask(COMMPORT_HANDLE, 0) or even CloseHandle(COMMPORT_HANDLE).  This will allow you to properly terminate your thread and therefore release the serial port for you to start sending data again.  However neither of these things will work under Windows and both will cause the thread you call them from to sleep waiting on the completion of the WaitCommEvent().  So, how do you end the WaitCommEvent() under Windows?  Well, ordinarily you'd use OVERLAPPED I/O and the thread blocking wouldn't be an issue, but since the solution has to be compatible with Windows CE as well, OVERLAPPED I/O isn't an option.  There is one thing you can do under Windows to end the WaitCommEvent() and that is to call the CancelSynchronousIo() function and this will end your WaitCommEvent(), but be aware this can be device dependent.  The main problem with CancelSynchronousIo() is that it isn't supported by Windows CE either, so you're out of luck using that for this problem!</p>
  
  <p>So how do you do it?  The fact is, to solve this problem, you simply can't use WaitCommEvent() as there is no way to terminate this function on Windows that is supported by Windows CE.  That then leaves you with ReadFile() which again will block whilst it is reading NON OVERLAPPED I/O and this <em>WILL</em> work with Comm Timeouts.</p>
  
  <p>Using ReadFile() and a COMMTIMEOUTS structure does mean that you will have to have a tight loop waiting for your serial data, but if you're not receiving large amount of serial data, it shouldn't be a problem.  Also an event for ending your loop with a small timeout will also ensure that resources are passed back to the system and you're not hammering the processor at 100% load.  Below is the solution I came up with and would appreciate some feedback, if you think it could be improved.</p>
  
  <pre><code>typedef struct
  {
      UINT8 sync;
      UINT8 op
      UINT8 dev;
      UINT8 node;
      UINT8 data;
      UINT8 csum;
  } COMMDAT;
  
  COMSTAT cs = {0};
  DWORD byte_count;
  COMMDAT cd;
  
  ZeroMemory(&amp;cd, sizeof(COMMDAT));
  bool recv = false;
  do
  {
      ClearCommError(comm_handle, 0, &amp;cs);
      if (cs.cbInQue == sizeof(COMMDAT))
      {
          ReadFile(comm_handle, &amp;cd, sizeof(COMMDAT), &amp;byte_count, NULL);
          recv = true;
      }
  } while ((WaitForSingleObject(event_handle, 2) != WAIT_OBJECT_0) &amp;&amp; !recv);
  ThreadExit(recv ? cd.data : 0xFF);
  </code></pre>
  
  <p>So to end the thread you just signal the event in the event_handle and that allow you to exit the thread properly and clean up resources and works correctly on Windows and Windows CE.</p>
  
  <p>Hope that helps everyone who I've seen has had difficulty with this problem.</p>