我一直在使用LibUSB在Linux上开发USB驱动程序,但现在我想让我的驱动程序之一为Windows编译(这是我第一次这么做)。
我的环境
我正在使用MinGW编译器(也使用Dev-cpp )编写Windows 7,并使用从此链接下载的预编译libusb库。
我的设备:,这是一个隐藏的触摸设备。所以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);
}
发布于 2013-06-27 18:23:42
似乎您需要安装winusb驱动程序- libusb可以在没有此驱动程序的情况下获取有关设备的信息,但它无法打开它们。
http://libusb.6.n5.nabble.com/LIBUSB-ERROR-NOT-SUPPORTED-td5617169.html
2012年4月4日,上午11点52分,Qu Phạm Minh 写道: 虽然我从来没有安装winusb驱动程序,但是我使用libusb获取我的usb (kingston usb,并且已经被系统识别)的信息,是的,这是可能的。但是你不能打开设备做进一步的事情。对于新用户来说,libusb Windows后端和Mac也是一个令人困惑的部分。libusb可以为非正常驱动程序的设备获取一些基本信息(例如: USB海量存储设备),但如果不将驱动程序更改为支持的驱动程序,则无法打开设备。 -小凡
发布于 2014-07-19 15:14:14
通过使用Zadig,您可以轻松地安装WinUSB驱动程序或libusb支持的其他驱动程序(libusb 32和libusbK),该应用程序就是为了解决这个问题而开发的。见https://zadig.akeo.ie。
但是,要记住的一点是,如果用WinUSB替换海量存储驱动程序或HID驱动程序( Windows会自动安装该驱动程序),则只能通过libusb访问设备,并且在卸载WinUSB驱动程序之前无法以海量存储方式访问设备。
最后,如果您控制了设备的固件,也可以创建在Vista或更高版本上自动安装WinUSB驱动程序的设备,这样用户就不必进行手动驱动程序安装(这可能需要连接WindowsUpdateforWindows7或更高版本,但即使没有Windows 8或更高版本的internet连接也可以工作)。见https://github.com/pbatard/libwdi/wiki/WCID-Devices。
免责声明我是Zadig/libwi,WCID wiki页面的作者,也是libusb Windows后端的贡献者。
https://stackoverflow.com/questions/17350177
复制相似问题