Questions tagged [hidapi]

HIDAPI is a multi-platform library which allows an application to interface with USB and Bluetooth HID-Class devices on Windows, Linux, and Mac OS X.

HIDAPI is a multi-platform library which allows an application to interface with USB and Bluetooth HID-Class devices on Windows, Linux, and Mac OS X. While it can be used to communicate with standard HID devices like keyboards, mice, and Joysticks, it is most useful when used with custom (Vendor-Defined) HID devices. Many devices do this in order to not require a custom driver to be written for each platform. HIDAPI is easy to integrate with the client application, just requiring a single source file to be dropped into the application. On Windows, HIDAPI can optionally be built into a DLL.

Programs which use HIDAPI are driverless, meaning they do not require the use of a custom driver for each device on each platform.

HIDAPI provides a clean and consistent interface for each platform, making it easier to develop applications which communicate with USB HID devices without having to know the details of the HID libraries and interfaces on each platform.

The HIDAPI source also provides a GUI test application which can enumerate and communicate with any HID device attached to the system. The test GUI compiles and runs on all platforms supported by HIDAPI.

Example

The sample program, which communicates with a modified version of the USB Generic HID sample which is part of the Microchip Application Library (in folder "Microchip Solutions\USB Device - HID - Custom Demos\Generic HID - Firmware" when the Microchip Application Framework is installed), looks like this (with error checking removed for simplicity):

#include <stdio.h>
#include <stdlib.h>

#include "hidapi.h"


int main(int argc, char* argv[])
{
    int res;
    unsigned char buf[65];
    #define MAX_STR 255
    wchar_t wstr[MAX_STR];
    hid_device *handle;
    int i;

    // Enumerate and print the HID devices on the system
    struct hid_device_info *devs, *cur_dev;

    devs = hid_enumerate(0x0, 0x0);
    cur_dev = devs; 
    while (cur_dev) {
        printf("Device Found\n  type: %04hx %04hx\n  path: %s\n  serial_number: %ls",
            cur_dev->vendor_id, cur_dev->product_id, cur_dev->path, cur_dev->serial_number);
        printf("\n");
        printf("  Manufacturer: %ls\n", cur_dev->manufacturer_string);
        printf("  Product:      %ls\n", cur_dev->product_string);
        printf("\n");
        cur_dev = cur_dev->next;
    }
    hid_free_enumeration(devs);


    // Open the device using the VID, PID,
    // and optionally the Serial number.
    handle = hid_open(0x4d8, 0x3f, NULL);

    // Read the Manufacturer String
    res = hid_get_manufacturer_string(handle, wstr, MAX_STR);
    printf("Manufacturer String: %ls\n", wstr);

    // Read the Product String
    res = hid_get_product_string(handle, wstr, MAX_STR);
    printf("Product String: %ls\n", wstr);

    // Read the Serial Number String
    res = hid_get_serial_number_string(handle, wstr, MAX_STR);
    printf("Serial Number String: %ls", wstr);
    printf("\n");

    // Send a Feature Report to the device
    buf[0] = 0x2; // First byte is report number
    buf[1] = 0xa0;
    buf[2] = 0x0a;
    res = hid_send_feature_report(handle, buf, 17);

    // Read a Feature Report from the device
    buf[0] = 0x2;
    res = hid_get_feature_report(handle, buf, sizeof(buf));

    // Print out the returned buffer.
    printf("Feature Report\n   ");
    for (i = 0; i < res; i++)
        printf("%02hhx ", buf[i]);
    printf("\n");

    // Set the hid_read() function to be non-blocking.
    hid_set_nonblocking(handle, 1);

    // Send an Output report to toggle the LED (cmd 0x80)
    buf[0] = 1; // First byte is report number
    buf[1] = 0x80;
    res = hid_write(handle, buf, 65);

    // Send an Output report to request the state (cmd 0x81)
    buf[1] = 0x81;
    hid_write(handle, buf, 65);

    // Read requested state
    res = hid_read(handle, buf, 65);
    if (res < 0)
        printf("Unable to read()\n");

    // Print out the returned buffer.
    for (i = 0; i < res; i++)
        printf("buf[%d]: %d\n", i, buf[i]);

    return 0;
}

License

HIDAPI may be used by one of three licenses as outlined in LICENSE.txt. These licenses are:

  • GPL v3 (see LICENSE-gpl3.txt),
  • BSD (see LICENSE-bsd.txt),
  • The more liberal original HIDAPI license (see LICENSE-orig.txt).

Download

HIDAPI can be downloaded from GitHub

98 questions
0
votes
0 answers

Mechanism to generate a dynamically sized USB HID Report

In order to allow for sending feature reports to my device in Windows, I am using the following special HID report in my USB device. It lets me use hidapi's set_feature_request / get_feature_request from userspace programs on report ID 0xAA... And…
Charles Lohr
  • 695
  • 1
  • 8
  • 23
