我的环境
我正在使用MinGW编译器(也使用Dev-cpp IDE)在Windows 7上工作,我正在使用从this link下载的预编译的libusb库.
我的设备:这是一款HID触控设备.因此Windows不需要驱动程序.我有一个额外的端点来获取某些调试数据.
我的代码:
我已编译代码列出连接到我的机器的所有设备和USB设备,代码可以工作.现在我添加代码来打开设备,以便获得设备句柄并开始通信.但该函数返回-12即LIBUSB_ERROR_NOT_SUPPORTED.
我该如何解决这个问题?
我在互联网上搜索并没有找到解决这个问题的明确方法.虽然它的代码在Linux上运行得很好.
P.S.:我在下面添加了整个代码. DoList();函数工作正常,但GetTRSDevice();功能在libusb_open(dev,& handle);失败.
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <libusb.h> libusb_device_handle* deviceHandle = NULL; int DoList(); libusb_device_handle* GetTRSDevice(void); int main() { int ret = libusb_init(NULL); if (ret < 0) { printf("Failed to init libusb"); return ret; } DoList(); deviceHandle = GetTRSDevice(); if(!deviceHandle) { printf("Failed to locate device"); goto fail_dev_open; } printf("Device opened"); libusb_close(deviceHandle); fail_dev_open: libusb_exit(NULL); return(ret); } int DoList() { libusb_device **devs; ssize_t cnt; cnt = libusb_get_device_list(NULL,&devs); if (cnt < 0) return (int) cnt; libusb_device *dev; int i = 0; while ((dev = devs[i++]) != NULL) { struct libusb_device_descriptor desc; int r = libusb_get_device_descriptor(dev,&desc); if (r < 0) { fprintf(stderr,"Failed to get device descriptor"); return(-1); } printf("%04x:%04x (bus %d,device %d)\n",desc.idVendor,desc.idProduct,libusb_get_bus_number(dev),libusb_get_device_address(dev)); } libusb_free_device_list(devs,1); return 0; } libusb_device_handle* GetTRSDevice(void) { int i = 0; ssize_t cnt; libusb_device *dev; libusb_device **devs; libusb_device_handle* handle = NULL; cnt = libusb_get_device_list(NULL,&devs); if (cnt < 0) { printf("Failed libusb_get_device_list"); return(0); } while ((dev = devs[i++]) != NULL) { struct libusb_device_descriptor desc; int ret = libusb_get_device_descriptor(dev,&desc); if (ret < 0) { printf("Failed libusb_get_device_descriptor"); continue; } if(desc.idVendor == 0X238f && desc.idProduct == 1) { int ret = libusb_open(dev,&handle); if (ret < 0) { printf("Failed libusb_open: %d\n\r",ret); break; } #ifndef WIN32 libusb_detach_kernel_driver(handle,0); #endif ret = libusb_claim_interface(handle,0); if (ret < 0) { libusb_close(handle); handle=NULL; break; } break; } } libusb_free_device_list(devs,1); return(handle); }
http://libusb.6.n5.nabble.com/LIBUSB-ERROR-NOT-SUPPORTED-td5617169.html:
On Wed,Apr 4,2012 at 11:52 PM,Quân Phạm Minh <[hidden email]>
wrote:although I never install winusb driver but I use libusb to get
information of my usb (kingston usb,and already
recognize by system)是的,这是可能的.但是你无法打开设备并做进一步的工作的东西.对于新用户而言,这是一个令人困惑的部分libusb Windows后端,同样适用于Mac OS X. libusb的可以获取具有不正确驱动程序的设备的一些基本信息(例如:USB大容量存储设备),但无法打开设备没有将驱动程序更改为支持的驱动程序. – 小凡