Pyserial list ports

I need a list or enumeration of existing serial ports. So far I used this enumerate_serial_ports () method , but it didn’t work with Windows 7. Do you know any alternative, how can I find out about available serial ports under windows 7?

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

I get IterationError enter image description here

+5
source share
3 answers

You raise IterationError, but this exception does not actually exist. Perhaps you should try to raise EnvironmentErrorthe conditions for this.

pySerial . : http://pyserial.sourceforge.net/examples.html#finding-serial-ports

+3

list_ports pyserial.

In [26]: from serial.tools import list_ports
In [27]: list_ports.comports()
Out[27]: 
[('/dev/ttyS3', 'ttyS3', 'n/a'),
 ('/dev/ttyS2', 'ttyS2', 'n/a'),
 ('/dev/ttyS1', 'ttyS1', 'n/a'),
 ('/dev/ttyS0', 'ttyS0', 'n/a'),
 ('/dev/ttyUSB0',
  'Linux Foundation 1.1 root hub ',
  'USB VID:PID=0403:6001 SNR=A1017L9P')]

:

$ python -m serial.tools.list_ports
/dev/ttyS0          
/dev/ttyS1          
/dev/ttyS2          
/dev/ttyS3          
/dev/ttyUSB0        
5 ports found
+15

Below you will find my helper function for printing names and descriptions of available COM ports using the module serial:

from serial.tools import list_ports
print(
    "\n".join(
        [
            port.device + ': ' + port.description
            for port in list_ports.comports()
        ]))

Output Example:

python.exe -u listSerialPorts.py
COM4: Sierra Wireless NMEA Port (COM4)
COM12: USB Serial Port (COM12)
COM10: USB Serial Port (COM10)
COM3: Intel(R) Active Management Technology - SOL (COM3)
COM5: Sierra Wireless DM Port (COM5)
+1
source

All Articles