0
votes
0 answers

What is this error message: hid_write/GetOverlappedResult: (0x000003E3) in hidapi-rs?

I am using HID to write data to a microcontroller and it works perfectly upon start-up allowing me to send the data. However, sometimes I receive the error message "hid_write/GetOverlappedResult: (0x000003E3) the I/O operation has been aborted…
0
votes
0 answers

Emulating gamepad with python hid module doesn't work

I am trying to capture some keyboard keys in python and trying to emulate a controller input which is connected to the USB port with a wire. I stumbled across hid module and was able to successfully read the report in my code. But when i use…
0
votes
0 answers

The HIDAPI.read Function doesn't return read data from usb

I am trying to create a program to send n bytes to HID device and then reading them from the device. I m using a hexadecimal list, for eg.[0x00,0x01,0x02...0x0n] as the data to be sent and recieved. My problem is that even though the data is sent…
prathetic
  • 1
  • 1
0
votes
0 answers

HidApi.read returning -2(DEVICE_ERROR) and "Device not initialised" seen when trying to fetch SN, PID

I am trying to open and read from a TRF7970A EVM board (by Texas Instruments) connected to my Mac. The device shows up in ioreg -lfxp IOUSB as a "USB to UART Bridge Controller" and gives the vid, pid and serial number. The device path in my Mac is…
Koushik
  • 1
  • 1
0
votes
0 answers

hidapi-rs can't find USB device

I have a USB2CAN device plugged into my computer. hidapi-rs for Rust can't see this device, but when I use rusb, I can see the device, but I can't connect to it (NotSupported error). The USB2CAN device VID is 32903 (u16) and the PID is 38 (u16). Is…
0
votes
0 answers

Python: Manage HID device using hidapi

I'm trying to read the output of an HID device (NFC Tag Reader: R-DT-EVO-HF2-HID) using hidapi on python 3.9.6. (Windows 10) `class HID_Reader(): def init(self): self.port = hid.device() a = hid.enumerate() self.port.open(0x10C4,…
Antoine
  • 1
  • 1
0
votes
1 answer

HIDAPI doesn't find and is not able to connect to USB HID device

I am trying to control a USB HID device (RF transceiver dongle for a weather station) using python and the hidapi module, but I am not able to open the device nor does it show up when using hid.enumerate. I am able to find and connect to the device…
gugelhüpf
  • 85
  • 2
  • 10
0
votes
0 answers

libusb will not link correctly when cross-compiling Rust hidapi-rs library to Android

I am writing an app for android that interfaces with HID devices. The backend is written in Rust using hidapi-rs and uses Jetpack Compose for the UI. I compile my rust library into a shared library using rust-android-gradle and can successfully load…
0
votes
1 answer

Cannot read from HID device after migrating from fileapi.h ReadFile in windows to hidapi hid_read in Ubuntu

I have a library for communicating with a HID device. The library works fine under windows using fileapi.h and read/write with ReadFile and WriteFile. I now want to make this library platform independent so I decided to migrate to hidapi. On Ubuntu…
0
votes
1 answer

Capture mouse HID packets on windows using C++?

I want to ditch Razer Synapse because it eats up to 600MB of RAM for nothing. I just want to use my macros for two additional keys found on my Razer Deathadder. I've sucessfully captured the HID packets for my Corsair K95 keyboard with C++ HID API…
kurta999
  • 60
  • 8
0
votes
0 answers

Python hidapi, why can't read my keyboard via usb?

I am trying to read the data input from my keyboard in python hidapi, and print it to the console. (This is the keyboard I am currently using, but I would like to read the USB with a python code too) I can't get it to work. At data =…
rupiman
  • 11
  • 2
0
votes
0 answers

Unable to open HID device in MacOS With Electron Universal macOS applications(Packaging with Electron Builder)

I used the HidAPI hid_open_path() function in V8 C++ to open the device for Node.js on the Electron platform, HidAPI function:from here First, execute the program in Debug Mode, and the device can be turned on successfully The recipient uses…
0
votes
0 answers

Compatibility issue between Rust's crates tokio and hidapi

I need to use hid device with tokio. I am using tokio="1.22.0" and hidapi="2.0.2". Here is a code snippet with shorthands. pub async fn new_hid( to_net : tokio::syn::cmpsc::Sender>, mut from_net :…
Alex K.
  • 93
  • 4
0
votes
0 answers

Is it possible to configure the Mindeo MP725 scanner programmatically?

I'm trying to send/read data via USB, in Windows. Using the library hid\hidapi (python). It is the only one that sees\working with the Mindeo MP725 on the system. QR Scanner Definition Example: {'path': b'\\\\?…