Listing serial (COM) ports on Windows?

Using pySerial with Python:

import serial.tools.list_ports


ports = list(serial.tools.list_ports.comports())
for p in ports:
    print p

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.