Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pyserial enumerate ports

I need list or enumerate of existing serial ports, Till now I was using this method enumerate_serial_ports(), but its not working with windows 7. Do you know some alternative how can I find out 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

like image 217
Meloun Avatar asked May 30 '11 12:05

Meloun


People also ask

Is PySerial the same as serial?

PySerial is a library which provides support for serial connections ("RS-232") over a variety of different devices: old-style serial ports, Bluetooth dongles, infra-red ports, and so on. It also supports remote serial ports via RFC 2217 (since V2. 5).


2 Answers

There's now a list_ports module built in to 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')]

The module can also be executed directly:

$ python -m serial.tools.list_ports
/dev/ttyS0          
/dev/ttyS1          
/dev/ttyS2          
/dev/ttyS3          
/dev/ttyUSB0        
5 ports found
like image 115
mgalgs Avatar answered Oct 03 '22 14:10

mgalgs


You're raising an IterationError, but that exception doesn't actually exist. Maybe you should try raising EnvironmentError for that condition as well.

The pySerial docs include some sample code for finding serial ports. Check them out: http://pyserial.sourceforge.net/examples.html#finding-serial-ports

like image 40
mrb Avatar answered Oct 03 '22 12:10

mrb