简体   繁体   English

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

[英]Disable CTS flow control on windows COM port

I am using SetCommState to configure a COM port.我正在使用SetCommState配置 COM 端口。

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

This appears to enable the CTS flow control, which my hardware does not support这似乎启用了我的硬件不支持的 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 ...

outputs输出

BaudRate 9600
fBinary 1
fParity 0
fOutxCtsFlow 1

I have tried using我试过使用

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

but this gives the same result.但这给出了相同的结果。

I have also tried over-writing the output from BuildCommDCBA我也尝试过从 BuildCommDCBA 重写 output

dcbSerialParams.fOutxCtsFlow = 0; dcbSerialParams.fOutxCtsFlow = 0;

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

but this also gives the same result.但这也给出了相同的结果。

The documentation for BuildCommDCBA says this BuildCommDCBA的文档说明了这一点

There are older and newer forms of the mode syntax.模式语法有较旧和较新的 forms。 The BuildCommDCB function supports both forms. BuildCommDCB function 支持 forms。 However, you cannot mix the two forms together.但是,您不能将两个 forms 混合在一起。

The newer form of the mode syntax lets you explicitly set the values of the flow control members of the DCB structure.较新形式的模式语法允许您显式设置 DCB 结构的流控制成员的值。 If you use an older form of the mode syntax, the BuildCommDCB function sets the flow control members of the DCB structure,如果您使用较旧形式的模式语法,则 BuildCommDCB function 设置 DCB 结构的流控制成员,

This certainly seems to have something to do with my problem.这当然似乎与我的问题有关。 However I cannot find a description of the newer and older forms of the mode syntax.但是我找不到模式语法的新旧 forms 的描述。 I have looked at this .我看过这个

Can I assume I am using the newer from?我可以假设我使用的是较新的吗? Why is the fOutxCtsFlow being set?为什么要设置 fOutxCtsFlow? How can I force it to unset?我怎样才能强制它取消设置?

According to MSDN :根据MSDN

The BuildCommDCB function adjusts only those members of the DCB structure that are specifically affected by the lpDef parameter... BuildCommDCB function 仅调整 DCB 结构中受 lpDef 参数影响的那些成员...

So you need to make sure all the other fields have acceptable values.因此,您需要确保所有其他字段都具有可接受的值。 The simplest way is to just initialize with最简单的方法是初始化

DCB dcbSerialParams = { 0 };

This should disable all flow control by setting all pertinent values to FALSE or 0 .这应该通过将所有相关值设置为FALSE0来禁用所有流控制。 As long as your string sets all the other important stuff (baud rate, parity, stop bits, and data size) this should be ok.只要您的字符串设置了所有其他重要的东西(波特率、奇偶校验、停止位和数据大小),这应该没问题。 In particular, you will get:特别是,您将获得:

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

Another option is to initialize the fields by calling one of另一种选择是通过调用其中一个来初始化字段

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM