最近做了一个有关串口通信的程序,在我们这边怎么测试都可以, 但一交到客户那边就问题百出,不是普通COM口通信失败,就是USB转COM口通信失败。通过LOG,我们知道原因所在,就是我们软件对串口DCB流控制的设置,跟客户的板子对DCB流控制设置不一致。而我们在无法获知客户DCB流控制设置的情况下,瞎改了一番DCB设置,结果是可想而知的。由参考得知如下经验(vc代码),希望和遇到同类问题的XDJM们一起分享。
 
VOID InitDCB(...)
{
     ...........
    // Set serial parameters
    dcb.BaudRate = m_dwBaudRate;     // Band rate
    dcb.ByteSize = m_nByteSize;      // Data bit, either 7 or 8
    dcb.Parity = m_nParity;          // Parity, must between 0 to 4
    dcb.StopBits = m_nStopBits;      // Stop bit must between 0 to 2
    dcb.fBinary = TRUE;              // Bianry must be TRUE in Win32
    dcb.fParity = FALSE;
   
#ifdef WIN32
    dcb.fDtrControl = DTR_CONTROL_ENABLE;
    dcb.fRtsControl = RTS_CONTROL_ENABLE;
#endif
 
    switch (stPortPara->Flow) { // Judge flow control
    case IdFlowX:
    /*--------软件流控制方式---------------
       dcb.fOutX = TRUE;
       dcb.fInX = TRUE;
       dcb.XonLim = CommXonLim;
       dcb.XoffLim = CommXoffLim;
       dcb.XonChar = XON;
       dcb.XoffChar = XOFF;
    --------------------------------------*/
       break;
    case IdFlowHard:
    /*--------硬件流控制方式---------------
       dcb.fOutxCtsFlow = TRUE;
#ifdef WIN32
       dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
#else
       dcb.CtsTimeout = 30;
       dcb.fRtsflow = TRUE;
#endif
    --------------------------------------*/
       break;
 
    }
     ...........
}