Cserialport类编写的串口助手,如何实现接受两个字节的数据,这是Cserialport类中的接受数据处理函数

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

}

1个回答

贴出你的代码看看。

你现在是自己给自己发消息?用spy++看看收到的消息是什么。

baidu_31083567
java-flash 我写的串口助手程序,在我的电脑上运行不丢包,在其他电脑上运行就会丢包,你有遇到过这么问题吗,是什么原因引起的呢
4 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问