int i =1;
while(i)
{
   WaitCommEvent(hComDev, &mask,&m_ov);
   while (((mask & EV_RXCHAR) != EV_RXCHAR) && count <100)
   {
    count++;
      WaitCommEvent(hComDev, &mask,&m_ov);
      Sleep(10);
    }
    ReadFile(hComDev, AcceptCode, 1, &nRealRead, &m_ov);
    if(!memcmp(AcceptCode,"T"))
        return  1;
    if(!memcmp(AcceptCode,"S"))
        Flag =1;
}
从串口读数据,如果第一次读到的是‘S’就再次读去串口,直到读到是'T'
现在问题是第一次能读到‘S’,但第二次读不到‘T’,但下位机确实已经发出‘T’了,不知道怎么回事?

解决方案 »

  1.   

    读之前先ClearCommError
    再判断一下cbInQue
    你用的是多线程的吧,给你个例子:
    void CSerialPort::ReceiveChar(CSerialPort* port, COMSTAT comstat)
    {
    BOOL  bRead = TRUE; 
    BOOL  bResult = TRUE;
    DWORD dwError = 0;
    DWORD BytesRead = 0;
    unsigned char RXBuff; for (;;) 

    // Gain ownership of the comm port critical section.
    // This process guarantees no other part of this program 
    // is using the port object. 

    EnterCriticalSection(&port->m_csCommunicationSync); // ClearCommError() will update the COMSTAT structure and
    // clear any other errors.

    bResult = ClearCommError(port->m_hComm, &dwError, &comstat); LeaveCriticalSection(&port->m_csCommunicationSync); // start forever loop.  I use this type of loop because I
    // do not know at runtime how many loops this will have to
    // run. My solution is to start a forever loop and to
    // break out of it when I have processed all of the
    // data available.  Be careful with this approach and
    // be sure your loop will exit.
    // My reasons for this are not as clear in this sample 
    // as it is in my production code, but I have found this 
    // solutiion to be the most efficient way to do this.

    if (comstat.cbInQue == 0)
    {
    // break out when all bytes have been read
    break;
    }

    EnterCriticalSection(&port->m_csCommunicationSync); if (bRead)
    {
    bResult = ReadFile(port->m_hComm, // Handle to COMM port 
       &RXBuff, // RX Buffer Pointer
       1, // Read one byte
       &BytesRead, // Stores number of bytes read
       &port->m_ov); // pointer to the m_ov structure
    // deal with the error code 
    if (!bResult)  

    switch (dwError = GetLastError()) 

    case ERROR_IO_PENDING: 

    // asynchronous i/o is still in progress 
    // Proceed on to GetOverlappedResults();
    bRead = FALSE;
    break;
    }
    default:
    {
    // Another error has occured.  Process this error.
    port->ProcessErrorMessage("ReadFile()");
    break;

    }
    }
    else
    {
    // ReadFile() returned complete. It is not necessary to call GetOverlappedResults()
    bRead = TRUE;
    }
    }  // close if (bRead) if (!bRead)
    {
    bRead = TRUE;
    bResult = GetOverlappedResult(port->m_hComm, // Handle to COMM port 
      &port->m_ov, // Overlapped structure
      &BytesRead, // Stores number of bytes read
      TRUE);  // Wait flag // deal with the error code 
    if (!bResult)  
    {
    port->ProcessErrorMessage("GetOverlappedResults() in ReadFile()");
    }
    }  // close if (!bRead)

    LeaveCriticalSection(&port->m_csCommunicationSync); // notify parent that a byte was received
    ::SendMessage((port->m_pOwner)->m_hWnd, WM_COMM_RXCHAR, (WPARAM) RXBuff, (LPARAM) port->m_nPortNr);//发送消息到窗口处理 } // end forever loop}
      

  2.   


    试一下下面的代码:while(1)

        WaitCommEvent(hComDev, &mask,&m_ov);
        SetCommMask(hComDev,EV_RXCHAR);
        
        if(mask & EV_RXCHAR)
        {
             do
              {
                ReadFile(hComDev, AcceptCode, 1, &nRealRead, &m_ov);
                if(nRealRead==1)
                {
                  //处理接到的字符
                 }
               }while (nRealRead==1)
         }  
    }
      

  3.   

    如:pcitman(不缺分只缺钱) 所说:
     
      读之前先ClearCommError
      再判断一下cbInQue这是速度不匹配的问题!