Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Listing serial (COM) ports on Windows?

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 HARDWARE\\DEVICEMAP\\SERIALCOMM 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:

import _winreg as winreg import itertools   def enumerate_serial_ports():     """ Uses the Win32 registry to return a iterator of serial          (COM) ports existing on this computer.       """     path = 'HARDWARE\\DEVICEMAP\\SERIALCOMM'     try:         key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, path)     except WindowsError:         raise IterationError      for i in itertools.count():         try:             val = winreg.EnumValue(key, i)             yield (str(val[1]), str(val[0]))         except EnvironmentError:             break 
like image 757
Eli Bendersky Avatar asked Jul 30 '09 09:07

Eli Bendersky


People also ask

How do I identify COM ports on my computer?

Follow the instructions to get your COM port number: Open Device Manager (Start → Control Panel → Hardware and Sound → Device Manager) Look in the Device Manager list, open the category "Ports" and find the matching COM Port. Take the number in the bracket behind the port description.

How do I find my COM ports on Windows 10?

Locate the correct COM portRight click on the Windows Start Icon and select "Device Manager." Open the "Ports (COM & LPT)" Section. Locate the "PI USB to Serial" and note which COM port it is using.

How do I check my COM port status?

On a Windows computerPress the Windows key + R, then type "cmd.exe" and click OK. Enter "telnet + IP address or hostname + port number" (e.g., telnet www.example.com 1723 or telnet 10.17. xxx. xxx 5000) to run the telnet command in Command Prompt and test the TCP port status.


2 Answers

Using pySerial with Python:

import serial.tools.list_ports   ports = list(serial.tools.list_ports.comports()) for p in ports:     print p 
like image 129
Matt Williams Avatar answered Oct 07 '22 15:10

Matt Williams


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.

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

like image 36
Oren Trutner Avatar answered Oct 07 '22 16:10

Oren Trutner