python:如何检测串行 COM 上的设备名称/ID

2022-01-18 00:00:00 python serial-port

问题描述

我想了解如何在 python 中执行此操作:

I would like some indication on how to do this in python:

  • 识别串口中指定名称的端口(DeviceVCP0和DeviceVCP1这些是通过在regedit窗口中浏览获得的)

  • Identify the port named a specific name in the serial com (DeviceVCP0 and DeviceVCP1 these are get by browsing in regedit window)

并获取插入设备的id

我已经可以使用扫描活动串行端口 COM 的 pySerial 代码识别可用的 COM

I can already identify the avalable COM with this pySerial code that scan up the active serial port COM

import serial

def scan():
    """scan for available ports. return a list of tuples (num, name)"""
    available = []
    for i in range(256):
        try:
            s = serial.Serial(i)
            available.append( (i, s.portstr))
            s.close()   # explicit close 'cause of delayed GC in java
        except serial.SerialException:
            pass
    return available

if __name__=='__main__':
    print "Found ports:"
    for n,s in scan():
        print "(%d) %s" % (n,s)

提前致谢


解决方案

我不确定你使用的是什么操作系统,但这是在 Win7-x64 中的

I am not sure what operating system you are using, but this is in Win7-x64

import win32com.client
wmi = win32com.client.GetObject("winmgmts:")
for serial in wmi.InstancesOf("Win32_SerialPort"):
       print (serial.Name, serial.Description)

使用此信息,您可以解析它并获取 COM 编号.您可以在此处获取 Serial 实例的其他属性:http://msdn.microsoft.com/en-us/library/aa394413(v=vs.85).aspx

Using this information, you can parse it and get the COM numbers. You can get other attributes of the Serial instances here: http://msdn.microsoft.com/en-us/library/aa394413(v=vs.85).aspx

相关文章