简体   繁体   中英

Disable CTS flow control on windows COM port

I am using SetCommState to configure a COM port.

        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

        _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

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

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 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. I have looked at this .

Can I assume I am using the newer from? Why is the fOutxCtsFlow being set? How can I force it to unset?

According to MSDN :

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

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 . 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

  • GetCommConfig() - Retrieves the current configuration of a communications device.
  • GetDefaultCommConfigA() - Retrieves the default configuration for the specified communications device.
  • GetCommState() - Retrieves the current control settings for a specified communications device.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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