Serial Port sometimes gets bad data

I have an application that I am working on that one of it's functions is to get data from a serial port and place the data in a class that I have set up for it.  The problem is that it doesn't always work.  The device that I am pulling data from sends the same data everytime but the pc is not receiving the same data everytime.  I am using createfile, readfile and writefile to use the com port.  My connection function where I use createfile looks like this
BOOL CSerialComm::OpenConnection()
{
      COMMTIMEOUTS     CommTimeOuts;
      hComm = CreateFile(      Port,
                                    GENERIC_READ | GENERIC_WRITE,
                                    0,
                                    0,
                                    OPEN_EXISTING,
                                    NULL,
                                    0);
      // open COMM device
      if (hComm == INVALID_HANDLE_VALUE)
      {
            return FALSE;
      }
      else
      {
            SetCommMask(hComm ,EV_RXCHAR );            // get any early notifications
            SetupComm(hComm, 8857, 8857);            // setup device buffers
            PurgeComm(hComm, PURGE_TXABORT | PURGE_RXABORT |      
                  PURGE_TXCLEAR | PURGE_RXCLEAR );// purge any information in the buffer
            // if Set up for overlapped I/O
            CommTimeOuts.ReadIntervalTimeout = 100;
            CommTimeOuts.ReadTotalTimeoutMultiplier = 10;
            CommTimeOuts.ReadTotalTimeoutConstant = 100;
            CommTimeOuts.WriteTotalTimeoutMultiplier = 10;
            CommTimeOuts.WriteTotalTimeoutConstant = 100;
            SetCommTimeouts(hComm, &CommTimeOuts);
      }
      // Setup COMM device
      if (!GetCommState( hComm, &dcb))
      {
            return FALSE;
      }
      else
      {
            dcb.DCBlength = sizeof(DCB);
            dcb.BaudRate = BaudRate;
            dcb.ByteSize = ByteSize;
            dcb.Parity = Parity;
            dcb.StopBits = StopBits;
            // setup hardware flow control
            if( bSet ==FC_DTRDSR)
            {
                  dcb.fOutxDsrFlow = bSet;
                  if (bSet)
                        dcb.fDtrControl = DTR_CONTROL_HANDSHAKE;
                  else
                        dcb.fDtrControl = DTR_CONTROL_ENABLE;
            }
            else if (bSet == FC_RTSCTS)
            {
                  dcb.fOutxCtsFlow = bSet;
                  if (bSet)
                        dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
                  else
                        dcb.fRtsControl = RTS_CONTROL_ENABLE;
            }
            else if (bSet == FC_XONXOFF)      // setup software flow control
            {
                  dcb.fInX = dcb.fOutX = bSet;
                  dcb.XonChar = ASCII_XON;
                  dcb.XoffChar = ASCII_XOFF;
                  dcb.XonLim = 100;
                  dcb.XoffLim = 100;
            }
            else
            {
            }
            dcb.fBinary = TRUE;
            dcb.fParity = TRUE;
            SetCommState(hComm, &dcb);
      }
      return TRUE;
}
my write seems to be working fine so I won't show it but my read looks like this:
BOOL CSerialComm::ReadCommBlock(LPSTR lpszBlock,int nMaxLength )
{
      DWORD dwLength = 0;
      DWORD dwRead;
      OVERLAPPED osReader = {0};

      osReader.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
      if (osReader.hEvent == NULL)
      {
            CloseHandle(osReader.hEvent);
            return FALSE;
      }
      if (!ReadFile(hComm, lpszBlock, nMaxLength, &dwRead, &osReader))
      {
            if (GetLastError()!= ERROR_IO_PENDING)      //read not delayed?
            {      //Error in communication
                  CloseHandle(osReader.hEvent);
                  return FALSE;
            }
      }
      else
      {      // read completed immediately
            if (dwRead < 1)
            {
                  CloseHandle(osReader.hEvent);
                  return FALSE;
            }
            else
            {
                  CloseHandle(osReader.hEvent);
                  return TRUE;
            }
      }
      CloseHandle(osReader.hEvent);
      return FALSE;
}
and I am setting up my serial like
CSerialComm::CSerialComm()
{
      BaudRate = CBR_9600;
      ByteSize = 8;
      Parity = NOPARITY;
      StopBits = ONESTOPBIT;
      bSet = 0x00;//FC_XONXOFF;//
      strcpy (Port, "COM1");
      FillMemory(&dcb, sizeof(dcb),0);
}
I am using it like
            char MBuffer[9000] = "";
            CSerialComm Serial;
            Serial.WriteCommChar(2);
            if (Serial.ReadCommBlock(MBuffer, 1) > 0){
                  if (Serial.ReadCommBlock(MBuffer+1, 8999) > 0)
                  {
                  // Some code
                  }
            }
            Serial.CloseConnection();
What would cause it to get false data.  One thing I thought was maybe there is still garbage in the rx line but I don't know how to check it or clear it out if there is.  Can someone help me please.
dociebAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

grg99Commented:
What exactly is wrong with the data?   Is it garbled?  Is some of the data missing?

If it's garbled there may be a handshaking problem.     Both ends have to be set up for the same kind of handshaking, AND the cable has to be correctly wired to pass the handshake info.

If you're sending just text, then you can use xon/xoff handshaking.  You can't if you're sending binary data as that can include xon or xoff.  Then you have to use hardware handshakng, which requires the cts/rts lines to be properly configured in the cable.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dociebAuthor Commented:
Sorry, I found the problem.  I had a problem with my clearRx function so it wasn't being cleared before I used it.  Here are some easy points for you.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.