Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding a specific serial COM port in pySerial (Windows)

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

like image 466
coolestDisplayName Avatar asked Jan 20 '16 01:01

coolestDisplayName


1 Answers

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)
like image 107
jzamora Avatar answered Sep 30 '22 21:09

jzamora