【问题标题】:iokit.IOServiceGetMatchingServices broken under Python3?iokit.IOServiceGetMatchingServices 在 Python3 下坏了?
【发布时间】:2015-06-13 07:27:23
【问题描述】:

我刚得到一个运行 Yosemite 的 mac mini(我通常使用 linux)。我试图让 serial.tools.list_ports.comports() 在 Python3 下工作。

我已将问题范围缩小到以下 sn-p 代码(从 pyserial 中提取):

import ctypes
from ctypes import util

iokit = ctypes.cdll.LoadLibrary(ctypes.util.find_library('IOKit'))

kIOMasterPortDefault = ctypes.c_void_p.in_dll(iokit, "kIOMasterPortDefault")

iokit.IOServiceMatching.restype = ctypes.c_void_p

iokit.IOServiceGetMatchingServices.argtypes = [ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p]
iokit.IOServiceGetMatchingServices.restype = ctypes.c_void_p

serial_port_iterator = ctypes.c_void_p()

print('kIOMasterPortDefault =', kIOMasterPortDefault)

response = iokit.IOServiceGetMatchingServices(
    kIOMasterPortDefault,
    iokit.IOServiceMatching('IOSerialBSDClient'),
    ctypes.byref(serial_port_iterator)
)
print('serial_port_iterator =', serial_port_iterator)

如果我在 python2 下运行它,那么它可以正常工作:

382 >python bug.py 
('kIOMasterPortDefault =', c_void_p(None))
('serial_port_iterator =', c_void_p(4355))

但是,当我在 Python3 下运行它时,它会失败(serial_port_iterator 保持为 c_void_p(None))

383 >python3 bug.py
kIOMasterPortDefault = c_void_p(None)
serial_port_iterator = c_void_p(None)

有谁知道为什么这在 Python3 下会失败,也许如何解决?

【问题讨论】:

    标签: python macos python-3.x pyserial


    【解决方案1】:

    好的 - 想通了。

    Python3 将字符串作为 unicode(宽)字符串传递,而 Python2 将字符串作为窄字符串传递。

    所以改变这一行

    iokit.IOServiceMatching('IOSerialBSDClient'),
    

    阅读

    iokit.IOServiceMatching(b'IOSerialBSDClient'),
    

    使其适用于 Python2 和 3。

    现在看看我是否可以将更改转换为 pyserial。

    【讨论】:

      猜你喜欢
      • 2018-08-12
      • 1970-01-01
      • 2010-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-17
      • 2011-03-12
      • 1970-01-01
      相关资源
      最近更新 更多