Example Code

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

Products and Environment

This section reflects the products and operating system used to create the example.

To download NI software, including the products shown below, visit ni.com/downloads.

    Hardware

  • Other

    Software

  • LabVIEW

Code and Documents

Attachment

Overview:

This VI monitors the connection to any USB device, be that a flash memory stick, a keyboard, a web camera,etc.. using Windows Registry

 

 

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 2012 (or compatible)

 

 

Steps to Implement or Execute Code:

Identify the device you wish to monitor for:

1. Plug a USB device into the system.
2. Open 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 last "\" is usually sufficient.
4. Or Open the windows registry (type "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.
9. Enter the device ID in the Device ID Control (similar to the default value of the control)
10.Run the VI

 

 

 

Additional Information or References:

Block Diagram

 

 

BD.PNG 

 

 

 

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

------
John.P | Certified LabVIEW Architect | NI Alliance Member

Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.

Comments
Frabto
Member
Member
on

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

John.P
Member
Member
on

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

------
John.P | Certified LabVIEW Architect | NI Alliance Member
碧海蓝天
Member
Member
on

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

I think  make a program with AUTO Download Mp3.

Steve_A.
Member
Member
on

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

John.P
Member
Member
on

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.

------
John.P | Certified LabVIEW Architect | NI Alliance Member
04RC51
Member
Member
on

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.

Slim60
Member
Member
on

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

Slim60
Member
Member
on

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.

DeepSpace
Member
Member
on

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.

04RC51
Member
Member
on

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.

DeepSpace
Member
Member
on

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

BenRoura
Member
Member
on

Just to say thx!