【问题标题】:Set DCB Fails When Attempting to Configure COM Port尝试配置 COM 端口时设置 DCB 失败
【发布时间】:2011-05-10 11:33:16
【问题描述】:

我正在尝试编写一个使用串行端口(例如 COM8)的 C++ MFC 应用程序。每次我尝试设置 DCB 时都会失败。如果有人能指出我做错了什么,我将不胜感激。

DCB dcb = {0};

dcb.DCBlength = sizeof(DCB);
port.Insert( 0, L"\\\\.\\" );

m_hComm = CreateFile(
    port,                           // Virtual COM port
    GENERIC_READ | GENERIC_WRITE,   // Access: Read and write
    0,                              // Share: No sharing
    NULL,                           // Security: None
    OPEN_EXISTING,                  // The COM port already exists.
    FILE_FLAG_OVERLAPPED,           // Asynchronous I/O.
    NULL                            // No template file for COM port.
    );

if ( m_hComm == INVALID_HANDLE_VALUE )
{
    TRACE(_T("Unable to open COM port."));
    ThrowException();
}

if ( !::GetCommState( m_hComm, &dcb ) )
{
    TRACE(_T("CSerialPort : Failed to get the comm state - Error: %d"), GetLastError());
    ThrowException();
}

dcb.BaudRate = 38400;               // Setup the baud rate.
dcb.Parity = NOPARITY;              // Setup the parity.
dcb.ByteSize = 8;                   // Setup the data bits.
dcb.StopBits = 1;                   // Setup the stop bits.

if ( !::SetCommState( m_hComm, &dcb ) ) // <- Fails here.
{
    TRACE(_T("CSerialPort : Failed to set the comm state - Error: %d"), GetLastError());
    ThrowException();
}

谢谢。

附加信息: 生成的错误代码为 87:“参数不正确。” 可能是微软有用的错误代码。 j/k

【问题讨论】:

  • 你可以提错误码。
  • @Amnon:好的,我在原始帖子中添加了错误代码信息,但我认为它没有多大帮助。

标签: c++ visual-studio-2008 mfc serial-port serial-communication


【解决方案1】:

这是我的代码,它运行良好。

/* Try to open the port */
hCom = CreateFile(szPort, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);

if (hCom != INVALID_HANDLE_VALUE) {
    printf("Handle success\n");
}

    dcb = { 0 };
    dcb.DCBlength = sizeof(dcb);

    fSuccess = GetCommState(hCom, &dcb);

    if (!fSuccess) {
        // Handle the error.
        printf("GetCommState failed with error %d.\n", GetLastError());
        CloseHandle(hCom);
        return APP_ERROR;
    }

    // Fill in DCB: 57,600 bps, 8 data bits, no parity, and 1 stop bit.
    dcb = { 0 };
    dcb.DCBlength = sizeof(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

    fSuccess = SetCommState(hCom, &dcb);

    if (!fSuccess) {
        // Handle the error.
        printf("SetCommState failed with error %d.\n", GetLastError());
        CloseHandle(hCom);
        return APP_ERROR;
    }
}

printf("Serial port successfully reconfigured.\n");

【讨论】:

    【解决方案2】:

    我能够使用 BuildCommDCB 解决问题:

    DCB dcb = {0};
    
    if ( !::BuildCommDCB( _T("baud=38400 parity=N data=8 stop=1"), &dcb ) )
    {
        TRACE(_T("CSerialPort : Failed to build the DCB structure - Error: %d"), GetLastError());
        ThrowException();
    }
    

    【讨论】:

    • 那么明确设置的字段和BuildCommDCB有什么区别?或者,只要它现在还在工作,我们就不在乎!
    • 我只是想让它工作。我投票赞成的关于停止位的 cmets 可能是正确的。
    • 那么,从您的原始代码中,您删除了波特率、奇偶校验等设置,并用 BuildCommDCB 替换了它们,或者您同时使用了两者来使其工作?
    • 这也给了我错误:':' token 之前的预期表达式
    【解决方案3】:

    我的钱花在这个上:

    dcb.StopBits = 1; 
    

    The MSDN docs 这么说 StopBits:

    要使用的停止位的数量。该成员可以是 以下值。

    ONESTOPBIT    0    1 stop bit.
    ONE5STOPBITS  1    1.5 stop bits.
    TWOSTOPBITS   2    2 stop bits.
    

    所以,你要求 1.5 个停止位,这是一个非常古老的东西,我什至不记得它来自哪里。电传打字机,可能。

    我猜您的驱动程序/硬件支持此模式的可能性很小,因此会出现错误。

    所以,改成dcb.StopBits = ONESTOPBIT;

    【讨论】:

      【解决方案4】:

      以下是一些可能性,不分先后。

      • GetCommState 正在用垃圾填充结构,因为端口尚未初始化。您可以跳过这一步。
      • 有两个参数控制奇偶校验设置,不清楚是否有无效组合。
      • StopBits 的值不是位数,它是一个幻数常数。值 1 等于 ONE5STOPBITS,与其他参数结合使用时可能无效。

      【讨论】:

      • 感谢您的提示。它们绝对值得牢记。
      【解决方案5】:

      看看你给函数的参数。正如错误代码所说,它们可能不正确。谷歌搜索“SetCommState 87”显示参数(例如波特率)与串行端口不兼容的几个实例。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-16
        • 2016-10-23
        • 1970-01-01
        相关资源
        最近更新 更多