0

我一直在尝试使用 hidapi 控制鼠标上的 LED,但每当我运行 hid_write() 时,它都会返回 -1。我发现调用 WriteFile() 时在函数内部引发了错误,并且错误是“访问被拒绝”。有一个软件 (iCue) 可以控制 LED 并使用 wireshark 我想我已经知道要发送给鼠标的内容了:Wireshark 截图

我的代码:

int main() {
CorsairHIDDevices corsair_devs; //Initialize vector of HID devices
int num = detectCorsairMouseHIDDevice(corsair_devs); //Enumerates HID devices and adds the mouse devices to corsair_devs. Returns number of devices added.
hid_device* handle;
handle = hid_open(corsair_devs[0].vendor_id, corsair_devs[0].product_id, nullptr);

//Creates message to send to mouse based on info from wireshark
int logoLed = 1;
int sideLed = 1;
int scrollLed = 1;
int ledCode = logoLed + 2 * sideLed + 4 * scrollLed;
int red = 255;
int green = 0;
int blue = 0;
uint8_t buf[64] = { 0 };
buf[0] = 0x07; buf[1] = 0xaa;
buf[4] = ledCode;
buf[5] = 0x07; buf[8] = 0x64;
buf[9] = red; buf[12] = red;
buf[10] = green; buf[13] = green;
buf[11] = blue; buf[14] = blue;
buf[15] = 0x05;

hid_write(handle, buf, 64); //Sends message to mouse. Returns -1 (error).
}

枚举时,有 5 个设备对应于鼠标,我不确定这意味着什么或如何确定要使用哪个设备,但我尝试了所有设备,但都没有工作。我没有很多使用 hidapi 的经验;大部分代码都是从这里重新使用的。任何帮助将不胜感激。谢谢。

4

1 回答 1

0

没关系,我让它工作了。我放弃了 hidapi 并让它与 libusb 一起工作。

libusb_init(NULL);

libusb_device** devs;
libusb_device* mouse_dev = NULL;
int numDevs = libusb_get_device_list(NULL, &devs);
for (int i = 0; i < numDevs; i++) {
    libusb_device* dev = devs[i];
    libusb_device_descriptor desc = { 0 };
    libusb_get_device_descriptor(dev, &desc);
    if (desc.idVendor == 0x1B1C) {
        mouse_dev = dev;
    }
}
libusb_device_handle* handleCorsair;
libusb_open(mouse_dev, &handleCorsair);

if (libusb_kernel_driver_active(handleCorsair, 0) == 1) {
    libusb_detach_kernel_driver(handleCorsair, 0);
}

libusb_claim_interface(handleCorsair, 1);

libusb_free_device_list(devs, 1);

int logoLed = 1; //Set colour for logo LED? 
int sideLed = 1; //Set colour for side LED?
int scrollLed = 1; //Set colour for scroll LED?
int ledCode = logoLed + 2 * sideLed + 4 * scrollLed;

int red = 255; //Intensity of red channel
int green = 255; //Intensity of green channel
int blue = 255; //Intensity of blue channel

uint8_t buff[64] = { 0 };
buff[0] = 0x07; buff[1] = 0xaa; buff[4] = ledCode; buff[5] = 0x07; buff[8] = 0x64;
buff[9] = red; buff[12] = red;
buff[10] = green; buff[13] = green;
buff[11] = blue; buff[14] = blue;
buff[15] = 0x05;

libusb_interrupt_transfer(handleCorsair, 0x02, buff, 64, NULL, 0);

libusb_release_interface(handleCorsair, 1);
libusb_close(handleCorsair);
libusb_exit(NULL);
于 2020-03-20T00:49:46.757 回答