C++ Configure DCB before opening serial port

Wim Sturkenboom 0 Reputation points
2024-02-04T17:26:16.3133333+00:00

In C# it's reasonably easy to create a serial port object, configure it and open the serial port. I'm looking for a way to do the same in C++. Opening the serial port with CreateFile asserts DTR and RTS which I would like to prevent. For this, I have the below code but it does not behave as expected. It retrieves the default COMMCONFIG, modifies it and sets it with the modified values. However the default does not change and opening the serial port still results in DTR being asserted. Tested with an SparkFun RedBoard with FT232 chip.

Advice appreciated.

// SerialConsoleCpp_DTR_RTS.cpp : This file contains the 'main' function. Program execution begins and ends there.
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,693 questions
{count} votes

1 answer

Sort by: Most helpful
  1. k s 0 Reputation points
    2024-09-11T12:13:24.6333333+00:00

    Dear friend, have you found the solution or workaround? I had the same issue with CH340 chip. I was planning to use RTS# line to reset my MCU. The problem was that after PC reboot or USB replug COM settings resets to defaults with RTS# & DTR# enabled. Then I have undesired pulse at first port opening with CreateFile() until DCB init with SetCommState(). With WCH VCP driver I have option DisableModemHandShake (see picture). With this option checked, the problem was solved, RTS# & DTR# lines not asserts, even after USB replug, DCB reading gives fRtsControl, fDtrControl = 0. However, I still can use these signals with EscapeCommFunction() method. FTDI VCP driver gives similar option (disable modem signals at startup), but unfortunately it doesn't work same way :( Did you solve the problem with FT232 chip?CH340_Modem_off

    HANDLE hComPort;
    char szComPortName[20];
    // ...	
    /* Try open */
    
    	hComPort = CreateFile(szComPortName, (GENERIC_READ | GENERIC_WRITE), 0, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
    // RTS# & DTR# signals asserted now
    	if (hComPort == INVALID_HANDLE_VALUE) {
    		GetLastErrorMessage();
    		return false;
    	}
    /* Set params */
    DCB dcb {};
    	dcb.DCBlength = sizeof(DCB);
    	GetCommState(hComPort, &dcb);
    	dcb.BaudRate = (DWORD)baud;
    	dcb.fBinary = 1;
    	dcb.ByteSize = 8;
    	dcb.Parity = NOPARITY;
    	dcb.StopBits = ONESTOPBIT;
    	dcb.fRtsControl = RTS_CONTROL_DISABLE;
    	dcb.fDtrControl = DTR_CONTROL_DISABLE;
    	if (!SetCommState(hComPort, &dcb)) {
    		GetLastErrorMessage();
    		Close();
    		return false;
    	}
    
    // RTS# & DTR# signals disabled after SetCommState() success.
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.