如何检测特定设备的存在
在 C# 应用程序中,有哪些好方法可以检测连接到计算机的特定设备是否存在?当然,以编程方式。我需要支持 Windows XP 和 7。
背景
我得到了一个使用某些连接设备的巨大 C# 应用程序。这些设备是定制硬件,不能在没有软件的情况下使用。当 C# 应用程序启动时,它会搜索此类连接的设备并在适当时与它们通信。可以有多个连接的设备。我还没有研究过搜索设备的源代码。这些设备通过 USB 或串行电缆插入 PC。它们在设备管理器中显示为 COM 端口。有时,PC 和设备之间可能存在 USB 集线器或串行 USB 转换器。
当设备连接某些USB集线器,或者使用某些串口转USB转换器时,有时软件无法正确检测到设备。有时,它会因电脑而异。我们不确定这是否会导致某些司机。
我被告知要研究设备搜索算法,如果可能的话,提出更好的解决方案并替换当前的实现。我以前从未完成过此类任务,我需要了解 Windows 如何在幕后管理设备。
如何在 C# 中搜索特定的连接设备?有哪些好的做法以及我应该注意或避免的内容?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在注册表中找到所有设备:HKLM/CurrentControlSet/Enum ( http://msdn.microsoft.com/en-us/library/windows/hardware/ff546173(v=vs.85).aspx )
但是串口会有问题。您无法确定哪个设备连接到串行端口。它可以是移动电话、调制解调器、鼠标、您的设备、另一个自定义设备等。为了保证它是连接到给定串行端口的您的设备,您必须尝试与其通信。如果它以正确的方式响应 - 那么是的,它就是您的设备。
因此,许多与串行设备配合使用的程序要求用户手动选择设备连接的串行端口。
UPD:
关于枚举串行端口的另一个链接:http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/afb62e84-53e5-4f17-ba91-8de15c4c2e38/
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/