Finding a specific serial COM port in pySerial (Windows)
Solution 1
I know this post is very old, but I thought I would post my findings since there was no 'accepted' answer (better late than never).
This documentation helped with determining members of the object, and I eventually came to this solution.
import serial.tools.list_ports
ports = list(serial.tools.list_ports.comports())
for p in ports:
if 'MyCDCDevice' in p.description:
print(p)
# Connection to port
s = serial.Serial(p.device)
Solution 2
To further extend on this, I've found it safer to make use of the PID and VID of the device in question.
import serial.tools.list_ports
# FTDI FT232 device (http://www.linux-usb.org/usb.ids)
pid="0403"
hid="6001"
my_comm_port = None
ports = list(serial.tools.list_ports.comports())
for p in ports:
if pid and hid in p.hwid:
my_comm_port = p.device
Better still, you can use the serial number of the device for the lookup, just in case you have 2 of the same device plugged in.
(Source)
coolestDisplayName
Updated on June 05, 2022Comments
-
coolestDisplayName almost 2 years
I have a script built (Windows 7, Python 2.7) to list the serial ports but I'm looking for a device with a specific name. My script:
import serial.tools.list_ports ports = list(serial.tools.list_ports.comports()) for p in ports: print(p)
This returns:
COM3 - Intel(R) Active Management Technology - SOL (COM3) COM6 - MyCDCDevice (COM6) COM1 - Communications Port (COM1) >>>
Great! However, I want this script to automatically pick out MyCDCDevice from the bunch and connect to it. I tried:
import serial.tools.list_ports ports = list(serial.tools.list_ports.comports()) for p in ports: if 'MyCDCDevice' in p: print(p) // do connection stuff to COM6
But that doesn't work. I suspect because p isn't exactly a string, but an object of some sort?
Anyways, what's the correct way to go about this?
Thanks!!