我正在尝试使用基于FTDI 的USB设备,但出现 -32 (EPIPE) 错误:
08-06 16:32:16.328: WARN/System.err(15547): ftdi_usb_open_dev()
08-06 16:32:16.328: WARN/System.err(15547): usb_detach_kernel_driver_np()libusb: 0.029116 debug [libusb_detach_kernel_driver] interface 0
08-06 16:32:16.328: WARN/System.err(15547): ftdi claim interface ...
08-06 16:32:16.328: WARN/System.err(15547): libusb-compat debug: usb_claim_interface: interface 0
08-06 16:32:16.328: WARN/System.err(15547): libusb: 0.030246 debug [libusb_claim_interface] interface 0
08-06 16:32:16.328: WARN/System.err(15547): claiming interface using fd = 4
08-06 16:32:16.328: WARN/System.err(15547): ftdi_usb_reset ...
08-06 16:32:16.328: WARN/System.err(15547): libusb-compat debug: usb_control_msg: RQT=40 RQ=0 V=0 I=0 len=0 timeout=300
08-06 16:32:16.328: WARN/System.err(15547): libusb: 0.031222 debug [libusb_get_next_timeout] next timeout in 0.300000s
08-06 16:32:16.328: WARN/System.err(15547): libusb: 0.031527 debug [libusb_handle_events_timeout_completed] doing our own event handling
08-06 16:32:16.328: WARN/System.err(15547): libusb: 0.032046 debug [handle_events] poll() 2 fds with timeout in 300ms
08-06 16:32:16.328: WARN/System.err(15547): libusb: 0.033023 debug [handle_events] poll() returned 1
08-06 16:32:16.338: WARN/System.err(15547): libusb: 0.033389 debug [reap_for_handle] urb type=2 status=-32 transferred=0
08-06 16:32:16.338: WARN/System.err(15547): libusb: 0.033755 debug [handle_control_completion] handling completion status -32
08-06 16:32:16.338: WARN/System.err(15547): libusb: 0.034091 debug [handle_control_completion] unsupported control request
08-06 16:32:16.338: WARN/System.err(15547): libusb: 0.034366 debug [usbi_handle_transfer_completion] transfer 0x2915e0 has callback 0x5ccb4
08-06 16:32:16.338: WARN/System.err(15547): libusb: 0.034732 debug [ctrl_transfer_cb] actual_length=0
USB 请求似乎与FTDI 芯片命令所要求的完全一致。
FTDI 上下文初始化没有错误,usb_dev 不为空,似乎没问题。电缆没问题,因为我可以用它来将Arduino草图上传到Duemilanove (FTDI) 板上。
所以我完全卡住了..我该怎么办?
我的代码
struct ftdi_context *ftdi_ctx;
struct usb_device *dev;
usb_dev_handle *udev;
// ...
ftdi_ctx = ftdi_new();
if (ftdi_ctx == NULL) {
fprintf(stderr, "error init ftdi context\n");
return 1;
}
ftdi_ctx->usb_write_timeout = 0;
ftdi_ctx->usb_read_timeout = 0;
// ...
udev = usb_open(dev);
int ret = ftdi_usb_open_dev(ftdi_ctx, dev, udev);
if (ret < 0) {
fprintf(stderr, "error opening ftdi device\n");
return ret;
}
ftdi_usb_open_dev()
稍作修改以做好准备usb_device
,不要在里面做 usb_open :
libftdi-0.1 代码 (ftdi.c):
int ftdi_usb_open_dev(struct ftdi_context *ftdi, struct usb_device *dev, struct usb_dev_handle *usb_dev)
{
int detach_errno = 0;
int config_val = 1;
fprintf(stderr, "ftdi_usb_open_dev()\n");
if (ftdi == NULL) {
fprintf(stderr, "ftdi context invalid\n");
ftdi_error_return(-8, "ftdi context invalid");
}
// 4ntoine (no need to open device if usb_dev is already passed)
if (usb_dev == NULL) {
if (!(ftdi->usb_dev = usb_open(dev)))
ftdi_error_return(-4, "usb_open() failed");
} else {
ftdi->usb_dev = usb_dev;
}
#ifdef LIBUSB_HAS_GET_DRIVER_NP
// Try to detach ftdi_sio kernel module.
// Returns ENODATA if driver is not loaded.
//
// The return code is kept in a separate variable and only parsed
// if usb_set_configuration() or usb_claim_interface() fails as the
// detach operation might be denied and everything still works fine.
// Likely scenario is a static ftdi_sio kernel module.
fprintf(stderr, "detaching kernel driver... \n");
if (ftdi->module_detach_mode == AUTO_DETACH_SIO_MODULE)
{
fprintf(stderr, "usb_detach_kernel_driver_np() ...\n");
if (usb_detach_kernel_driver_np(ftdi->usb_dev, ftdi->interface) != 0 && errno != ENODATA) {
fprintf(stderr, "failed to detach\n");
detach_errno = errno;
}
}
#endif
fprintf(stderr, "ftdi claim interface ...\n");
if (usb_claim_interface(ftdi->usb_dev, ftdi->interface) != 0)
{
fprintf(stderr, "failed to claim interface\n");
ftdi_usb_close_internal (ftdi);
if (detach_errno == EPERM)
{
ftdi_error_return(-8, "inappropriate permissions on device!");
}
else
{
ftdi_error_return(-5, "unable to claim usb device. Make sure the default FTDI driver is not in use");
}
}
fprintf(stderr, "ftdi claimed interface\n");
fprintf(stderr, "ftdi_usb_reset ...\n");
if (ftdi_usb_reset (ftdi) != 0)
{
ftdi_usb_close_internal (ftdi);
ftdi_error_return(-6, "ftdi_usb_reset failed");
}
我已经用另一个 FTDI 板(Arduino Nano v3)对其进行了测试,但仍然出现同样的错误,所以问题很可能不在板上......
我已经在另一台支持 USB 主机的 Android 设备和另一个 Android 操作系统版本(4.0.x)上对其进行了测试,但仍然是同样的错误......