我一直在Linux上使用LibUSB开发USB驱动程序,但现在我想为Windows编译我的一个驱动程序(这是我第一次这样做).
我正在使用MinGW编译器(也使用Dev-cpp IDE)在Windows 7上工作,我正在使用从此链接下载的预编译的libusb库.
我的设备:这是一款HID触控设备.因此Windows不需要驱动程序.我有一个额外的端点来获取某些调试数据.
我已编译代码列出连接到我的机器的所有设备和USB设备,代码可以工作.现在我添加代码来打开设备,以便获得设备句柄并开始通信.但是函数返回-12即LIBUSB_ERROR_NOT_SUPPORTED.
我该如何解决这个问题?
我在互联网上搜索并没有找到解决这个问题的明确方法.虽然它的代码在Linux上运行得很好.
PS:我在下面添加了整个代码.该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);
}
Run Code Online (Sandbox Code Playgroud)
ant*_*duh 10
您似乎需要安装winusb驱动程序 - libusb可以获取有关没有此驱动程序的设备的信息,但它无法打开它们.
http://libusb.6.n5.nabble.com/LIBUSB-ERROR-NOT-SUPPORTED-td5617169.html:
2012年4月4日星期三下午11:52,QuânPhạmMinh<[hidden email]>写道:
虽然我从来没有安装winusb驱动程序,但我使用libusb来获取我的usb信息(金士顿usb,已经被系统识别)
是的,这是可能的.但是你无法打开设备并做更多事情.对于新用户来说,这是关于libusb Windows后端的混乱部分,同样对于Mac OS X也是如此.libusb可以获取具有不正确驱动程序的设备的一些基本信息(例如:USB大容量存储设备),但是如果不将驱动程序更改为支持的驱动程序,则无法打开设备.
- 小凡
您可以通过使用Zadig轻松安装WinUSB驱动程序或libusb支持的其他驱动程序(libusb-win32和libusbK),Zadig是为解决此问题而开发的应用程序.请参阅https://zadig.akeo.ie.
有一点要记住,虽然是如果你更换了WinUSB大容量存储驱动程序或HID驱动(的Windows自动安装),你将只能通过libusb的访问设备,并且将无法访问您的在卸载WinUSB驱动程序之前,设备将作为大容量存储或HID.
最后,如果你有固件为您的设备的控制,还可以创建一个自动在Vista上安装了WinUSB驱动程序或更高版本,让用户不必去通过手动安装驱动程序的设备(这可能需要Windows Update for Windows 7或更早版本的连接,但即使没有Windows 8或更高版本的Internet连接也应该可以正常工作.请参阅https://github.com/pbatard/libwdi/wiki/WCID-Devices.
[免责声明]我是Zadig/libwi的作者,WCID维基页面以及libusb Windows后端的贡献者.
| 归档时间: |
|
| 查看次数: |
22069 次 |
| 最近记录: |