我正在测试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()的正确设置。