Pregunta

I'm having trouble listing available serial ports and I really need help debugging this. In Python 2.7.5 the COM-ports are listed correctly while PySerial returns an empty list in Python 3.3.5.

I found one other lonely soul with the same problems on the internet (no answers), but the problem doesn't seem to be popular at all - maybe it's my system?

I'm using Mac OS X 10.9.2 and installed python and python3 via homebrew. I updated everything just now. PySerial is at version 2.7 in both pip and pip3.

The output:

Python 2.7.5 (default, Nov  4 2013, 18:04:45) 
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.2.79)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from serial.tools import list_ports
>>> list_ports.comports()
[['/dev/cu.Bluetooth-Incoming-Port', 'n/a', 'n/a'], ['/dev/cu.Bluetooth-Modem', 'n/a', 'n/a']]

Python 3.3.5 (default, Mar 10 2014, 13:25:50) 
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.2.79)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from serial.tools import list_ports
>>> list_ports.comports()
[]
¿Fue útil?

Solución

For the moment I'm resorting to the following method. The PySerial tools are broken.

>>> import glob
>>> glob.glob('/dev/tty.*')
['/dev/tty.Bluetooth-Incoming-Port', '/dev/tty.Bluetooth-Modem']

Otros consejos

The root cause of the problem stems from the fact that python3 encodes strings as unicode (wide) strings, and python2 encodes strings as narrow strings.

So the pyserial code needs to pass down narrow strings when calling the API functions from iokit.

I discovered that somebody else also ran into this and posted a patch. You can find his patch at the end of this issue: http://sourceforge.net/p/pyserial/patches/38/

Using that patch, I now get the same behaviour from python3 as I do from python2.

I had installed python3 using macports, and my installation of pyserial for python3 was found in this directory: /opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/serial

so I executed the following:

cd /opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/serial/tools
sudo cp list_ports_osx.py list_ports_osx_orig.py
sudo curl -O http://sourceforge.net/p/pyserial/patches/_discuss/thread/603bd426/55a8/attachment/list_ports_osx.py

and that made list_ports.comports() work for me.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top