473,418 Members | 2,039 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,418 software developers and data experts.

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

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.
Sep 9 '09 #1
3 26710
madankarmukta
308 256MB
@Charming12
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!
Sep 9 '09 #2
Plater
7,872 Expert 4TB
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:
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
Sep 9 '09 #3
Thanks a lot,

I will give it a try, and will surely comeback if it works
Sep 9 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: joe bloggs | last post by:
I am writing a mobile application to interface with a legacy system and I am planning to use web services to communicate with this system. The legacy system receives data through a serial port. ...
1
by: Jan | last post by:
Hello, is there someone who know's how to read and write data to a serial port using visualbasic 8 thanx in advance
0
by: pandi | last post by:
Hi, I am using a check scanner.It has functions which gives output like image ,checknumber. i am using serial port to get this output in my coding. My problem is i am not able to read the display...
0
by: usagimys | last post by:
Hi all, i'm very new in serial port communication.. here i got some problem in writing to the serial port.. i have done the connection successfully.. let me explain my situation.. i'm doing a system...
1
by: Jan | last post by:
Hello, I want to send some simple strings to com1 using vb2005. I want to read it in Hyperterminal. I succeeded to send some data en read it in Hyperterminal, only the data I received is not...
0
by: ghjk | last post by:
I want to read sms from GSM modem using C# in serial communication. I wrote the code. But i want to do it automatically. I put my code here and please tell me how can i do it automatically. ...
0
by: Dhananjay | last post by:
Hi, I am working on an VB.Net application which I want to communicate to external device using comm port (Serial Port) . So for that first I am trying to simulate the communication on serial...
1
by: lutherchp | last post by:
A baffling end to my week! I open my serial port on my Debian PC (Debian version 5.0.1) I have a decent Null Modem lead going to another PC (I'll call it PC#2), with its port open with the same...
4
by: mayaanu | last post by:
I have developed a GUI based application in C# for communicating with a 16 bit littleendian microcontroller device .the device has a serial port interface. 1.my appplication opens a COM port on...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.