title: Listing serial (COM) ports on Windows?
body: <p>I'm looking for a robust way to list the available serial (COM) ports on a Windows machine. There's <a href="https://stackoverflow.com/questions/1081871/how-to-find-available-com-ports">this post about using WMI</a>, 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.</p>
  
  <p>I currently know of two other approaches:</p>
  
  <ol>
  <li><p>Reading the information in the <code>HARDWARE\\DEVICEMAP\\SERIALCOMM</code> registry key. This looks like a great option, but is it <strong>robust</strong>? I can't find a guarantee online or in MSDN that this registry cell indeed always holds the full list of available ports.</p></li>
  <li><p>Tryint to call <code>CreateFile</code> on <code>COMN</code> 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.</p></li>
  </ol>
  
  <p>Any other methods/ideas/experience to share?</p>
  
  <p><strong>Edit:</strong> by the way, here's a simple Python implementation of reading the port names from registry:</p>
  
  <pre><code>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
  </code></pre>