使用Python脚本查找USB串行端口
我正在尝试用python编写一个脚本,以便在1秒内找到插入笔记本电脑的USB串行适配器的COM号。 我需要的是隔离COMx端口,以便显示结果并使用该特定端口打开putty。你能帮我吗 到目前为止,我已经在batch/powershell中编写了一个脚本,我正在获取此信息,但我还无法分离COMx端口的文本,以便使用串行参数调用putty程序。 我还可以通过Python找到端口,但我无法将其与字符串分离使用Python脚本查找USB串行端口,python,windows,serial-port,usb,Python,Windows,Serial Port,Usb,我正在尝试用python编写一个脚本,以便在1秒内找到插入笔记本电脑的USB串行适配器的COM号。 我需要的是隔离COMx端口,以便显示结果并使用该特定端口打开putty。你能帮我吗 到目前为止,我已经在batch/powershell中编写了一个脚本,我正在获取此信息,但我还无法分离COMx端口的文本,以便使用串行参数调用putty程序。 我还可以通过Python找到端口,但我无法将其与字符串分离 import re # Used for regular expressio
import re # Used for regular expressions (unused)
import os # To check that the path of the files defined in the config file exist (unused)
import sys # To leave the script if (unused)
import numpy as np
from infi.devicemanager import DeviceManager
dm = DeviceManager()
dm.root.rescan()
devs = dm.all_devices
print ('Size of Devs: ',len(devs))
print ('Type of Devs: ',type(devs))
myarray = ([])
myarray =np.array(devs)
print ('Type of thing: ',type(myarray))
match = '<USB Serial Port (COM6)>' (custom match. the ideal would be "USB Serial Port")
i=0
#print (myarray, '\n')
while i != len(devs):
if match == myarray[i]:
print ('Found it!')
break
print ('array: ',i," : ", myarray[i])
i = i+1
print ('array 49: ', myarray[49]) (here I was checking what is the difference of the "element" inside the array)
print ('match : ', match) (and what is the difference of what I submitted)
print ('end')
import re#用于正则表达式(未使用)
导入os#以检查配置文件中定义的文件路径是否存在(未使用)
导入系统#以保留脚本(如果未使用)
将numpy作为np导入
从infi.devicemanager导入devicemanager
dm=设备管理器()
dm.root.rescan()
devs=dm.all\u设备
打印('Devs的大小:',len(Devs))
打印('开发人员类型:',类型(开发人员))
myarray=([])
myarray=np.array(开发人员)
打印('对象类型:',类型(myarray))
匹配=“”(自定义匹配。理想的匹配方式是“USB串行端口”)
i=0
#打印(myarray,“\n”)
而我len(开发人员):
如果匹配==myarray[i]:
打印('找到了!')
打破
打印('array:',i,“:”,myarray[i])
i=i+1
print('array 49:',myarray[49])(这里我正在检查数组中“element”的区别)
打印(“匹配:”,匹配)(与我提交的内容有什么区别)
打印('结束')
我希望if match==myarray[I]能够找到这两个元素,但由于某些原因,它没有找到。这让我意识到这两个是不一样的
提前感谢您的帮助
==更新===
完整的脚本可以在这里找到
如果Python说字符串不一样,我敢说它们很可能不一样 您可以与以下内容进行比较:
if "USB Serial Port" in devs[i]:
然后,您应该能够找到一个包含USB端口的匹配项,而不是一个完整的逐字母匹配项
不需要使用numpy,
devs
已经是一个列表,因此是可编辑的。这是@MacrosG的后续回答我尝试了一个关于设备属性的最小示例
from infi.devicemanager import DeviceManager
dm = DeviceManager()
dm.root.rescan()
devs = dm.all_devices
print ('Size of Devs: ',len(devs))
for d in devs:
if "USB" in d.description :
print(d.description)
如果要使用正则表达式执行此操作,请执行以下操作:
def main():
from infi.devicemanager import DeviceManager
import re
device_manager = DeviceManager()
device_manager.root.rescan()
pattern = r"USB Serial Port \(COM(\d)\)"
for device in device_manager.all_devices:
try:
match = re.fullmatch(pattern, device.friendly_name)
except KeyError:
continue
if match is None:
continue
com_number = match.group(1)
print(f"Found device \"{device.friendly_name}\" -> com_number: {com_number}")
return 0
if __name__ == "__main__":
import sys
sys.exit(main())
输出:
Found device "USB Serial Port (COM3)" -> com_number: 3
非常感谢所有人,尤其是bigdataolddriver,因为我使用了他的解决方案 最后一件事
for d in devs:
if "USB Serial Port" in d.description :
str = d.__str__()
COMport = str.split('(', 1)[1].split(')')[0]
i=1
break
else:
i=0
if i == 1:
print ("I found it!")
print(d.description, "found on : ", COMport)
subprocess.Popen(r'"C:\Tools\putty.exe" -serial ', COMport)
elif i ==0:
print ("USB Serial Not found \nPlease check physical connection.")
else:
print("Error")
你知道如何将COMport作为参数传递给putty.exe吗
====更新=====
if i == 1:
print ("I found it!")
print(d.description, "found on : ", COMport)
command = '"C:\MyTools\putty.exe" -serial ' + COMport
#print (command)
subprocess.Popen(command)
谢谢大家 很好!对现在唯一要做的就是获取括号中的COMx信息!顺便问一下,你知道我怎样才能得到这个吗?