I am currently facing issues with the CloseHandle method, trying to close a virtual serial port COM in C++. I use the methods CreateFile and WriteFile to send messages to my device. When my application closes, I call the CloseHandle method in my class destructor. Everything works fine when I close properly my application.
My device is plugged in the PC using a RJ45 to USB adaptor, which is plugged into the USB port of my computer. Then, I use a FTDI driver to communicate on a virtual serial COM port.
The issue occurs when I unplug the RJ45 cable from the adaptor: in this case, the ReadFile function correctly returns that it cannot communicate with the device; then, I want to close my application, calling the CloseHandle method. In this case, the CloseHandle method is blocking indefinitely and my application do not close. However, when I re-plugg the RJ45 cable, the CloseHandle method finishes and my application is able to close.
Something interesting is that I do not have the same issue when I unplug directly the USB port: in this case, the CloseHandle method works fine and my application closes.
Is there something in the communication buffer to clear in that case? (I have heard about the EscapeCommFunction method with the DTR signal or RTS signal but I tried this without success!) Why is CloseHandle blocking in this case? And why don't I have the same issue when I unplugg the USB cable?
I am not an expert in USB/serial communications, so please excuse the vocabulary.