Closing FTDI Comm port with CloseHandle() takes a lot of time

  c++, ftdi, serial-port, winapi

I’m working in a C/C++ program that communicates with a RS232 device.
The port is opened using CreateFile()

hComm = CreateFile(DevPath, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);

Then, the port is Setup using SetupComm(), SetCommState() and SetCommTimeouts() with no hardware control and a baudrate of 115200 baud and timeouts as shown here:

    cTimeouts.ReadIntervalTimeout = MAXDWORD;
    cTimeouts.ReadTotalTimeoutConstant = 1;
    cTimeouts.ReadTotalTimeoutMultiplier = 0;
    cTimeouts.WriteTotalTimeoutConstant = 40;
    cTimeouts.WriteTotalTimeoutMultiplier = 1;

The port is closed using CloseHandle()

CloseHandle(hComm);

This is not a real-time system but has to be very fast, and after noticing some delays I’ve measured that instruction CloseHandle() takes always more than 100ms to run, which is a huge amount of time, taking into account that opening and configuring the port takes only 20ms.

Test conditions are as follows:

  • There isn’t any anti-virus or firewall in the test machine (off-line).
  • I’m using Visual Studio on an brand-new Intel-based Core-i5 computer, running in Windows 10.
  • FTDI USB device (Virtual CommPort)
  • There is no data pending to be read or written (or it shouldn’t).

I’ve tried to compile a similar program in Linux and the Close time reduces to 25ms.
I’ve also tried to use a physical RS232 connection and the close times turns to be 1ms, so I presume this comes from the FTDI driver.

Does anyone have any clue on what could be happening. Am I missing anything?
Did anyone experienced something similar?

Source: Windows Questions C++

LEAVE A COMMENT