为什么我用SetCommState老是返回FALSE?有哪些情况会调用该函数不成功?代码如下:
// 。GetCommState(hCom, &dcb); // hCom是串口句柄,不为空
dcb.fBinary  = TRUE;
dcb.fParity  = TRUE;
dcb.StopBits = 1;
dcb.BaudRate = 9600;
dcb.ByteSize = 8;
dcb.Parity   = NOPARITY;
if ( !SetCommState(hCom, &dcb) )
{
    AfxMessageBox("Set fail.");
    return;
}//。请帮帮忙!! 谢谢!

解决方案 »

  1.   

    If the function fails, the return value is zero. To get extended error information, call GetLastError. 
      

  2.   

    TO: zhuyanwei(zyw) 谢谢! 为什么要把停止位设成0? 为什么设成1不行?
      

  3.   

    The SetCommState function fails if the XonChar member of the DCB structure is equal to the XoffChar member. 
    加上下面几句试试:
       dcb.XonChar = ASCII_XON ;
       dcb.XoffChar = ASCII_XOFF ;
       dcb.XonLim = 100 ;
       dcb.XoffLim = 100 ;
      

  4.   

    我觉得是dcb结构没有完全初始化,
    正如ymbymb所说,加上
    dcb.XonChar = ASCII_XON ;
       dcb.XoffChar = ASCII_XOFF ;
       dcb.XonLim = 100 ;
       dcb.XoffLim = 100 ;
    之类试试吧
      

  5.   

    你采用FormatMessage+GetLastError来看看到底是什么原因?然后对证下药。
    加上下面四句看看(不使用流控制):
    dcb.fOutxCtsFlow = FALSE;
    dcb.fOutxDsrFlow = FALSE;
    dcb.fOutX = FALSE;
    dcb.fInX = FALSE;
      

  6.   

    DCB::StopBits
    0:1个停止位
    1:1.5个停止位
    2:2个停止位.