如何获取 Win32 中可用串行端口的列表? [英] How do I get a list of available serial ports in Win32?

查看:59
本文介绍了如何获取 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 端口,耗时 0ms.

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 端口,还报告友好名称",耗时 15 毫秒.

"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 端口,还报告友好名称",耗时 47 毫秒.

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_MACHINEHARDWAREDEVICEMAPSERIALCOMM
✔ 找到 com0com 端口,耗时 0ms.这显然是 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 中可用串行端口的列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