title: WaitCommEvent WINAPI Serial Port Event
body: <p>I have been refering to the following page <a href="https://msdn.microsoft.com/en-us/library/ff802693.aspx" rel="nofollow noreferrer">https://msdn.microsoft.com/en-us/library/ff802693.aspx</a> to create event driven serial port. In my initialisation I set the following:</p>
  
  <pre><code>Status = GetCommState(hComm, &amp;dcbSerialParams); 
  Status = SetCommState(hComm, &amp;dcbSerialParams); 
  Status = SetCommMask(hComm, EV_RXCHAR);
  </code></pre>
  
  <p>In my WinMain I then use WaitCommEvent to listen for the data to be recieved on the port. It looks like the following:</p>
  
  <pre><code>while(1){
      if( PeekMessage(&amp;msg, NULL, 0, 0, PM_REMOVE)) {
          DispatchMessage(&amp;msg);
      }
      else{
          //SetWindowText(hStatic, "");
          Status = WaitCommEvent(hComm, &amp;dwEventMask, NULL); //Wait for the character to be received
          if (Status == TRUE){
              ReadFromSerialPort();
          }
      }
    }
  </code></pre>
  
  <p>This works fine, all the data is being read exactly as it is coming in and I can press a write to port button and the data is written and other stuff so no real complaints. However the code is basically glorified polling, constantlty checking the status of WaitCommEvent and reading the port when true. I would rather not poll at all and just sit there until an event happens, like an Interrupt Service Routing used in microcontrollers. Is this possible using win32?</p>
  
  <p>Thanks.</p>