本文介绍了如何获得 Win32 中可用串行端口的列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些遗留代码,通过调用 EnumPorts() 函数,然后过滤以COM"开头的端口名称.

I have some legacy code that provides a list of the available COM ports on the PC by calling the EnumPorts() function and then filtering for the port names that start with "COM".

出于测试目的,如果我可以将此代码与 com0com 之类的内容一起使用,那将非常有用,它提供成对的虚拟 COM 端口作为空调制解调器循环在一起.

For testing purposes it would be very useful if I could use this code with something like com0com, which provides pairs of virtual COM ports looped together as a null-modem.

然而,EnumPorts() 函数找不到 com0com 端口(即使没有过滤COM").HyperTerminal 和 SysInternals PortMon 都可以看到它们,所以我确定它安装正确.

However the com0com ports are not found by the EnumPorts() function (even without filtering for "COM"). HyperTerminal and SysInternals PortMon can both see them, so I'm sure it is installed correctly.

那么是否还有其他一些 Win32 函数可以提供可用串行端口的明确列表?

推荐答案

EnumSerialPorts v1.20 Nick D 建议使用九种 不同的方法来列出串口!我们当然不会缺少选择,尽管结果似乎各不相同.

The EnumSerialPorts v1.20 suggested by Nick D uses nine different methods to list the serial ports! We're certainly not short on choice, though the results seem to vary.

为了省去其他人的麻烦,我将在此处列出它们并指出它们在我的 PC (XP Pro SP2) 上成功找到了 com0com 端口:

To save others the trouble, I'll list them here and indicate their success in finding the com0com ports on my PC (XP Pro SP2):

  1. CreateFile("COM" + 1->255) 由 Wael Dalloul 建议
    ✔ 找到 com0com 端口,耗时 234 毫秒.

  1. CreateFile("COM" + 1->255) as suggested by Wael Dalloul
    ✔ Found com0com ports, took 234ms.

QueryDosDevice()
✔ 找到 com0com 端口,耗时 0 毫秒.

QueryDosDevice()
✔ Found com0com ports, took 0ms.

GetDefaultCommConfig("COM" + 1->255)
✔ 找到 com0com 端口,耗时 235 毫秒.

GetDefaultCommConfig("COM" + 1->255)
✔ Found com0com ports, took 235ms.

SetupAPI1"使用对 SETUPAPI.DLL 的调用
✔ 找到com0com端口,也报友好名称",耗时15ms.

"SetupAPI1" using calls to SETUPAPI.DLL
✔ Found com0com ports, also reported "friendly names", took 15ms.

SetupAPI2"使用对 SETUPAPI.DLL 的调用
✘ 没有找到com0com端口,报友好名称",耗时32ms.

"SetupAPI2" using calls to SETUPAPI.DLL
✘ Did not find com0com ports, reported "friendly names", took 32ms.

EnumPorts()
✘ 报一些非COM端口,没有找到com0com端口,耗时15ms.

EnumPorts()
✘ Reported some non-COM ports, did not find com0com ports, took 15ms.

使用 WMI 调用
✔ 找到com0com端口,也报友好名称",耗时47ms.

Using WMI calls
✔ Found com0com ports, also reported "friendly names", took 47ms.

使用 MSPORTS.DLL 调用的 COM 数据库
✔/✘ 报一些非COM端口,找到com0com端口,耗时16ms.

COM Database using calls to MSPORTS.DLL
✔/✘ Reported some non-COM ports, found com0com ports, took 16ms.

迭代注册表项 HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM
✔ 找到 com0com 端口,耗时 0 毫秒.这显然是 SysInternals PortMon 使用的内容.

基于这些结果,我认为 WMI 方法可能最适合我的要求,因为它相对较快,而且作为奖励,它还提供了友好名称(例如通信端口 (COM1)"、com0com - 串行端口模拟器").

Based on those results I think the WMI method probably suits my requirements best as it is relatively fast and as a bonus it also gives the friendly names (e.g. "Communications Port (COM1)", "com0com - serial port emulator").

这篇关于如何获得 Win32 中可用串行端口的列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 00:15