Newsgroups : Borland : borland.public.delphi.nativeapi.win32 : 2007 Jun : How to detect when COM port is about to be disabled?

www.cryer.info
Managed Newsgroup Archive

How to detect when COM port is about to be disabled?

Subject:How to detect when COM port is about to be disabled?
Posted by:"Ivo Bauer" (abu..@zom.zc)
Date:Fri, 8 Jun 2007 10:18:51

Hi,

I have a Prolific USB-to-Serial (RS-232) converter that allows me to
use an USB slot as a virtual COM port. I installed the driver that
comes with the hardware and it works just fine.

However, when my application has this virtual COM port open and
someone pulls the cable out of the USB slot, this virtual port gets
disabled by Windows and is no longer accessible. Any subsequent
attempt to call Win32 API routine that requires a valid COM port file
handle fails with GetLastError() returning ERROR_FILE_NOT_FOUND (code 2).

Here are some questions that I'd appreciate to get answered:

1) Is it possible to detect (be notified) when this virtual COM port
is about to be disabled by Windows so that I get a chance to set a
flag in my code that the serial connection is no longer active? If so,
how?

2) When the converter cable is pulled out of the USB slot, I can
usually recover from this issue by closing and reopening the port
again. But this doesn't work always - sometimes CreateFile() API call
fails with ERROR_GEN_FAILURE (code 31) - the COM port cannot be opened
anymore and the only solution to make it work again is to restart the
Windows. What can I do in order to recover from this error? Or should
I give up and display a warning message to the user that the Windows
needs to be restarted?

Thanks a lot in advance!


--
Ivo Bauer [OZM Research]

Replies:

www.cryer.info
Managed Newsgroup Archive