如何使用pylibftdi连接到FT230?

时间:2018-01-17 01:22:16

标签: python-2.7 ftdi

我只有一个FTDI FT230X  连接到我的机器。 我可以使用lsusb

查看设备
$ lsusb
Bus 002 Device 005: ID 051d:0002 American Power Conversion Uninterruptible Power Supply
Bus 002 Device 004: ID 046d:c408 Logitech, Inc. Marble Mouse (4-button)
...
Bus 003 Device 010: ID 0403:6015 Future Technology Devices International, Ltd Bridge(I2C/SPI/UART/FIFO)
...

0403:6015条目是FT230X。 我想在Python中讨论这个问题,所以我们做了

>>> import pylibftdi
>>> dev = pylibftdi.Device()

但是会收到错误信息

~/.virtualenvs/labrad/local/lib/python2.7/site-packages/pylibftdi/device.pyc in __init__(self, device_id, mode, encoding, lazy_open, chunk_size, interface_select, device_index, auto_detach, **kwargs)
    114         # lazy_open tells us not to open immediately.
    115         if not lazy_open:
--> 116             self.open()
    117 
    118     def __del__(self):

~/.virtualenvs/labrad/local/lib/python2.7/site-packages/pylibftdi/device.pyc in open(self)
    154             self.fdll.ftdi_deinit(byref(self.ctx))
    155             del self.ctx
--> 156             raise FtdiError(msg)
    157 
    158         if self.auto_detach and self.driver.libftdi_version().major > 0:

FtdiError: device not found (-3)

我们也可以尝试明确指定序列号:

>>> import usb.core
>>> devs = list(usb.core.find(find_all=True))
>>> for x in str(devs).split(','):
        print(x)
[<DEVICE ID 051d:0002 on Bus 002 Address 005>
<DEVICE ID 046d:c408 on Bus 002 Address 004>
<DEVICE ID 04ca:006e on Bus 002 Address 003>
<DEVICE ID 8087:0024 on Bus 002 Address 002>
<DEVICE ID 1d6b:0002 on Bus 002 Address 001>
<DEVICE ID 1d6b:0003 on Bus 004 Address 001>
<DEVICE ID 1366:0101 on Bus 003 Address 009>
<DEVICE ID 0403:6015 on Bus 003 Address 010>  # <-- that one
<DEVICE ID 1d6b:0002 on Bus 003 Address 001>
<DEVICE ID 1050:0211 on Bus 001 Address 003>
<DEVICE ID 8087:0024 on Bus 001 Address 002>
<DEVICE ID 1d6b:0002 on Bus 001 Address 001>]

>>> my_dev = devs[7]
>>> ser = devs.serial_number
>>> import pylibftdi
>>> pylibftdi.Device(ser)

同样的错误。

为什么会这样? pylibftdi documentation声称Device构造函数应该使用第一个可用设备,而我的情况只有一个。 错误消息“找不到设备”让我想知道pylibftdi正在使用的驱动程序是否支持FT230X,在我的情况下我认为是libftdi1

1 个答案:

答案 0 :(得分:1)

pylibftdi有一个USB产品ID(idProduct)的硬编码列表,它将在pylibftdi.driver.USB_PID_LIST中搜索。这不包括FT230X芯片,它也不会查询libftdi以获取底层库支持的芯片:

来自pylibftdi / driver.py:

# Opening / searching for a device uses this list of IDs to search
# by default. These can be extended directly after import if required.
FTDI_VENDOR_ID = 0x0403
USB_VID_LIST = [FTDI_VENDOR_ID]
USB_PID_LIST = [0x6001, 0x6010, 0x6011, 0x6014]

不幸的是,这似乎不起作用:

>>> import pylibftdi
>>> pylibftdi.driver.USB_PID_LIST.append(0x6015)
>>> print(["%04x " % x for x in pylibftdi.USB_PID_LIST])
['6001 ', '6010 ', '6011 ', '6014 ', '6015 ']
>>> dev = pylibftdi.Device()  
Segmentation fault

这里的问题似乎是当pylibftdi调用libusb来设置自动分离时。如果我执行以下操作似乎有效:

>>> import pylibftdi
>>> pylibftdi.driver.USB_PID_LIST.append(0x6015)
>>> print(["%04x " % x for x in pylibftdi.USB_PID_LIST])  
['6001 ', '6010 ', '6011 ', '6014 ', '6015 ']
>>> dev = pylibftdi.Device(auto_detach=False)  
>>> dev
<pylibftdi.device.Device at 0x7f586d08f350>

对我来说,这并不能防止libftdi成功分离内核驱动程序(/ dev / ttyUSB0在我运行时会消失),但它会阻止会话关闭后重新附加。