擅长:python、mysql、java
<p>我假设您正在设备管理器中专门寻找一个被描述为USB到RS232的COM端口,而不是想要列出所有可用的COM端口?</p>
<p>另外,您还没有提到您正在开发的操作系统,或者您正在使用的Python版本,但是对于使用Python 3.4的Windows系统,这一点对我很有用:</p>
<pre><code>import serial.tools.list_ports
def serial_ports():
# produce a list of all serial ports. The list contains a tuple with the port number,
# description and hardware address
#
ports = list(serial.tools.list_ports.comports())
# return the port if 'USB' is in the description
for port_no, description, address in ports:
if 'USB' in description:
return port_no
</code></pre>