aboutsummaryrefslogtreecommitdiffstats
path: root/support/scripts/win-list-com-ports.py
blob: 3e6ecb80122c552521f0346dfd4320037130762e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# Windows program for listing COM (serial) ports.
#
# enumerate_serial_ports() is by Eli Bendersky:
#
# http://eli.thegreenplace.net/2009/07/31/listing-all-serial-ports-on-windows-with-python/

import _winreg as winreg
import itertools

def enumerate_serial_ports():
    """ Uses the Win32 registry to return an
        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])
        except EnvironmentError:
            break

for com in enumerate_serial_ports():
    print com