python:如何检测串行COM上的设备名称/ID [英] python : How to detect device name/id on a serial COM
问题描述
我想知道如何在 python 中执行此操作:
I would like some indication on how to do this in python:
在串行 com 中识别以特定名称命名的端口(\Device\VCP0 和 \Device\VCP1 这些是通过在 regedit 窗口中浏览获得的)
Identify the port named a specific name in the serial com (\Device\VCP0 and \Device\VCP1 these are get by browsing in regedit window)
并获取插入的设备的id
And get the id of the device that is pluged
我已经可以用这个 pySerial 代码识别可用的 COM,它扫描活动的串行端口 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
这篇关于python:如何检测串行COM上的设备名称/ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!