List of available COM ports with Python

I am looking for an easy way to list the entire available COM port on a PC.

I found this method, but it is specific to Windows: List of serial ports (COM) on Windows?

I am using Python 3 with pySerial on a Windows 7 PC.

I found in the pySerial API ( http://pyserial.sourceforge.net/pyserial_api.html ) the serial.tools.list_ports.comports() ) function that lists COM ports (exactly what I want).

 import serial.tools.list_ports print(list(serial.tools.list_ports.comports())) 

But it does not seem to work. When my COM USB gateway is connected to a PC (I see COM5 in the device manager), this COM port is not included in the list returned by list_ports.comports() . Instead, I only get COM4, ​​which seems to be connected to the modem (I don't see it in the COM & LPT section of the device manager)!

Do you know why this does not work? Do you have another solution that is not system specific?

+47
python pyserial
Aug 23 '12 at 11:25
source share
5 answers

This is the code I'm using.

Successfully tested on Windows 8.1 x64, Windows 10 x64, Mac OS X 10.9.x / 10.10.x / 10.11.x and Ubuntu 14.04 / 14.10 / 15.04 / 15.10 with Python 2 and Python 3.

 import sys import glob import serial def serial_ports(): """ Lists serial port names :raises EnvironmentError: On unsupported or unknown platforms :returns: A list of the serial ports available on the system """ if sys.platform.startswith('win'): ports = ['COM%s' % (i + 1) for i in range(256)] elif sys.platform.startswith('linux') or sys.platform.startswith('cygwin'): # this excludes your current terminal "/dev/tty" ports = glob.glob('/dev/tty[A-Za-z]*') elif sys.platform.startswith('darwin'): ports = glob.glob('/dev/tty.*') else: raise EnvironmentError('Unsupported platform') result = [] for port in ports: try: s = serial.Serial(port) s.close() result.append(port) except (OSError, serial.SerialException): pass return result if __name__ == '__main__': print(serial_ports()) 
+93
Jan 08 '13 at
source share

You can use:

python -c "import serial.tools.list_ports;print serial.tools.list_ports.comports()"

He knows to python -c "import serial.tools.list_ports;print [port for port in serial.tools.list_ports.comports() if port[2] != 'n/a']" port: python -c "import serial.tools.list_ports;print [port for port in serial.tools.list_ports.comports() if port[2] != 'n/a']"

See here for more details: https://pyserial.readthedocs.org/en/latest/tools.html#module-serial.tools.list_ports

+14
Nov 12 '15 at 3:44
source share

A possible refinement of Thomas’s excellent response to Linux and possibly OSX also attempting to open ports and return only those that could be opened. This is because Linux at least lists the boat of ports as files in / dev / that are not associated with anything. If you work in a terminal, / dev / tty is the terminal you work in, and opening and closing may lead to your command line, so glob is not intended for this. The code:

  # ... Windows code unchanged ... elif sys.platform.startswith ('linux'): temp_list = glob.glob ('/dev/tty[A-Za-z]*') result = [] for a_port in temp_list: try: s = serial.Serial(a_port) s.close() result.append(a_port) except serial.SerialException: pass return result 

This Thomas code modification was tested only on Ubuntu 14.04.

+10
Jul 13 '14 at 6:21
source share

Several options are available:

Call QueryDosDevice with NULL lpDeviceName to list all DOS devices. Then use CreateFile and GetCommConfig with each device name to find out if it is a serial port.

Call SetupDiGetClassDevs with ClassGuid GUID_DEVINTERFACE_COMPORT.

WMI is also available for C / C ++ programs .

There's some talk about win32 newsgroup and CodeProject, er, project .

+1
Aug 23 '12 at 11:29
source share

moylop260 clarification:

 import serial.tools.list_ports list = serial.tools.list_ports.comports() connected = [] for element in list: connected.append(element.device) print("Connected COM ports: " + str(connected)) 

The ports that exist on the equipment are listed here, including those that are used. There is much more information in the list for the pyserial tool documentation.

+1
Aug 01 '17 at 18:20
source share



All Articles