Author Topic: C#: The correct way of detecting a USB has been plugged in?  (Read 8407 times)

goodnewsjim

  • Frequent Contributor
  • ****
  • Posts: 52
C#: The correct way of detecting a USB has been plugged in?
« on: December 09, 2013, 03:05:31 pm »
Hello,

In code provided, there is a function called:
internal Boolean FindDeviceFromGuid(System.Guid myGuid, ref String devicePathName);

In it, you can find if new USB connections are being made.

I was thinking of having a main loop that checked this function every 3 seconds.  But I wonder is that the proper way to detect if a USB connection has been made?  Maybe there's an event that gets called when one gets plugged in that I am not aware of?

Some context:
1) Jan's code and my microchip code are working together and communicating nicely.
2) I'm adding new boards with different serial numbers.
3) I can detect the serial numbers on the boards and communicate to the right board.

What I am trying to do is make it so people can hot plug in new boards(same vendorid, same productid, dif serialid), and their serial numbers to be populated in a drop down menu(The guy with the arrow).  Right now the list can't populate until I previously detected the USB connection has been made.  It is a real simple problem which I can fix myself by calling FindDeviceFromGuid every 3 seconds.  But I was just wondering if I'm solving this problem in the incorrect fashion.  What is the right way to do this?

,Jim

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: C#: The correct way of detecting a USB has been plugged in?
« Reply #1 on: December 09, 2013, 09:51:43 pm »
In the example code, take a look at:

AddDeviceArrivedHandler

AddDeviceRemovedHandler

goodnewsjim

  • Frequent Contributor
  • ****
  • Posts: 52
Re: C#: The correct way of detecting a USB has been plugged in?
« Reply #2 on: December 10, 2013, 12:48:20 pm »
Hello Jan,

Thank you for the information.  I have one more question.

Do you happen to remember where that example code is located at?

(on http://www.lvr.com/winusb.htm) I tried:
1)winusb_cs v2.1 for Visual C# .NET. Created with Visual Studio 2012 for the .NET Framework V4.0 or later. Uses WMI to detect device arrival and removal. Updated 10/18/13.

2)winusb_cs for Visual C# .NET v2.0. Created with Visual Studio 2012 for the .NET Framework V4.0 or later. Updated 5/3/12.

3)winusb_cs for Visual C# .NET. Created with Visual Studio 2008 for the .NET Framework V2.0 or later. Updated 2/11/09.

4)WinUSB Component for .Net. For bulk transfers. From Asaf Shelly.

Are there more examples than what is on that page?  I also googled: AddDeviceArrivedHandler with no matches.

Thanks again, definitely getting your book. (edit just got it through a reference on your site :)
,Jim
« Last Edit: December 10, 2013, 12:52:12 pm by goodnewsjim »

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: C#: The correct way of detecting a USB has been plugged in?
« Reply #3 on: December 10, 2013, 12:53:48 pm »
The newest version

http://www.janaxelson.com/winusb.htm

winusb_cs v2.1 for Visual C# .NET. Created with Visual Studio 2012 for the .NET Framework V4.0 or later. Uses WMI to detect device arrival and removal. Updated 10/18/13.
« Last Edit: July 13, 2014, 09:58:00 pm by Jan Axelson »

goodnewsjim

  • Frequent Contributor
  • ****
  • Posts: 52
Re: C#: The correct way of detecting a USB has been plugged in?
« Reply #4 on: December 10, 2013, 01:10:08 pm »
Ah, thanks Jan,

You know what the problem is?  I downloaded it and opened it with firefox's temporary directories.  Apparently it didn't recognize all the files, so when I did a search for: AddDeviceArrivedHandler, nothing came up.

I'm good to go.

,Jim