串口程序中,第1次串口已经设置为“9600,m,7,1”,如何更改设置为“9600,s,7,1”

解决方案 »

  1.   

    可以不用关闭.用getcommstate获取当前的DCB配置,更改参数,然后用setcommstate设置
      

  2.   

    同意楼上的 // Reset error state
    m_lLastError = ERROR_SUCCESS; // Check if the device is open
    if (m_hFile == 0)
    {
    // Set the internal error code
    m_lLastError = ERROR_INVALID_HANDLE; // Issue an error and quit
    _RPTF0(_CRT_WARN,"CSerial::Setup - Device is not opened\n");
    return m_lLastError;
    } // Obtain the DCB structure for the device
    CDCB dcb;
    if (!::GetCommState(m_hFile,&dcb))
    {
    // Obtain the error code
    m_lLastError = :: GetLastError(); // Display a warning
    _RPTF0(_CRT_WARN,"CSerial::Setup - Unable to obtain DCB information\n");
    return m_lLastError;
    } // Set the new data
    dcb.fBinary = DWORD(1);//允许二进制
    dcb.BaudRate = DWORD(eBaudrate);
    dcb.ByteSize = BYTE(eDataBits);
    dcb.Parity   = BYTE(eParity);
    dcb.StopBits = BYTE(eStopBits); // Determine if parity is used
    dcb.fParity  = (eParity != EParNone); // Set the new DCB structure
    if (!::SetCommState(m_hFile,&dcb))
    {
    // Obtain the error code
    m_lLastError = ::GetLastError(); // Display a warning
    _RPTF0(_CRT_WARN,"CSerial::Setup - Unable to set DCB information\n");
    return m_lLastError;
    } // Return successful
    return m_lLastError;