Example Programs

Showing results for 
Search instead for 
Did you mean: 

Check if a USB Device is Connected to the System - Windows Registry

by Member John.P on ‎09-14-2009 02:58 AM

Overview

This code allows you to check if any USB device is connected to the system.

Description

There are no native LabVIEW functions to do this and the VI needed to be able to monitor for ANY USB device, be that a flash memory stick, a keyboard, a web camera.

The information needed is all stored in the Windows Registry so as well as being a useful VI this is also an example of accessing the registry.

The main issue with this is different USB devices are stored in different registry keys.  For example all the devices that appear as storage devices are under the usbSTOR key, HCI devices such as the keyboard appear under the usbehci key etc.

The next issue is that if you have never plugged any device of a certain type into your system, the key will simply not exist.  This code accounts for both issues.  It allows us to add registry keys to search, and will handle any errors generated by keys not being found.

Requirements

LabVIEW 8.0 (or compatible)

Steps to Implement or Execute Code

Identify the device you wish to monitor for:

1. Plug it into the system.

2. Bring up the Windows Device Manager and locate the USB device.

3. Under the details tab Note down the device instance ID, this varies from device to device but the last part after the final "\" is usually sufficient.

4. Bring up the windows registry (regedit in the run menu).

5. Navigate to HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services.

6. Locate the device ID in one of the USB registry keys ( device must be connected,  might be easier to search for it).

7. Note down the subkey (folder) the device is stored in.  By default the VI uses USBSTOR, usbccgp, usbehci, usbaudio.

8. If the device falls under one of these subkeys you are ready to go.  If not add the sub key to the paths in the VI.

Enter the Device ID into the VI and run it, the VI will check if the device is present and return a boolean.  The VI is setup ready as a sub VI.

Additional Information or References

USB Device Detect.png

Notes

Saved in 8.0, will open correctly in any later version.

I would be very pleased with feedback, if anybody has a better method of achieving this I would be interested in hearing.

All ideas are welcome providing they only use code that is free to distribute.

**This document has been updated to mee the current required format for the NI Code Exchange. **

Comments
Member Frabto
Member

Another Method,

Use Devcon.exe to via LabVIEW command prompt to get any parameters from Device manager.

http://support.microsoft.com/kb/311272

Ref:http://forums.ni.com/ni/board/message?board.id=140&message.id=32878&requireLogin=False

Member John.P
Member

Hey Niju,

Thanks for the feedback, I have come across Devcon and it can provide the functionality.  However Microsoft state it is not re-distributable so it would be unsuitable for most applications.

John

Member 碧海蓝天
Member

How can I perform Auto check remove disk and Download a file from PC!

I think  make a program with AUTO Download Mp3.

Member Steve_A.
Member

I believe this could be helpful...

However, I do not see any code to download! I've been able to copy everything except for the text block array looking thing.

If someone could explain how to create that array, or show where the code is, that would be much appreciated.

-Steve

Member John.P
Member

Hi Steve,

Not sure why the code was not attached before.

If you have LabVIEW 2009 or later you can import the png directly onto the block diagram.

Member 04RC51
Member

This VI worked nicely for me (WinXP SP3 & LV2011SP1), though I found the Match Regular Expression function will error if I search for the entire device ID (it didn't like the backslash '\' character, not sure why?); a partial key worked fine as suggested.  I also found that the registry subkey (folder) where the device ID was stored could be found in the Device Manager details tab under 'Services'.  Make sure to add this subkey (folder) to the array of subkey paths to search.

Member Slim60
Member

I am trying to get familiar with Devcon as I have never used it before. When I run any command listed in the devcon website the command line (cmd.exe) opens friefly and then closes without showing me the list of usb devices that I want to see.  When running it from labview using System Exec.vi, it seems to run but it doesn't report any output.  Can someone put me in the right direction for reading the devcon output ?

Thanks in advance

Member Slim60
Member

Does Scan Registry for USB Device.vi work with windows7 as I tried it and found my usb device in the following path system\CurrentControlSet\Control\Class\{3F966BD9-FA04-4EC5-991C-D326973B73B5128}.

I am typing the path literally.  I entered the path system\CurrentControlSet\Control\Class\ as the last element of the input array and entered 3F966BD9-FA04-4EC5-991C-D326973B73B5128

in the Device ID to Check if Present control and it didn't work. I then entered the same subkey as {3F966BD9-FA04-4EC5-991C-D326973B73B5128} as it shows and it didn't work.

Can someone let me know what I am missing.

Member DeepSpace
Member

HI,

Could you please tell me how the sub key paths are constructed, and also what data in the registry is being searched for?

Thanks so much.

Member 04RC51
Member

To give you an example, in the ‘Subkey Path to Search’ string control of the Scan Registry for USB Device.vi, I use the path: “System\\CurrentControlSet\\Services\\usbser\\enum“ (without quotes); and for the ‘Device ID to Check if Present’, I just use just the last few (unique) characters of the Device Instance ID, which for my particular device is “1.3.X”.

Your path may very well be different from mine, and note that I found that using the full Device Instance ID to be less reliable than using a partial string when finding my device.

Member DeepSpace
Member

Thank you for the response. That was very helpful. Have a great day.

Member BenRoura
Member

Just to say thx!