如题,谢谢!!!

解决方案 »

  1.   

    DWORD _stdcall TGGCommunication::CommThread(LPVOID pParam)
    {
        TGGCommunication *port = (TGGCommunication*)pParam;
        port->m_bThreadAlive = true;
        //DWORD BytesTransfered = 0;
        DWORD Event = 0;
        DWORD CommEvent = 0;
        DWORD dwError = 0;
        COMSTAT comstat;
        BOOL  bResult = true;    // Clear comm buffers at startup
        if (port->m_hComm)  // check if the port is opened
            PurgeComm(port->m_hComm, PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT);    // begin forever loop.  This loop will run as long as the thread is alive.
            for (;;)
            {
                bResult = WaitCommEvent(port->m_hComm, &Event, &port->m_ov);
                if (!bResult)
                {
                    switch (dwError = GetLastError())
                    {
                        case ERROR_IO_PENDING:
                        {
                            break;
                        }
                        case 87:
                        {
                            break;
                        }
                        default:
                        {
                            port->ProcessErrorMessage("WaitCommEvent()");
                            break;
                        }
                    }
                }
                else
                {
                    bResult = ClearCommError(port->m_hComm, &dwError, &comstat);
                    if (comstat.cbInQue == 0)
                        continue;
                } // end if bResult
                Event = WaitForMultipleObjects(3, port->m_hEventArray, false, INFINITE);
                switch (Event)
                {
                    case 0:
                    {
                        port->m_bThreadAlive = false;
                        ExitThread(100);
                        break;
                    }
                    case 1: // read event
                    {
                        GetCommMask(port->m_hComm, &CommEvent);
                        if (CommEvent & EV_CTS)
                            ::SendMessage(port->m_pOwner->Handle, WM_COMM_CTS_DETECTED, (WPARAM) 0, (LPARAM) port->m_nPortNr);
                        if (CommEvent & EV_RXFLAG)
                            ::SendMessage(port->m_pOwner->Handle, WM_COMM_RXFLAG_DETECTED, (WPARAM) 0, (LPARAM) port->m_nPortNr);
                        if (CommEvent & EV_BREAK)
                            ::SendMessage(port->m_pOwner->Handle, WM_COMM_BREAK_DETECTED, (WPARAM) 0, (LPARAM) port->m_nPortNr);
                        if (CommEvent & EV_ERR)
                            ::SendMessage(port->m_pOwner->Handle, WM_COMM_ERR_DETECTED, (WPARAM) 0, (LPARAM) port->m_nPortNr);
                        if (CommEvent & EV_RING)
                            ::SendMessage(port->m_pOwner->Handle, WM_COMM_RING_DETECTED, (WPARAM) 0, (LPARAM) port->m_nPortNr);
                        if (CommEvent & EV_RXCHAR)
                            ReceiveChar(port, comstat);
                        break;
                    }
                    case 2:
                    {
                        WriteChar(port);
                        break;
                    }
                }// end switch
            }// close forever loop
        }
        delete port;
    }
      

  2.   

    可以在收之前sleep一下。
    因为你去收的时候设备未必准备好你去读数据了。
      

  3.   

    Sleep()
    因为你断点调试时相当于作了Sleep()操作。