我正在尝试编写一个应用程序来读取和写入连接的USB设备。我正在使用libusb。在我找到设备之后,配置失败了。我正在http://libusb.sourceforge.net/doc/index.html关注libusb的开发人员指南。其中说明:
usb_set_configuration设置设备的活动配置。配置参数是描述符字段bConfigurationValue中指定的值。成功时返回0或<错误时为0。
我在我的源代码(下面)中演示了我使用bConfigurationValue作为配置参数,但是我从usb_set_configuration返回的值总是-1。我不明白为什么?
输出:
[user@local workspace]$ ./application
Device Found @ Address 005
Vendor ID 0x018d1
Product ID 0x04e12
INFO: bConfigurationValue = 1
config status =-1
claim status =-1
alt status =-22
TX status =-1
RX status =-1 -> RX char = 0
[user@local workspace]$
来源:
int main(int argc, char *argv[])
{
struct usb_bus *bus;
struct usb_device *dev;
struct usb_dev_handle *android_handle;
int status;
unsigned char send_data = 0x51;
unsigned char receive_data = 0;
usb_init();
if ((handle = locate_device()) == 0)
{
printf("Error: Could not open the device\n");
return (-1);
}
printf("INFO: bConfigurationValue = %d\n", config_val);
status = usb_set_configuration(handle, config_val);
printf("config status = %d\n", status);
status = usb_claim_interface(handle, 0);
printf("claim status = %d\n", status);
open_status = usb_set_altinterface(handle, 0);
printf("alt status = %d\n", status);
status = usb_bulk_write(handle, 4, &send_data, 1, 500);
printf("TX status = %d\n", status);
usb_bulk_read(handle, 3, &receive_data, 1, 500);
printf("RX status = %d -> RX char = %d\n", status, receive_data);
usb_close(handle);
return 0;
}
usb_dev_handle *locate_device(void)
{
...
config_val = dev->config[myDeviceIndex].bConfigurationValue;
...
}
编辑:
以root身份运行应用程序时,将返回以下错误代码:
Device Found @ Address 005
Vendor ID 0x018d1
Product ID 0x04e12
INFO: bConfigurationValue = 1
config status = -16
claim status = -16
alt status = -22
TX status = -16
RX status = -16 -> RX char = 0