pyserial enumerate ports
Solution 1
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
Solution 2
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
Solution 3
Below you find my helper function to print the names and description of the available com ports, using the serial
module:
from serial.tools import list_ports
print(
"\n".join(
[
port.device + ': ' + port.description
for port in list_ports.comports()
]))
Example output:
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)
Meloun
Updated on June 07, 2022Comments
-
Meloun almost 2 years
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
-
JBernardo almost 13 yearsHis problem isn't with the IterationError. Fixing this name won't solve the problem.
-
Exectron about 11 yearsThat URL doesn't seem applicable now. Maybe pyserial.sourceforge.net/shortintro.html#testing-ports instead.
-
RyanN about 9 yearsThis code didn't work for me, Since list_ports.comports() is a generator, I had to do: from serial.tools import list_ports for a in list_ports.comports(): print(a)
-
payne almost 9 years@RyanN You can easily turn a generator into a list by wrapping it with list()
-
RufusVS over 8 yearslist_ports does not seem to find all the available com ports, because I have a USB serial device that does not show up using the list_ports generator, yet realterm finds it and puts it in the list of selectable ports without any problem. It ends up being at COM20, and if I open COM20 with the serial module, it works fine. But if I want the user to be able to select among the existing ports, I have to make it show up on the list of available ports.