在 windows COM 端口上禁用 CTS 流控制

Disable CTS flow control on windows COM port

我正在使用 SetCommState 配置 COM 端口。

        if (!BuildCommDCBA(
                 "baud=9600 parity=N data=8 stop=1",
                &dcbSerialParams))
            return;
        SetCommState(myCOMHandle, &dcbSerialParams);

这似乎启用了我的硬件不支持的 CTS 流量控制

        _COMMCONFIG cfg;
        DWORD sz = sizeof(cfg);
        if (!GetCommConfig(
                myCOMHandle, // Handle to the Serial port
                &cfg,
                &sz))
            std::cout << "GetCommConfig FAILED\n";
        DCB dcb = cfg.dcb;
        std::cout << "\nBaudRate " << dcb.BaudRate
                  << "\nfBinary " << dcb.fBinary
                  << "\nfParity " << dcb.fParity
                  << "\nfOutxCtsFlow " << dcb.fOutxCtsFlow ...

产出

BaudRate 9600
fBinary 1
fParity 0
fOutxCtsFlow 1

我试过使用

"baud=9600 parity=N data=8 stop=1 octs=off"

但这给出了相同的结果。

我也试过重写 BuildCommDCBA 的输出

dcbSerialParams.fOutxCtsFlow = 0;

        if (!BuildCommDCBA(
                 "baud=9600 parity=N data=8 stop=1",
                &dcbSerialParams))
            return;
        dcbSerialParams.fOutxCtsFlow = 0;
        SetCommState(myCOMHandle, &dcbSerialParams);

但这也给出了相同的结果。

BuildCommDCBA 的文档是这样说的

There are older and newer forms of the mode syntax. The BuildCommDCB function supports both forms. However, you cannot mix the two forms together.

The newer form of the mode syntax lets you explicitly set the values of the flow control members of the DCB structure. If you use an older form of the mode syntax, the BuildCommDCB function sets the flow control members of the DCB structure,

这似乎与我的问题有关。但是我找不到新旧形式的模式语法的描述。我看过 this.

我可以假设我使用的是更新的版本吗?为什么要设置 fOutxCtsFlow?我怎样才能强制取消设置?

根据MSDN

The BuildCommDCB function adjusts only those members of the DCB structure that are specifically affected by the lpDef parameter...

因此您需要确保所有其他字段都具有可接受的值。最简单的方法就是用

初始化
DCB dcbSerialParams = { 0 };

这应该通过将所有相关值设置为 FALSE0 来禁用所有流量控制。只要你的字符串设置了所有其他重要的东西(波特率、奇偶校验、停止位和数据大小),这应该没问题。特别是,您将获得:

fBinary = FALSE;
fNull = FALSE;
fErrorChar = FALSE;
fParity = FALSE;
fRtsControl = RTS_CONTROL_DISABLE;
fOutxCtsFlow = FALSE;
fOutX = FALSE;
fInX = FALSE;
fDtrControl = DTR_CONTROL_DISABLE;
fOutxDsrFlow = FALSE;

另一种选择是通过调用

之一来初始化字段