LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

How do I handle loss of USB connection in CVI?

Hello,
 
We use CVI for our GUIs and we make our devices in-house.  We have a 3rd party driver to create a virtual serial port for USB connections.  Problem: when the connection is lost, my GUI freezes up because the virtual serial port is destroyed by the OS (Win XP) and my GUI is still trying to use the comport created when the USB was connected.  Is there a function I can use in CVI to poll and notify me when this serial port is destroyed by the OS?  I've been trying GetComStat and I get an XOFF value (0x1000) when the connection is lost, but I have handshaking off in my software and I'm surprised I get this response.  I think this is what I need to look for to stop my timers and inform the user that the connection was lost, but I'm not sure.  For now, I'm trying it.  However, how can I recognize when the USB device is reconnected?
 
Thanks,
 
Judy
0 Kudos
Message 1 of 4
(3,123 Views)
You could have a function which is triggered periodically by a timer (once a second?), to check the COM status. If the port is not already open (I'm assuming here that a 'destroyed' port reads as closed, not open), then it could try to open it - when the USB is plugged back in this will succeed and the function can pass the relevant information back to your main program.
 
JR
0 Kudos
Message 2 of 4
(3,122 Views)

Hi Judy,

I have only used this in XP.

You will have to poll for this, I know of no way to make it cause a callback.

If your USB device is assigned COM6 for example, then search the registry for "COM6".

On my PC it's HKLM_HARDWARE_DEVICEMAP_SERIALCOMM device/Serial2 COM6

The entry for the part is only present when the device is. I.E. When the virtual com port is unplugged, Windows removes the registery entry.

The Programmers Toolbox has functions for accesssing the registery. You can use a timer to poll or just call it on a coms error.

I use this technique to find USB virtual com ports as finding the number Windows assigns is a pain.

 

Hope that helps,

John

John C
0 Kudos
Message 3 of 4
(3,114 Views)
My advice would be to drop the USB to COM conversion and write directly the
USB driver with either VISA or (better) LibUsb, which will also compile on
Linux.
Unless you do strange async transfers, it's no harder to write a USB driver
than a serial one and you get many more advantages.
--
Guillaume Dargaud
http://www.gdargaud.net/
"You can go anywhere you want if you look serious and carry a clipboard."


0 Kudos
Message 4 of 4
(3,077 Views)