text: """
  Some serial port utilities for Windows and PySerial
  
  Eli Bendersky (eliben@gmail.com)
  License: this code is in the public domain
  """
  import re, itertools
  import _winreg as winreg
  
     
  def full_port_name(portname):
      """ Given a port-name (of the form COM7, 
          COM12, CNCA0, etc.) returns a full 
          name suitable for opening with the 
          Serial class.
      """
      m = re.match('^COM(\d+)$', portname)
      if m and int(m.group(1)) < 10:
          return portname    
      return '\\\\.\\' + portname    
      
  
  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 StopIteration
  
      for i in itertools.count():
          try:
              val = winreg.EnumValue(key, i)
              yield str(val[1])
          except EnvironmentError:
              break
  
  
  if __name__ == "__main__":
      import serial
      for p in enumerate_serial_ports():
          print p, full_port_name(p)
          
  
  
  
  