【问题标题】:Listing serial ports in Mac OS X and Python 3列出 Mac OS X 和 Python 3 中的串行端口
【发布时间】:2014-03-17 23:44:20
【问题描述】:

我无法列出可用的串行端口,我真的需要帮助来调试它。 在 Python 2.7.5 中,COM 端口正确列出,而 PySerial 在 Python 3.3.5 中返回一个空列表。

我在互联网上发现另一个孤独的灵魂有同样的问题(没有答案),但这个问题似乎一点也不流行——也许是我的系统?

我使用的是 Mac OS X 10.9.2,并通过自制软件安装了 python 和 python3。我刚刚更新了所有内容。 PySerial 在 pip 和 pip3 中都是 2.7 版。

输出:

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()
[]

【问题讨论】:

  • 我对 pyserial 也有同样的问题。 Python 3.4.1,OSX 10.9。总是返回空列表。找到解决方案了吗?
  • 你可以在问题的底部找到我的修复。
  • @Thomas 您的编辑很有用:您应该考虑重写它作为您自己问题的答案!

标签: macos python-3.x pyserial


【解决方案1】:

目前我采用以下方法。 PySerial 工具坏了。

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

【讨论】:

    【解决方案2】:

    问题的根源在于python3将字符串编码为unicode(宽)字符串,而python2将字符串编码为窄字符串。

    所以pyserial代码在从iokit调用API函数时需要传递窄字符串。

    我发现其他人也遇到了这个问题并发布了一个补丁。您可以在本期末尾找到他的补丁: http://sourceforge.net/p/pyserial/patches/38/

    使用该补丁,我现在从 python3 中获得与从 python2 中相同的行为。

    我已经使用 macports 安装了 python3,我为 python3 安装的 pyserial 是在这个目录中找到的: /opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/serial

    所以我执行了以下操作:

    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
    

    这使得 list_ports.comports() 对我有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-24
      • 1970-01-01
      • 2010-10-29
      • 2014-07-25
      • 2016-12-22
      • 2017-01-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多