Pyserial List Serial Ports

Listports does not seem to find all the available com ports, because I have a USB serial device that does not show up using the listports generator, yet realterm finds it and puts it in the list of selectable ports without any problem. I'm looking for a robust way to list the available serial (COM) ports on a Windows machine. There's this post about using WMI, but I would like something less.NET specific - I want to get the list of ports in a Python or a C program, without.NET. I currently know of two other approaches: Reading the information in the HARDWAREDEVICEMAPSERIALCOMM registry key.

Active1 year, 5 months ago

I'm looking for a robust way to list the available serial (COM) ports on a Windows machine. There's this post about using WMI, but I would like something less .NET specific - I want to get the list of ports in a Python or a C++ program, without .NET.

I currently know of two other approaches:

  1. Reading the information in the HARDWAREDEVICEMAPSERIALCOMM registry key. This looks like a great option, but is it robust? I can't find a guarantee online or in MSDN that this registry cell indeed always holds the full list of available ports.

  2. Tryint to call CreateFile on COMN with N a number from 1 to something. This isn't good enough, because some COM ports aren't named COMN. For example, some virtual COM ports created are named CSNA0, CSNB0, and so on, so I wouldn't rely on this method.

Any other methods/ideas/experience to share?

Edit: by the way, here's a simple Python implementation of reading the port names from registry:

Community
Eli BenderskyEli Bendersky
179k72 gold badges310 silver badges382 bronze badges

8 Answers

Several options are available:

  1. Call QueryDosDevice with a NULL lpDeviceName to list all DOS devices. Then use CreateFile and GetCommConfig with each device name in turn to figure out whether it's a serial port.

  2. Call SetupDiGetClassDevs with a ClassGuid of GUID_DEVINTERFACE_COMPORT.

  3. WMI is also available to C/C++ programs.

Pyserial Com Port Example

There's some conversation on the win32 newsgroup and a CodeProject, er, project.

Oren TrutnerOren Trutner
20.7k7 gold badges48 silver badges54 bronze badges
Matt WilliamsMatt Williams

The PySerial project provides a couple of solutions.

zdavzdav
Pyserial List Serial Ports

I just created the following, based on reading through the C++ source to EnumSerialPorts and seeing the function GetDefaultCommConfig(). It looked like the simplest method using simple ANSI C and a single API call for each possible COM port.

tomlogictomlogic
8,8853 gold badges26 silver badges55 bronze badges

This is definitely fairly late, but it proved helpful to me!

Particularly this example:

eatonphileatonphil
5,73515 gold badges60 silver badges105 bronze badges

There's an example in the pyserial distro now that does this called scanwin32.py

BenBen

I think WMI is the way to go since it's pretty easy to get going and it has minimal code. It saves you from having to dig inside the registry and gives you some guarantee that it will work for more general situations in the future.

You can install everything needed with pip install pypiwin32 WMI and it works out-of-the-box.

Code

Output

My guess is that your serial port is some sort of Plug 'n Play so this should work fine. For some reason Win32_SerialPort doesn't work for all ports.

PithikosPithikos
8,27714 gold badges75 silver badges103 bronze badges
Paul WilliamsPaul Williams

Not the answer you're looking for? Browse other questions tagged pythoncwindowswinapiserial-port or ask your own question.

Active1 year ago

As you can see the comports are not readable, I can make it readable through for loops but I need it in list with readable word, eg. ['COM1','COM2','COM3']

Anyone has any idea to change it? I tried create a new list and append in the loop also no luck. When I print out the result is the same.

UPDATES:I think what I did last time was appending each object together again. I am able to get the specific information I want which is COM with the code below:

AnonymousProgrammer
AnonymousProgrammerAnonymousProgrammer

3 Answers

You can iterate through the list and get the names:

eyllanesc

Pyserial List Serial Ports List

eyllanesc
111k12 gold badges41 silver badges75 bronze badges

The comports() method return ListPortInfo object.

Then, you can access a bunch of attributes of this class to get the desired results:

  • name: Short device name, e.g. ttyUSB0
  • device: Full device name/path, e.g. /dev/ttyUSB0

See the docs.

An general solution for this, could be:

AbeAbe

statement prints

in Windows 10 (with python 3.61 + pyserial v3.4). Changed to

ports = [p.device for p in comports()]

to get valid port name.

Pyserial List Com Ports

Vinu Raja Kumar CVinu Raja Kumar C

Python Serial List Serial Ports

Not the answer you're looking for? Browse other questions tagged pythonportpython-3.5pyserial or ask your own question.