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.
- Pyserial Com Port Example
- Pyserial List Serial Ports List
- Pyserial List Com Ports
- Python Serial List Serial Ports
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. 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.Tryint to call
CreateFile
onCOMN
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:
8 Answers
Several options are available:
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.
Call SetupDiGetClassDevs with a ClassGuid of GUID_DEVINTERFACE_COMPORT.
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 TrutnerThe PySerial project provides a couple of solutions.
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.
This is definitely fairly late, but it proved helpful to me!
Particularly this example:
eatonphileatonphilThere's an example in the pyserial distro now that does this called scanwin32.py
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.
Not the answer you're looking for? Browse other questions tagged pythoncwindowswinapiserial-port or ask your own question.
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:
3 Answers
You can iterate through the list and get the names:
eyllanescPyserial List Serial Ports List
eyllanescThe 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:
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.