title: Serial port not reading complete transmission
body: <p>I attempted to modify Teunis van Beelen's Rs232 library, from Polling to event driven and non-overlapped to suit my project. <a href="http://www.teuniz.net/RS-232/" rel="nofollow">RS232 Library</a></p>
  
  <p>I expect to receive blocks of data (roughly 100 to 200 chars)  every 200ms.</p>
  
  <p>Problem I am having is the received data is very inconsistent, it is cut off at random points, and incomplete.</p>
  
  <p>I would like ReadFile() to return only after reading one whole block of data.( or something to that effect)</p>
  
  <p>I feel like the problem is with the time out settings, because by altering the figures I get different results, but I just cant get it right, my best result so far has been set all time out values to 0 and let ReadFile() expect 150 bytes, this way ReadFile() dose not return unless it reads 150 chars, but this just go out of sync after few transmissions, as I have no idea how much data to expect.</p>
  
  <p>these are the main changes to the polling function in Teunis's code , besides time out settings, all other settings are unchanged:
  </p>
  
  <pre><code>//Using the EV_RXCHAR flag will notify the thread that a byte arrived at the port
   DWORD dwError  = 0;
  //use SetCommMask and WaitCommEvent to see if byte has arrived at the port
  //SetCommMask sets the desired events that cause a notification.
  if(!SetCommMask(Cport[comport_number],EV_RXCHAR)){
      printf("SetCommMask Error");
      dwError = GetLastError();
      // Error setting com mask
      return FALSE;
  }
  //WaitCommEvent function detects the occurrence of the events.
  DWORD dwCommEvent;
  for( ; ; )
  {
      //wait for event to happen
      if (WaitCommEvent(Cport[comport_number],&amp;dwCommEvent,NULL))
      {
          if(ReadFile(Cport[comport_number], buf, 1, (LPDWORD)((void *)&amp;n), NULL)){
              //Byte has been read, buf is processed in main
          }
          else{
              //error occoured in ReadFile call
              dwError = GetLastError();
              break;
              }
      else{
          //error in WaitCommEvent
          break;
      }
  
      break;  //break after read file
  }
  </code></pre>
  
  <p>attempt 2 as suggested by <a href="http://msdn.microsoft.com/en-us/library/ff802693.aspx#serial_topic1" rel="nofollow">MSDN article on serial com</a> using Do While to cycle through every character in the buffer, this method did not yield any good results either.</p>
  
  <pre><code>DWORD dwError  = 0;
  
  /*
  Using the EV_RXCHAR flag will notify the thread that a byte arrived at the port
  */
  //use SetCommMask and WaitCommEvent to see if byte has arrived at the port
  //SetCommMask sets the desired events that cause a notification.
  if(!SetCommMask(Cport[comport_number],EV_RXCHAR)){
      printf("SetCommMask Error");
      dwError = GetLastError();
      // Error setting com mask
      return FALSE;
  }
  //WaitCommEvent function detects the occurrence of the events.
  DWORD dwCommEvent;
  for( ; ; )
  {
      //wait for event to happen
      if (WaitCommEvent(Cport[comport_number],&amp;dwCommEvent,NULL))
      {
          //Do while loop will cycle ReadFile until bytes-read reach 0,
          do{
          if(ReadFile(Cport[comport_number], buf, size, (LPDWORD)((void *)&amp;n), NULL)){
              //Byte has been read, buf is processed in main
          }
          else{
              //error occoured in ReadFile call
              dwError = GetLastError();
              break;
              }
              }while(n);
          }
      else{
          //error in WaitCommEvent
          break;
      }
      break;  //break after read file
  }
  </code></pre>
  
  <p>I am wondering if rewriting the code in overlapped mode will improve things, but I dont see the advantages as I have no need for multi threading. any suggestions would be great!</p>
  
  <p>Thank you.</p>