问题描述
我正在尝试用 python 编写一个脚本,这样我就可以在 1 秒内找到我插入笔记本电脑的 USB 串行适配器的 COM 号.我需要的是隔离 COMx 端口,以便我可以显示结果并使用该特定端口打开腻子.你能帮我解决这个问题吗?
I am trying to write a script in python so I can find in 1 sec the COM number of the USB serial adapter I have plugged to my laptop.What I need is to isolate the COMx port so I can display the result and open putty with that specific port. Can you help me with that?
到目前为止,我已经在批处理/powershell 中编写了一个脚本,并且我正在获取此信息,但是我无法分离 COMx 端口的文本,因此我可以使用串行参数调用 putty 程序.我也可以通过 Python 找到端口,但我无法将其与字符串隔离.
Until now I have already written a script in batch/powershell and I am getting this information but I havent been able to separate the text of the COMx port so I can call the putty program with the serial parameter.I have also been able to find the port via Python but I cant isolate it from the string.
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')
我期待 if match == myarray[i] 找到这两个元素,但由于某种原因它没有.它让我知道这两个不一样.
I was expecting the if match == myarray[i] to find the two elements but for some reason it doesnt. Its returning me that those two are not the same.
提前感谢您的帮助!
=== 更新 ===完整的脚本可以在这里找到https://github.com/elessargr/k9-serial
=== UPDATE ===Full script can be found herehttps://github.com/elessargr/k9-serial
推荐答案
这是@MacrosG 的后续回答
我尝试了一个带有设备属性的最小示例
this is a follow up answer from @MacrosG
i tried a minimal example with properties from Device
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)
这篇关于使用 Python 脚本查找 USB 串口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!