在 C# 中发现串口

发布于 2024-12-23 03:19:52 字数 250 浏览 4 评论 0 原文

我正在编写一个通过向 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夏尔 2024-12-30 03:19:52

您可以使用管理对象来查询可用的 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.

╰沐子 2024-12-30 03:19:52

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.

远山浅 2024-12-30 03:19:52

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文