<pre><code>import serial as ser
import serial.tools.list_ports as listport
import re
try:
# VID regex
regex_vplogVID = re.compile(r'{\S+}_VID')
# I want to find COM port by using specific hwid
port_device = [n.device for n in listport.comports() if re.findall(regex_vplogVID, n.hwid)]
vplogserial = ser.Serial(port_device[0])
except Exception as e:
print(e)
</code></pre>
<p>实际上,我是使用python的新手程序员。
我想找到端口使用唯一的hwid,但我认为列表理解是不合适的,因为端口将返回一个。你知道吗</p>
<p>我使用的是简单的for循环代码吗?
请分享您的意见。:)谢谢你的阅读。你知道吗</p>
<p>我会使用<code>for</code>循环,如果只是因为一旦找到唯一的设备,就可以提前停止迭代。你知道吗</p>
<pre><code>for n in listport.comports():
if re.findall(regex_vplogVID, n.hwid):
vplogserial = ser.Serial(n.device)
break
</code></pre>