1

我正在为 Arduino 测试我的 PyUSB 代码,它使用 FT232 作为接口 IC。arduino mcu 只是在 9600 中打印出一些字符。

在努力解决它的文档之后,我可以让它工作。但是我只能从它的 EP 中读取一些垃圾字符。

import usb.core, usb.util

dev = usb.core.find(idVendor=0x0403, idProduct=0x6001)

if dev is None:
    raise ValueError("FT232 not found. Have U enable it in LibUSB filter driver?")

dev.ctrl_transfer(0x40, 0x00, 0, 0, '')
dev.ctrl_transfer(0x40, 0x01, 0, 0, '')
dev.ctrl_transfer(0x40, 0x02, 0, 0, '')
dev.ctrl_transfer(0x40, 0x03, 0x4138, 0, '') # libFTDI 9600 bps

cfg = dev.get_active_configuration()
intf = cfg[(0,0)]
ep = intf[0]

while True:
    dat = dev.read(ep.bEndpointAddress, 64, intf, 100)        
    if len(dat)>2:
        print ""
        print ': '+ ''.join([chr(c) for c in dat])[2:len(dat)]
        for i in range(2,len(dat)):
            print '%02X'%dat[i],
        print ""

我不确定 dev.ctrl_transfer 部分,此代码从 libFTDI 声明。0x4138 用于将其设置为 9600bps。

我以前把这四个句子写成:dev.ctrl_transfer(0x40, 0x00, 0, 0, 0) dev.ctrl_transfer(0x40, 0x01, 0, 0, 0) dev.ctrl_transfer(0x40, 0x02, 0, 0, 0 ) dev.ctrl_transfer(0x40, 0x03, 0x4138, 0, 0) # libFTDI 9600bps

但我收到以下错误:

Traceback (most recent call last):
  File "C:\Downloads\SemiconDev\Freescale FRDM KL25Z\Software\libUsb\FT232_PyUSB.py", line 83, in <module>
    dev.ctrl_transfer(0x40, 0x00, 0, 0, 0)
  File "C:\Python25\Lib\site-packages\usb\core.py", line 693, in ctrl_transfer
    a = _interop.as_array(data_or_wLength)
  File "C:\Python25\Lib\site-packages\usb\_interop.py", line 135, in as_array
    a.fromstring(data)
TypeError: fromstring() argument 1 must be string or read-only buffer, not int

我仍在寻找 ctrl_transfer() 的正确设置。

4

0 回答 0