8

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!!

4
  • 1
    add else: print "NOT:",repr(p) where you have your comment (which is not a python comment...but i suspect you knew that) Commented Jan 20, 2016 at 1:20
  • 1
    It would be str(p), but you can also use p.description. Commented Jan 20, 2016 at 3:02
  • 1
    I figured it would be just that simple! Thanks guys, YOU ROCK! str(p) did the trick. Commented Jan 20, 2016 at 8:38
  • In windows am facing problem even with import after installing pyserial, any idea on how to solve this. Error is: "ImportError: No module named tools.list_ports" Commented May 7, 2018 at 11:46

3 Answers 3

6

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)
Sign up to request clarification or add additional context in comments.

Comments

5

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)

Comments

0

You can use serial.tools.list_ports.grep, which searches all of the description fields for you. For example:

from serial.tools import list_ports

try:
    cdc = next(list_ports.grep("MyCDCDevice"))
    # Do connection stuff on cdc
except StopIteration:
    print "No device found"

If that doesn't work, you may try adding a * to the end of the string you pass to grep in case there are extra characters in the descriptor.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.