Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reopening serial port fails if not closed properly with CloseHandle

I am working with USB device on Windows that is seen as a virtual serial port. I can communicate with the device using CreateFile and ReadFile functions, but in some cases my application does not call CloseHandle (when my application in development crashes). After that all calls to CreateFile fail (ERROR_ACCESS_DENIED), and the only solution is to log in to my computer again. Is there any way to force closing the open handle (or reopening) programmatically?

like image 946
superg Avatar asked Sep 06 '25 12:09

superg


2 Answers

This is certainly not normal. Windows automatically closes any handles that are left open after the process terminates. This must be a flaw in your USB device driver, although it is hard to see how it could mess this up. The ones that emulate serial ports are however notoriously lousy. Well, nothing much you can do but hope for a driver update from the manufacturer. Or a device from another manufacturer.

like image 50
Hans Passant Avatar answered Sep 09 '25 13:09

Hans Passant


I agree with both previous posts.

  1. This is not a normal situation.
  2. Unplugging the USB device usually helps.

This problem is related to the glitches in the FTDI driver, which is responsible for implementing a virtual COM port. On the other hand those "glitches" are related to various malfunctions of the USB devices. (Of course this doesn't justify the FTDI driver).

BTW there're several other known problems with some FTDI drivers:

  • Sometimes call to CloseHandle just hangs the calling thread.
  • Sometimes also the application is still "visible" in the task manager, even after it's closed. Task manager can't terminate the application, and the debugger can't be attached to it. Its EXE file is locked (can't be erased).

Usually unplugging the USB device immediately helps in those situations. The FTDI driver, which seems to be "waiting for something" awakes.

like image 23
valdo Avatar answered Sep 09 '25 12:09

valdo