title: Serial communication read not working at all time
body: <p>I am writing a c++ application for half duplex communication to download data from a device. Following is the class i am using for serial communication.</p>
  
  <pre><code>class CSerialCommHelper 
  {
      HANDLE m_pPortHandle;           //Handle to the COM port
      HANDLE m_hReadThread;           //Handle to the Read thread
      HANDLE m_hPortMutex;            //Handle to Port Mutex
      std::wstring m_strPortName;     //Portname
      COMMTIMEOUTS m_CommTimeouts;    //Communication Timeout Structure
      _DCB dcb;                       //Device Control Block
      DWORD m_dwThreadID;             //Thread ID
      string m_strBuffer;
  
  public:
      CSerialCommHelper();
      HRESULT Open();
      HRESULT ConfigPort();
      static void * ReadThread(void *);
      HRESULT Write(const unsigned char *,DWORD);
      string GetFrameFromBuffer();
      HRESULT Close();
      ~CSerialCommHelper(void);
  };
  </code></pre>
  
  <p>ReadThread and Write function is as follows :</p>
  
  <pre><code>  void * CSerialCommHelper::ReadThread(void * pObj)
    {
      CSerialCommHelper *pCSerialCommHelper =(CSerialCommHelper *)pObj; 
      DWORD dwBytesTransferred =0;
      DWORD byte=0;;
  
      while (pCSerialCommHelper-&gt;m_pPortHandle != INVALID_HANDLE_VALUE)
      {
          pCSerialCommHelper-&gt;m_strBuffer.clear();
          pCSerialCommHelper-&gt;m_usBufSize=0;
  
          WaitForSingleObject(pCSerialCommHelper-&gt;m_hPortMutex,INFINITE);
  
          do
          {
              dwBytesTransferred = 0;
  
  
              ReadFile (pCSerialCommHelper-&gt;m_pPortHandle,&amp;byte,1,&amp;dwBytesTransferred,NULL);
              if (dwBytesTransferred == 1)
              {
                  pCSerialCommHelper-&gt;m_strBuffer.push_back((char)byte);
                  pCSerialCommHelper-&gt;m_usBufSize++;
                  continue;
              }
  
  
          }
          while ((dwBytesTransferred == 1) &amp;&amp; (pCSerialCommHelper-&gt;m_pPortHandle != INVALID_HANDLE_VALUE));
  
  
              ReleaseMutex(pCSerialCommHelper-&gt;m_hPortMutex);
              Sleep(2);
  
  
      }
      ExitThread(0);
  
  
      return 0;
  </code></pre>
  
  <p>}</p>
  
  <p>Write function waits for readthread to release mutex and writes to data to port .
  GetFrameFromBuffer will be called from application which uses the SerialCommhelper
  and it returns the m_strBuffer string . </p>
  
  <p>My problem is whenever i am trying to  download huge amount of data.
  I am losing some data frames . 
  I am getting response from device in between .0468 to .1716 secs.</p>
  
  <p>After analysing different error scenarios i came to know that is not problem with time as other frames are getting downloaded at the same time interval.</p>
  
  <p>Function which is calling getframebuffer is  continuosly calling it until is gets a filled string.</p>