ni.com is currently undergoing scheduled maintenance.

Some services may be unavailable at this time. Please contact us for help or try again later.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Detecting USB device using .NET connectivity

Hello. I want to recieve an event notification when the USB device is plug in or unplugged from the system. There is a special System.Management assembly which allows to catch system messages (also notifications from USB controller). I've tried to rebuilt some easy C# examples on LV using .NET event callback chain, it seems I'm missing something (See attached VI's). 

On code example:

using System;
using System.Management;
using System.Windows.Forms;

namespace WMISample
{
  public class WMIReceiveEvent
  {
  public static void Main()
  {
  try
  {
  WqlEventQuery query = new WqlEventQuery(
  "SELECT * FROM __InstanceCreationEvent WITHIN 10 WHERE TargetInstance ISA 'Win32_USBControllerDevice'");

  ManagementEventWatcher watcher = new ManagementEventWatcher(query);
  Console.WriteLine("Waiting for an event...");

  ManagementBaseObject eventObj = watcher.WaitForNextEvent();

  Console.WriteLine("{0} event occurred.", eventObj["__CLASS"]);

  // Cancel the event subscription
  watcher.Stop();
  return;
  }
  catch(ManagementException err)
  {
  MessageBox.Show("An error occurred while trying to receive an event: " + err.Message);
  }
  }
  }
}


Also here is some useful links:
http://dotnetslackers.com/community/blogs/basharkokash/archive/2008/03/15/USB-Detection-source-code.aspx 
http://www.developerfusion.com/forum/thread/53237/

Any help will be appreciated.

Best Regards, Konstantin.

Download All
0 Kudos
Message 1 of 6
(5,429 Views)

A search of the NI forums for "USB Event notification" returned an entry that may help you:

 

Windows device event notification

 

 

 

Message Edited by Phillip Brooks on 12-01-2008 06:49 AM
Message 2 of 6
(5,412 Views)

I couldn't look at your code initially, I usually work on a LabVIEW 7.0 machine.

 

You may want to look at the .NET PowerModeChanged Notifier example I coded some time ago.  It's been used by others, and should help you understand how to capture .NET events and convert them to LabVIEW events...

Message 3 of 6
(5,400 Views)

Hi there

 

.NET events and LabVIEW aren't working together in most cases. I redraw your C# code, seems to work (see attachment). You have to wait a few seconds for the event to arrive.

Best regards
chris

CL(A)Dly bending G-Force with LabVIEW

famous last words: "oh my god, it is full of stars!"
0 Kudos
Message 4 of 6
(5,358 Views)

Hi,chrisger, thanks for help. Could you resave the VI into 8.2 version? About .NET events: may be there are some problems with delegating from LV side etc.

0 Kudos
Message 5 of 6
(5,348 Views)

there you are...

 

 

Best regards
chris

CL(A)Dly bending G-Force with LabVIEW

famous last words: "oh my god, it is full of stars!"
0 Kudos
Message 6 of 6
(5,346 Views)