本人用ReadFile读串口,可每次只能读8个字节,不知哪里出问题了。初始化串口的代码如下:
HANDLE CBjqDlg::hInitComPort(int nComNum,int nBaud,int nData,int nStop)
{ if(m_hComPart!=NULL)
CloseHandle(m_hComPart);
char *ComName;
ComName=new char[MAX_PATH];
sprintf(ComName,"COM%d",nComNum);
m_hComPart=CreateFile(ComName, 
                GENERIC_WRITE|GENERIC_READ,
0,
NULL,
OPEN_EXISTING,
0,
NULL);
delete ComName;
    if(m_hComPart!=INVALID_HANDLE_VALUE)
{
        GetCommState(m_hComPart,&mDCB);
mDCB.BaudRate=nBaud;
mDCB.ByteSize=nData;
mDCB.StopBits=nStop;
mDCB.Parity=0;
SetCommState(m_hComPart,&mDCB);
COMMTIMEOUTS ctout;
GetCommTimeouts(m_hComPart,&ctout);
ctout.ReadIntervalTimeout=MAXDWORD;
ctout.ReadTotalTimeoutConstant=0;
ctout.ReadTotalTimeoutMultiplier=0;
SetCommTimeouts(m_hComPart,&ctout);
return m_hComPart;
}
else
return NULL;
}
大家帮忙呀!

解决方案 »

  1.   

    tmerr_t CSerial::serial_setup(void)
    {
        DCB dcb;
    //    BOOL fSuccess;
        COMMTIMEOUTS timeouts;
        static const char *pcCommPort[4] = {"COM1", "COM2", "COM3", "COM4"};  // create events
    if (m_ov.hEvent != NULL)
    ResetEvent(m_ov.hEvent);
    m_ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
    if (m_hWriteEvent != NULL)
    ResetEvent(m_hWriteEvent);
    m_hWriteEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
    if (m_hShutdownEvent != NULL)
    ResetEvent(m_hShutdownEvent);
    m_hShutdownEvent = CreateEvent(NULL, TRUE, FALSE, NULL); // initialize the event objects
    m_hEventArray[0] = m_hShutdownEvent; // highest priority
    m_hEventArray[1] = m_ov.hEvent;
    m_hEventArray[2] = m_hWriteEvent;    
        hCom = CreateFile(pcCommPort[0],
          GENERIC_READ | GENERIC_WRITE,
          0,    // comm devices must be opened w/exclusive-access
          NULL, // no security attributes
          OPEN_EXISTING, // comm devices must use OPEN_EXISTING
          FILE_FLAG_OVERLAPPED,//FILE_FLAG_OVERLAPPED,0
          NULL  // hTemplate must be NULL for comm devices
            );
        
        if (hCom == INVALID_HANDLE_VALUE) {
            Application->MessageBox("CreateFile(): error (failed to open serial port)",NULL,MB_OK);
            return E_PKTDRV_INIT;
         }    // We will build on the current configuration, and skip setting the size
        // of the input and output buffers with SetupComm.
        if (!(GetCommState(hCom, &dcb))) {
            Application->MessageBox("GetCommState() error ",NULL,MB_OK);
            return E_PKTDRV_INIT;
        }    // Fill in the DCB
        dcb.BaudRate = CBR_115200;      // set the baud rate
        dcb.ByteSize = 8;             // data size, xmit, and rcv
        dcb.Parity = NOPARITY;        // no parity bit
        dcb.StopBits = ONESTOPBIT;    // one stop bit
        dcb.fAbortOnError = FALSE;    // Bug fix "ReadFile() error 995"    // Note the DTR = +12V and RTS = -12V is needed to power the serial
        // level converter! dcb.fDtrControl = DTR_CONTROL_ENABLE; 
    dcb.fRtsControl = RTS_CONTROL_DISABLE;     if (!(SetCommState(hCom, &dcb))) {
            Application->MessageBox("SetCommState(): error ",NULL,MB_OK);
            return E_PKTDRV_INIT;
        }     timeouts.ReadIntervalTimeout = 0;
        timeouts.ReadTotalTimeoutMultiplier = 0;
        timeouts.ReadTotalTimeoutConstant = 0;
        timeouts.WriteTotalTimeoutMultiplier = 0;
        timeouts.WriteTotalTimeoutConstant = 0;    assert(SetCommTimeouts (hCom, &timeouts));    
        assert(SetCommMask (hCom, EV_RXCHAR));    return E_OK;}static void serial_listener(LPVOID pParam) //unsigned long __stdcall
    {
    CSerial * pCSerial = (CSerial*)pParam;//    int nRet;
    COMSTAT comstat;
         DWORD event_mask;
    DWORD dwError = 0;
    DWORD Event = 0;
    DWORD CommEvent = 0;    while (1)
        {
            // Wait the event signalling characters received.
    //        if (WaitCommEvent (hCom, &event_mask, NULL) != TRUE) {
            if (WaitCommEvent (pCSerial->hCom, &event_mask, &pCSerial->m_ov) != TRUE) {
    // If WaitCommEvent() returns FALSE, process the last error to determin
    // the reason..
    // DWORD dwError = 0; switch (dwError = GetLastError())

    case ERROR_IO_PENDING: 

    // This is a normal return value if there are no bytes
    // to read at the port.
    // Do nothing and continue
    break;
    }
    case 87:
    {
    // Under Windows NT, this value is returned for some reason.
    // I have not investigated why, but it is also a valid reply
    // Also do nothing and continue.
    break;
    }
    default:
    {
    // All other error codes indicate a serious error has
    // occured.  Process this error.
                Application->MessageBox("WaitCommEvent() error ",NULL,MB_OK);
    break;
    }
    }
            }else {
                ClearCommError(pCSerial->hCom, &dwError, &comstat);
                if (comstat.cbInQue == 0)
                    continue;
            } // if(!nRet){
            
            // Main wait function.  This function will normally block the thread
            // until one of nine events occur that require action.
            Event = WaitForMultipleObjects(3, pCSerial->m_hEventArray, FALSE, INFINITE);
            switch (Event)
            {
                case 0:
                // Shutdown event.  This is event zero so it will be
                // the higest priority and be serviced first.                // Kill this thread.  break is not needed, but makes me feel better.
                    pCSerial->serial_exit();
                    break;
                case 1: // read event
                    GetCommMask(pCSerial->hCom, &CommEvent);
                    if (CommEvent & EV_RXCHAR){
                    // Receive character event from port.
                        pCSerial->ReceiveChar(comstat);
                    }
                    break;
                case 2: // write event
                     // Write character event from port
    //                pCSerial->serial_transmit();
                    break;
                default:
                    break;
            } // end switch    }//while(1)
    }
      

  2.   

    感谢lilyh(力力)大力支持,等我编好程序即结贴。