【发布时间】: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