我电路上用了芯片,把波特率可提高到921600,现用CSerialPort类编串口,发现它一次只读一个数据,这样似乎效率不行,我接上来的数据老出错,请问有哪位高手用过的这个类的,如果我要把它修改成每次读30个字节,要怎么修改?我的接收事件响应程序要怎么编?请指教....帮上忙就给分答谢...

解决方案 »

  1.   

    我试过这样:
    void CSerialPort::ReceiveChar(CSerialPort* port, COMSTAT comstat)
    {
    BOOL  bRead = TRUE; 
    BOOL  bResult = TRUE;
    DWORD dwError = 0;
    DWORD BytesRead = 0;
    // unsigned char RXBuff;    int nlength;
        unsigned char *RXBuff=new unsigned char[30];
        for (;;) 

    ...........
                    ...........

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

    EnterCriticalSection(&port->m_csCommunicationSync); if (bRead)
    {
    nlength=min(30,comstat.cbInQue);
    bResult = ReadFile(port->m_hComm, // Handle to COMM port 
    RXBuff, // RX Buffer Pointer
    nlength, // Read one byte
    &BytesRead, // Stores number of bytes read
            &port->m_ov); // pointer to the m_ov structure

    // deal with the error code 
      ::SendMessage((port->m_pOwner)->m_hWnd, WM_COMM_RXCHAR, (WPARAM) RXBuff, (LPARAM) nlength);
    } // end forever loop}
    接收响应函数:LONG CNIRDlg::OnCommunication(WPARAM *ch, LPARAM length)
    {
    for(int i=0;i<length;i++)
    {
    m_ReceiveData+=ch[i];
    } delete ch;
    return 0;
    }以上不行,不知道问题出在了哪里,请哪位高手指教啊,急啊!!!!
      

  2.   

    我试过这样: 
    void CSerialPort::ReceiveChar(CSerialPort* port, COMSTAT comstat) 

    BOOL  bRead = TRUE; 
    BOOL  bResult = TRUE; 
    DWORD dwError = 0; 
    DWORD BytesRead = 0; 
    // unsigned char RXBuff;     int nlength; 
        unsigned char *RXBuff=new unsigned char[30];  
    for (;;) 

    ........... 
    ........... if (comstat.cbInQue == 0) 

    // break out when all bytes have been read 
    break; 
    } EnterCriticalSection(&port->m_csCommunicationSync); if (bRead) 

      nlength=min(30,comstat.cbInQue); 
    bResult = ReadFile(port->m_hComm, // Handle to COMM port 
                       RXBuff, // RX Buffer Pointer 
                       nlength, // Read one byte 
                       &BytesRead, // Stores number of bytes read 
                       &port->m_ov); // pointer to the m_ov structure 

    // deal with the error code ...........
    ...........  ::SendMessage((port->m_pOwner)->m_hWnd, WM_COMM_RXCHAR, (WPARAM) RXBuff, (LPARAM) nlength); 
    } // end forever loop } 
    接收响应函数: LONG CNIRDlg::OnCommunication(WPARAM *ch, LPARAM length) 

    for(int i=0;i <length;i++) 

      m_ReceiveData+=ch[i]; 
    } delete ch; 
    return 0; 
    } 以上不行,不知道问题出在了哪里,请哪位高手指教啊,急啊!!!!
      

  3.   

    if (bRead) 

    nlength=min(30,comstat.cbInQue); 
    bResult = ReadFile(port->m_hComm, // Handle to COMM port 
    RXBuff, // RX Buffer Pointer 
    nlength, // Read one byte 
    &BytesRead, // Stores number of bytes read 
            &port->m_ov); // pointer to the m_ov structure 
    // deal with the error code 
      ::SendMessage((port->m_pOwner)->m_hWnd, WM_COMM_RXCHAR, (WPARAM) RXBuff, (LPARAM) nlength); 
    } // end forever loop 这种写法当然会出错了,在串口接收数据时,从开始,只要他有数据就开始读,nlength=min(30,comstat.cbInQue); 
    不是30的时候,可能是20时 ,你也读,也发送消息,这样过去后数据不完整,肯定会出错了,在这里加一个判断当实际读有30个字节是否在去读和发送消息:
    if (bRead) 

    nlength=min(30,comstat.cbInQue); 
     if(30==nlength)
     {
       bResult = ReadFile(port->m_hComm, // Handle to COMM port 
                  RXBuff, // RX Buffer Pointer 
                  nlength, // Read one byte 
                  &BytesRead, // Stores number of bytes read 
                &port->m_ov); // pointer to the m_ov structure 
         // deal with the error code 
        ::SendMessage((port->m_pOwner)->m_hWnd, WM_COMM_RXCHAR, (WPARAM) RXBuff, (LPARAM)     nlength); 
      }
    } // end forever loop 建议你定的读取字节最好是你数据大小的整倍数,这样有利于你OnCommunication(WPARAM *ch, LPARAM length) 分解数据,比如你数据传上来的格式是double型:1.24,2,21,3.56, 那么你定的每次读取的大小就是8+1的倍数