问题描述
我在寻找一个可靠的方法列出Windows计算机上的可用串口(COM)端口。有的,但我想东西少.NET具体 - 我想在Python或C ++程序的端口列表,没有.NET
。我目前所知道的另外两种方法:
-
阅读在
信息HARDWARE \\\\ \\\\ DEVICEMAP SERIALCOMM
注册表项。这看起来像一个很好的选择,但是它的强大的?在网上或MSDN此注册表细胞确实是始终保持可用端口的完整列表,我找不到一个保证。 -
Tryint叫
的CreateFile
在COMN
带有n的数量从1到的东西。这是不够的,因为有些COM端口没有被命名COMN。例如,创建了一些虚拟COM端口被命名为CSNA0,CSNB0,等等,所以我不会靠这个方法。
任何其他的方法/创意/经验可以分享?
编辑:顺便说一句,这里有一个简单的Python实现从注册表中读取端口名称:
进口_winreg为WinReg项
进口和itertools
高清enumerate_serial_ports():
使用Win32注册表返回的串行迭代器
(COM)端口现有的这台计算机上。
路径='HARDWARE \\\\ \\\\ DEVICEMAP SERIALCOMM
尝试:
键= winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE,路径)
除了WindowsError:
提高IterationError 因为我在itertools.count():
尝试:
VAL = winreg.EnumValue(键,I)
收益率(STR(VAL [1]),STR(VAL [0]))
除了EnvironmentError:
打破
有几种选择:
-
用NULL lpDeviceName列出所有的DOS设备。然后,使用的CreateFile和每台设备的名称依次弄清楚无论它是一个串行端口。
-
SetupDiGetClassDevs 与ClassGuid GUID_DEVINTERFACE_COMPORT的。
-
和$ C $的CProject,呃,。
I'm looking for a robust way to list the available serial (COM) ports on a Windows machine. There's this post about using WMI, but I would like something less .NET specific - I want to get the list of ports in a Python or a C++ program, without .NET.
I currently know of two other approaches:
Reading the information in the
HARDWARE\\DEVICEMAP\\SERIALCOMM
registry key. This looks like a great option, but is it robust? I can't find a guarantee online or in MSDN that this registry cell indeed always holds the full list of available ports.Tryint to call
CreateFile
onCOMN
with N a number from 1 to something. This isn't good enough, because some COM ports aren't named COMN. For example, some virtual COM ports created are named CSNA0, CSNB0, and so on, so I wouldn't rely on this method.
Any other methods/ideas/experience to share?
Edit: by the way, here's a simple Python implementation of reading the port names from registry:
import _winreg as winreg import itertools def enumerate_serial_ports(): """ Uses the Win32 registry to return a iterator of serial (COM) ports existing on this computer. """ path = 'HARDWARE\\DEVICEMAP\\SERIALCOMM' try: key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, path) except WindowsError: raise IterationError for i in itertools.count(): try: val = winreg.EnumValue(key, i) yield (str(val[1]), str(val[0])) except EnvironmentError: break
解决方案Several options are available:
Call QueryDosDevice with a NULL lpDeviceName to list all DOS devices. Then use CreateFile and GetCommConfig with each device name in turn to figure out whether it's a serial port.
Call SetupDiGetClassDevs with a ClassGuid of GUID_DEVINTERFACE_COMPORT.
There's some conversation on the win32 newsgroup and a CodeProject, er, project.
这篇关于清单在Windows上串行(COM)端口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!