操作系统:Windows 10 64位
编译器:MSVC 19 std:c++20
静态链接
我有下面的代码,它只是初始化并打印有关设备的一些信息
#include "libusb.h"
#include <iostream>
int main()
{
libusb_context* cntx{ nullptr };
int status{ libusb_init(&cntx) };
if (status != LIBUSB_SUCCESS)
{
std::cerr << libusb_strerror(status);
return -1;
}
libusb_device** devices;
ssize_t numberOfDevices{ libusb_get_device_list(cntx, &devices) };
if (numberOfDevices <= 0)
{
std::cerr << "Device does NOT found\n";
return -1;
}
std::cout << "Found " << numberOfDevices << " Devices\n";
int index{ 0 };
std::cout << std::hex;
std::cout << "Device Address: " << +libusb_get_device_address(devices[index]) << '\n'
<< "Port Number: " << +libusb_get_port_number(devices[index]) << '\n'
<< "Bus Number: " << +libusb_get_bus_number(devices[index]) << '\n'
<< "Device Speed: ";
switch (libusb_get_device_speed(devices[index])
{
case LIBUSB_SPEED_SUPER: std::cout << "5Gb\n"; break;
case LIBUSB_SPEED_SUPER_PLUS: std::cout << "10Gb\n"; break;
case LIBUSB_SPEED_FULL: std::cout << "12Mb\n"; break;
case LIBUSB_SPEED_LOW: std::cout << "1.5Mb\n"; break;
case LIBUSB_SPEED_HIGH: std::cout << "480Mb\n"; break;
default: std::cout << "UNKNOWN\n"; break;
}
到目前为止一切顺利,但是当我想打开(例如)设备[0]时,LIBUSB_ERROR_NOT_SUPPORTED
将返回:
constexpr std::uint16_t VID{ 0x8086 };
constexpr std::uint16_t PID{ 0x1D26 };
libusb_device_handle* device{ nullptr };
status = libusb_open(devices[index], &device);
if (status)
{
std::cerr << "Can NOT open the device: " << libusb_strerror(status) << '\n';
device = libusb_open_device_with_vid_pid(cntx, VID, PID);
if (!device)
{
std::cerr << "Can NOT open the device with VID & PID\n";
return -1;
}
}
std::cout << "Device opened\n";
return 0;
}
都不起作用。 顺便说一句,该设备是由微程序员编程的,所以我不知道他是如何编程的,我的工作只是从设备中获取数据。
最佳答案
libusb可以枚举所有USB设备。但它只能打开安装了 WinUSB 驱动程序(或 libusbK 或 libusb0)的设备。 WinUSB 是直接与 USB 设备及其端点交互的通用驱动程序,无需实现和提供您自己的设备驱动程序。
如果设备未实现 Windows 提供并加载标准驱动程序的任何标准 USB 协议(protocol)(大容量存储、摄像头、音频、串行端口等),并且设备未附带其需要先安装自己的驱动程序。
在 Linux 和 macOS 上,这不是问题,因为没有专用驱动程序的 USB 设备可供应用程序使用,无需任何驱动程序麻烦。
为了安装WinUSB,Zadig可以使用。确保选择正确的设备,如果出现问题可以拔掉该设备。如果更换 USB 主 Controller 、键盘等关键设备的驱动程序,PC 可能无法再启动。
为了自动化WinUSB安装,设备可以实现额外的USB控制请求。有两种选择:
关于c++ - libusb_open 在 Windows 10 上返回 LIBUSB_ERROR_NOT_SUPPORTED,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72068206/