5

我正在尝试编写一个使用串行端口(例如 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

4

5 回答 5

12

我的钱在这个:

dcb.StopBits = 1; 

MSDN 文档是这样说 StopBits 的:

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

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

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

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

因此,将其更改为dcb.StopBits = ONESTOPBIT;

于 2010-11-15T22:49:18.313 回答
3

我能够使用以下方法解决问题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();
}
于 2010-11-15T21:42:58.550 回答
3

以下是一些没有特定顺序的可能性。

  • GetCommState由于端口尚未初始化,因此正在用垃圾填充结构。你可以跳过这一步。
  • 有两个参数控制奇偶校验设置,不清楚是否有任何无效组合。
  • StopBits 的值不是位数,它是一个幻数常数。值 1 等于ONE5STOPBITS与其他参数组合时可能无效的值。
于 2010-11-15T21:56:35.380 回答
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");
于 2018-06-11T12:30:42.857 回答
0

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

于 2010-11-15T21:19:09.393 回答