在 raspbian soft-float 上将 FTDI D2XX 驱动程序与来自 Raspberry Pi 的 Python 一起使用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14615308/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using FTDI D2XX drivers with Python from Raspberry Pi on raspbian soft-float
提问by TheDuke
We have a USB device controlled by FTDI's D2XX drivers. It's currently controlled from a Windows machine via a Python interface and as a fun project I tried moving the control to a Raspberry Pi (about 1/10th the cost of a PC, not including the OS cost).
我们有一个由 FTDI 的 D2XX 驱动程序控制的 USB 设备。它目前是通过 Python 接口从 Windows 机器控制的,作为一个有趣的项目,我尝试将控制移到 Raspberry Pi(大约是 PC 成本的 1/10,不包括操作系统成本)。
There were many hurdles to clear, but after a few weeks I finally found all the answers and got it working. The answers were spread across several forums so as a thank you to the Stack Overflow community I thought I'd consolidate them here.
有许多障碍需要清除,但几周后我终于找到了所有的答案并让它发挥了作用。答案散布在多个论坛中,以感谢 Stack Overflow 社区,我想我会在这里合并它们。
First, the project required:
一、项目要求:
- An operating system: I picked up the latest "Wheezy"Raspbian (hard-float 2012-12-16) from the Raspberry Pi webpage. This included Python.
- The FTDI D2XX driversto talk to our FTDI device.
- A Python interface to the D2XX driver. I used PyUSB
- Our Python script
- 操作系统:我从Raspberry Pi 网页上找到了最新的“Wheezy”Raspbian(hard-float 2012-12-16)。这包括 Python。
- 该FTDI D2XX司机谈谈我们FTDI设备。
- D2XX 驱动程序的 Python 接口。我使用了PyUSB
- 我们的 Python 脚本
I downloaded the Wheezy distribution and used Win32DiskImager to write to a 4 GB SD card. The Raspberry Pi booted with no problems. I then unpacked the D2XX library (libftd2xx.so) and installed it into /usr/local/lib.
我下载了 Wheezy 发行版并使用 Win32DiskImager 写入 4 GB SD 卡。树莓派启动没有问题。然后我解压 D2XX 库 (libftd2xx.so) 并将其安装到/usr/local/lib.
PyUSB (1.6) currently is tested only against Windows, but they provide the source code. It's pretty straightforward to compile a copy for Raspberry Pi. Basically, modify setup.py to link to the libftd2xx.so library (no need to copy it). Also edit d2xx/_d2xx.c to comment out the routines with no Linux implementation (currently ftobj_Rescan, ftobj_Reload ftobj_GetComPortNumber). Copy WinTypes.h and ftd2xx.h from the FTDI D2XX driver download (in the release) directory into ftdi-win32 and run python setup.py installwhich will compile and install the Python module.
PyUSB (1.6) 目前仅针对 Windows 进行了测试,但它们提供了源代码。为 Raspberry Pi 编译一个副本非常简单。基本上,修改 setup.py 以链接到 libftd2xx.so 库(无需复制)。还要编辑 d2xx/_d2xx.c 以注释掉没有 Linux 实现的例程(当前是 ftobj_Rescan、ftobj_Reload ftobj_GetComPortNumber)。将 FTDI D2XX 驱动程序下载(在发行版中)目录中的 WinTypes.h 和 ftd2xx.h 复制到 ftdi-win32 并运行python setup.py install,这将编译和安装 Python 模块。
Once all that was done I wrote a simple Python script to talk to the FTDI chip. Note you need to run via sudo.
完成所有这些后,我编写了一个简单的 Python 脚本来与 FTDI 芯片通信。请注意,您需要通过 sudo 运行。
import d2xx
jd = d2xx.open(0)
pd = jd.eeRead()
print pd
The d2xx module could not seem to find the libftd2xx.so file. So, I tweaked the setup.py script to link to the static copy of the library, libftd2xx.a. Voila, I had my first clue of the problem: The D2XX library was built using soft-float, and my Wheezy distribution was configured to use floating point registers. That is, the gcc on my system generated code that was binary incompatible with the D2XX libraries and would not allow them to be linked to.
d2xx 模块似乎找不到 libftd2xx.so 文件。因此,我调整了 setup.py 脚本以链接到库的静态副本 libftd2xx.a。瞧,我对这个问题有了第一个线索:D2XX 库是使用软浮点构建的,而我的 Wheezy 发行版配置为使用浮点寄存器。也就是说,我系统上的 gcc 生成的代码与 D2XX 库二进制不兼容,并且不允许链接到它们。
To fix this I downloaded the soft-float debian "wheezy" distribution(2012-08-08) and wrote to the 4 GB SD card. This time the image would not boot. After looking around I found this helpful answer. In short, there's a problem with the boot image for the soft-float so that for some Raspberry Pi boards, it won't boot. The solution is to replace the start.elf file on the soft-float distribution with one that does work e.g. a copy from the hard-float Raspbian image. Fortunately, the SD card has two partitions: a FAT one and an ext3(?) one. The boot image is on the FAT partition, so it was trivial to pop the hard-float SD card into a Windows box, copy the start.elf file, pop in the soft-float SD card and update its start.elf with the hard-float one. After that, the Raspberry Pi booted no problem.
为了解决这个问题,我下载了soft-float debian "wheezy" 发行版(2012-08-08) 并写入了 4 GB SD 卡。这次映像将无法启动。环顾四周后,我找到了这个有用的答案. 简而言之,软浮动的启动映像存在问题,因此对于某些 Raspberry Pi 板,它无法启动。解决方案是将软浮动发行版上的 start.elf 文件替换为有效的文件,例如来自硬浮动 Raspbian 映像的副本。幸运的是,SD 卡有两个分区:FAT 分区和 ext3(?) 分区。启动映像在 FAT 分区上,因此将硬浮动 SD 卡弹出到 Windows 盒中,复制 start.elf 文件,弹出软浮动 SD 卡并使用硬盘更新其 start.elf 是微不足道的。 - 浮动一个。之后,树莓派启动没有问题。
After installing FTDI's D2XX drivers and building a d2xx Python module from PyUSB, I tried the test script again. Again it failed. The d2xx module could read the libftd2xx.so library no problem, but for some reason could just not talk to the device.
在安装 FTDI 的 D2XX 驱动程序并从 PyUSB 构建 d2xx Python 模块后,我再次尝试了测试脚本。又失败了。d2xx 模块可以读取 libftd2xx.so 库没问题,但由于某种原因无法与设备通信。
采纳答案by TheDuke
I wasn't sure where the problem lay: Was it PyUSB, an issue with FTDI's libftd2xx.so or some issue with the Debian distribution?
我不确定问题出在哪里:是 PyUSB、FTDI 的 libftd2xx.so 的问题还是 Debian 发行版的问题?
With the FTDI package there's a test, under release/examples/EEPROM/read. You must build it, but that's simply a matter of typing make. Running it (via sudo), it failed to open the USB device, so clearly it wasn't PyUSB. After poking around, I found a reference to a driver, ftdi_sio, and that it could conflict with other D2XX drivers. So, using lsmod, I saw ftdi_sio was already installed by default, so I ran rmmod ftdi_sio. After that, everything worked. The read command should show something like this:
使用 FTDI 包有一个测试,在release/examples/EEPROM/read. 您必须构建它,但这只是键入 make 的问题。运行它(通过 sudo),它无法打开 USB 设备,所以很明显它不是 PyUSB。在四处寻找之后,我发现了一个对驱动程序 ftdi_sio 的引用,并且它可能与其他 D2XX 驱动程序发生冲突。所以,使用lsmod,我看到 ftdi_sio 已经默认安装,所以我运行rmmod ftdi_sio. 在那之后,一切都奏效了。读取命令应显示如下内容:
Library version = 0x10112
Opening port 0
FT_Open succeeded. Handle is 0xf7d240
FT_GetDeviceInfo succeeded. Device is type 4.
FT_EE_Read succeeded.
Signature1 = 0
Signature2 = -1
Version = 1
VendorId = 0x0407
ProductId = 0x6009
Manufacturer = MagicIncorporated
ManufacturerId = wo
Description = MyCompany Test Board
SerialNumber = testit_028
MaxPower = 44
PnP = 1
SelfPowered = 0
RemoteWakeup = 1
2232RC:
-------
Rev5 = 0x1
IsoInA = 0x0
IsoInB = 0x0
IsoOutA = 0x0
IsoOutB = 0x0
PullDownEnable5 = 0x0
SerNumEnable5 = 0x0
USBVersionEnable5 = 0x0
USBVersion5 = 0x110
AIsHighCurrent = 0x0
BIsHighCurrent = 0x0
IFAIsFifo = 0x0
IFAIsFifoTar = 0x0
IFAIsFastSer = 0x0
AIsVCP = 0x0
IFBIsFifo = 0x0
IFBIsFifoTar = 0x0
IFBIsFastSer = 0x0
BIsVCP = 0x0
Returning 0
I also switched from PyUSB to ftd2xx, because it's pure python, but it wasn't strictly necessary.
我也从 PyUSB 切换到ftd2xx,因为它是纯 python ,但这并不是绝对必要的。
回答by Mike
There is a special version of the libftd2xx 1.1.12 that fixes the problems on Raspberry pi. That would probably fix your problem.
有一个特殊版本的 libftd2xx 1.1.12 修复了 Raspberry pi 上的问题。那可能会解决您的问题。
I forgot where I found it, but I have a copy on the download page of my website, Lightput Download page.
我忘记在哪里找到它了,但我在我网站的下载页面 Lightput 下载页面上有一份副本。

