我可以在不实际打开 COM 端口的情况下检查是否可以打开它吗?

发布于 2024-07-24 03:34:51 字数 317 浏览 4 评论 0原文

通常,在 PC 上,有一些非标准的 COM 端口集可供终端仿真器使用。 我希望能够扫描端口 COM1-COM32 以查看哪些端口(如果有)可以作为串行端口打开。 我只需在所有这些端口上调用 CreateFile() 即可做到这一点; 然而,我实际上必须暂时打开/关闭(并影响 DTR/RTS)端口。

我想向用户提供可用端口的准确列表。 问题是我不想影响当前未使用的端口的 DTR/RTS 线路,因为可能有连接到这些端口的硬件不希望看到 DTR/RTS 上的任何转换。

有没有办法只问一个问题:“CreateFile() 会成功吗?” 在指定的 com 端口上?

Typically, on a PC, there is some non-standard set of COM ports available for use by a terminal emulator. I would like to be able to scan ports COM1-COM32 to see which (if any) of those ports can be opened as a serial port. I can do this be simply calling CreateFile() on all those ports; however, then I actually have to open/close (and affect DTR/RTS) the port momentarily.

I want to provide an accurate list to the user of the ports that are available. The problem is that I don't want to affect the DTR/RTS lines of the ports that are not currently being used because there may be hardware connected to those ports that does not want to see any transition on DTR/RTS.

Is there a way to just ask the question: "Will CreateFile() succeed?" on specified com ports?

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

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

发布评论

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

评论(3

栀梦 2024-07-31 03:34:51

不,这样的问题是没办法问的。 无论如何,答案没有任何意义。 即使操作系统确定打开 COM 端口在某个时间点会成功,但这并不意味着您可以稍后打开它。 (例如,它可能会被另一个应用程序打开。)您可以使用SetupDiXXX函数来枚举系统中的COM端口,但这实际上只是返回有关已安装和活动驱动程序的信息。 除此之外,它不提供任何保证。

No, there is no way to ask such a question. The answer wouldn't be meaningful anyway. Even if the OS determined that opening a COM port will succeed at some point in time, that doesn't mean you can open it later. (It might get opened by another application, for instance.) You can use the SetupDiXXX functions to enumerate the COM ports in the system, but this really just returns information about the installed and active drivers. It doesn't provide any guarantees beyond that.

み零 2024-07-31 03:34:51

几种方法

a) QueryDosDevice() - 检查名称中的 COM

b) GetDefaultCommConfig() - 遍历所有可能的序列名称,例如。 尝试获取 COM1、COM2、... COMn 的配置

c) HKLM/HARDWARE/DEVICEMAP/SERIALCOMM - 枚举键

d) SetupAPI - 如果我在不广泛查阅参考手册的情况下记住如何使用它,我就不是人类

了)???

编辑:正如彼得所指出的,除了枚举之外,没有任何功能可以告诉您是否可以访问该设备。

Several ways

a) QueryDosDevice() - check against COM in the names

b) GetDefaultCommConfig() - Go thru all the possible serial names, eg. try to get config for COM1, COM2, ... COMn

c) HKLM/HARDWARE/DEVICEMAP/SERIALCOMM - Enumerate the keys

d) SetupAPI - if I would remember how to use this without extensively consulting the reference manual I would not be human

e) ???

Edited: as noted by Peter, beyond enumeration, there is no function that can tell you if you can access the device or not.

不及他 2024-07-31 03:34:51

HKLM/硬件/DEVICEMAP/SERIALCOMM 是我的最爱。

在我看来,该解决方案显示了最佳的实施简易性与准确率。

尽管我见过至少一家 USB 转串行端口制造商,其硬件并不总是列在其中(FTDI IIRC),但在大多数情况下它足够准确。

HKLM/HARDWARE/DEVICEMAP/SERIALCOMM is my favorite.

this solution shows the best ease of implementation vs accuracy ratio IMO.

It's accurate enough in most cases although I've seen at least one manufacturer of USB to Serial ports whose HW doesn't always list in there (FTDI IIRC).

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