7

In a C# application, what are some good ways to detect the presence of certain device connected to the computer? Programatically, of course. I need to support Windows XP and 7.

Background

I've been given a huge C# application that uses some connected device. The devices are custom hardware and are not designed to use without the software. When the C# app starts, it searches for such connected devices and communicates with them when appropriate. There can be more than one connected devices. I haven't looked into the source code that searches the devices. The devices are plugged into the PC with either USB or serial cable. They appear as COM port in device manager. Sometimes there can be USB hub or serial to USB converters in between the PC and the device.

When the device is connected with certain USB hub, or certain serial to USB converter is used, sometimes the software cannot detect the device properly. Sometimes it varies from PC to PC. We're not sure if it causes for some driver.

I'm told to look into the device searching algorithm and if possible, come up with better solution and replace current implementation. I haven't done this sort of task before and I need to learn how Windows manage the devices under the hood.

How can I search for specific connected devices in C#? What are some good practice and what I should be aware of or avoid?

Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
Donotalo
  • 12,748
  • 25
  • 83
  • 121
  • Maybe this article is interresting : http://stackoverflow.com/questions/68749/working-with-usb-devices-in-net and http://www.developerfusion.com/article/84338/making-usb-c-friendly/ – Ruben Dec 29 '11 at 09:48
  • You can enumerate all connected (usb) devices using WMI or such. They're pretty slow to work with as far as I can remember. There could be better ways though. http://stackoverflow.com/questions/3331043/get-list-of-connected-usb-devices – CodingBarfield Dec 29 '11 at 10:22

1 Answers1

3

You can find all devices in registry: HKLM/CurrentControlSet/Enum ( http://msdn.microsoft.com/en-us/library/windows/hardware/ff546173(v=vs.85).aspx )

But there will be problem with serial port. You can't be certain in which device is connected to a serial port. It can be mobile phone, modem, mouse, your device, another custom device, etc. To guarantee that it is your device connected to a given serial port you must try to communicate with it. And if it responds in proper way - then yes, it is your device.

Because of this many programs that works with serial devices asks user to manually select serial port, where device is connected.

UPD:

Another link about enumerating serial ports: http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/afb62e84-53e5-4f17-ba91-8de15c4c2e38/

werewindle
  • 3,009
  • 17
  • 27
  • the link you provided is for windows CE. can you provide a link for windows xp/7? thanks. i couldn't find it in msdn by a quick search. – Donotalo Dec 29 '11 at 11:01