I am searching for a simple method to list all available com port on a PC.
I have found this method but it is Windows-specific: Listing serial (COM) ports on Windows?
I am using Python 3 with pySerial on a Windows 7 PC.
I have found in the pySerial API (http://pyserial.sourceforge.net/pyserial_api.html) a function serial.tools.list_ports.comports()
that lists com ports (exactly what I want).
import serial.tools.list_ports
print(list(serial.tools.list_ports.comports()))
But it seems that it doesn't work. When my USB to COM gateway is connected to the PC (I see the COM5 in the Device Manager), this COM port isn't included in the list returned by list_ports.comports()
. Instead I only get COM4 which seems to be connected to a modem (I don't see it in the COM&LPT section of Device Manager)!
Do you know why it doesn't work? Have you got another solution which is not system specific?
comports()
function that was described in this question (without precise information on how to reproduce it) has probably been fixed. Start by tryingimport serial.tools.list_ports; print([comport.device for comport in serial.tools.list_ports.comports()])
. Only if that doesn't work for you are any of the answers below relevant to you. – DriscollListPortInfo
objects. To get the names or other information you must use the attributes of these objects when building the list. See: pythonhosted.org/pyserial/… – Tertius