我正在编写一个通过向 USB 端口发送字节进行通信的应用程序。我希望允许用户选择端口,为此,我使用 SerialPort.GetPortNames()
。不幸的是,它返回一个空数组。该方法据称会读取注册表中的数据(HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM),但 SERIALCOMM 目录丢失。当我在 Linux 上运行该程序时,它会按预期列出我的四个 USB 端口。如何以不同的方式修复注册表/发现端口?
I'm writing an application that communicates by sending bytes to the USB port. I'd like to allow the user to choose the port and to do that, I use SerialPort.GetPortNames()
. Unfortunately, it returns an empty array. The method supposedly reads the registry for data (HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM), but the SERIALCOMM directory is missing. When I run the program on Linux, it lists my four USB ports, as expected. How can I fix the registry/discover the ports in a different way?
发布评论
评论(3)
您可以使用管理对象来查询可用的 COM 端口。有趣的表是:
您可以在 WMI Code Creator,并生成 VBScript/VB/C# 代码。
You could use Management Objects to query available COM ports. The interesting tables are:
You can explore these namespaces in the WMI Code Creator, and generate VBScript/VB/C# code.
SerialPort.GetPortNames()
是发现串行端口的正确方法。但如果您使用的是 Windows 7,我认为这可能是此方法列出的注册表项的权限问题。我找到了一篇描述此问题的帖子 Microsoft.com。The
SerialPort.GetPortNames()
are the correct method to discover serial ports. But if you are using Windows 7, I think it could be issues with permissions on the registry keys this method lists. I found a post which describe this problem at Microsoft.com.SerialPort.GetPortNames()
返回计算机上安装的 COM 端口列表,而不是 USB 端口。我认为 Windows 不会为您提供 USB 端口列表,而只会为您提供可用的 USB 设备。尽管名称为“通用串行总线”,但它们并不是通常意义上的“串行端口”。
SerialPort.GetPortNames()
returns the list of COM ports install on the machine, not USB ports. I don't think Windows will provide you with a list of USB ports, only USB devices that are available.Despite the name "Universal Serial Bus", they are not "Serial Ports" in the convential meaning.