Connecting Tech Pros Worldwide Help | Site Map

How to detect if a device is connected to PC using USB or Serial Port?

Newbie
 
Join Date: Sep 2008
Posts: 18
#1: Sep 9 '09
Hi All,

I have to deal with some devices like Pen Tablet, WebCam etc to work for a product.
Now my Problem is some times while working , I am not able to tell whether a certain device is connected to PC .

So , Is there any way in C# where i can detect if device is connected using USB port or serial port.
Any help will be useful.

Thanks in Advance
Eric.
Familiar Sight
 
Join Date: Apr 2008
Posts: 148
#2: Sep 9 '09

re: How to detect if a device is connected to PC using USB or Serial Port?


Quote:

Originally Posted by Charming12 View Post

Hi All,

I have to deal with some devices like Pen Tablet, WebCam etc to work for a product.
Now my Problem is some times while working , I am not able to tell whether a certain device is connected to PC .

So , Is there any way in C# where i can detect if device is connected using USB port or serial port.
Any help will be useful.

Thanks in Advance
Eric.

It seems that you need to use the System.Runtime.InteropServices namespace to come to the result.The solution needs to make use of the unmanaged code.

Please refer the link below
http://stackoverflow.com/questions/2...n-is-connected

Thanks!
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,150
#3: Sep 9 '09

re: How to detect if a device is connected to PC using USB or Serial Port?


Do you want to know if the device is attached via serial OR usb, or you want to find any devices attached to either port type?
You can probably accomplish that using something from the WMI libraries.
Look for something called "WMICodeCreator", its really usefull for playing around and seeing what you can find.

For example using WIn32_PnPEntity it made this code:
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Management;
  3. using System.Windows.Forms;
  4.  
  5. namespace WMISample
  6. {
  7.     public class MyWMIQuery
  8.     {
  9.         public static void Main()
  10.         {
  11.             try
  12.             {
  13.                 ManagementObjectSearcher searcher = 
  14.                     new ManagementObjectSearcher("root\\CIMV2", 
  15.                     "SELECT * FROM Win32_PnPEntity"); 
  16.  
  17.                 foreach (ManagementObject queryObj in searcher.Get())
  18.                 {
  19.                     Console.WriteLine("-----------------------------------");
  20.                     Console.WriteLine("Win32_PnPEntity instance");
  21.                     Console.WriteLine("-----------------------------------");
  22.                     Console.WriteLine("Caption: {0}", queryObj["Caption"]);
  23.                     Console.WriteLine("Description: {0}", queryObj["Description"]);
  24.                     Console.WriteLine("DeviceID: {0}", queryObj["DeviceID"]);
  25.                 }
  26.             }
  27.             catch (ManagementException e)
  28.             {
  29.                 MessageBox.Show("An error occurred while querying for WMI data: " + e.Message);
  30.             }
  31.         }
  32.     }
  33. }
  34.  
Producing something like:
Quote:
Win32_PnPEntity instance
-----------------------------------
Caption: 802.11n USB Wireless LAN Card - Deterministic Network Enhancer Miniport

Description: Deterministic Network Enhancer Miniport
DeviceID: ROOT\DNI_DNEMP\0007
-----------------------------------
Win32_PnPEntity instance
-----------------------------------
Caption: 802.11n USB Wireless LAN Card - Virtual Machine Network Services Driver

Description: Virtual Machine Network Services Driver
DeviceID: ROOT\CNTX_VPCNETS2_MP\0004
-----------------------------------
Win32_PnPEntity instance
-----------------------------------
Caption: 802.11n USB Wireless LAN Card - Packet Scheduler Miniport
Description: Packet Scheduler Miniport
DeviceID: ROOT\MS_PSCHEDMP\0007
Newbie
 
Join Date: Sep 2008
Posts: 18
#4: Sep 9 '09

re: How to detect if a device is connected to PC using USB or Serial Port?


Thanks a lot,

I will give it a try, and will surely comeback if it works
Reply


Similar .NET Framework bytes