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
1 answer

Can't build hidapi rust example

filip@filip-pc:~/Desktop/hidTest$ cargo build Compiling pkg-config v0.3.17 Compiling libc v0.2.71 Compiling cc v1.0.54 Compiling hidapi v1.2.2 error: failed to run custom build command for `hidapi v1.2.2` Caused by: process didn't…
Filip Laurentiu
  • 854
  • 3
  • 9
  • 29
0
votes
1 answer

Hidapi hid_write() returns error (Windows 10, C++)

I've been trying to control the LEDs on my mouse using hidapi but whenever I run hid_write() it returns -1. I've found that the error is being thrown inside the function when WriteFile() is called and that the error is "Access is denied". There is…
0
votes
0 answers

Difference between HID class and Audio class?

I was working on USB Devices. Every where i get examples as speaker for Audio class and keyboard, mouse for HID class. As per my understanding we use HID class API's to enumerate audio devices like speaker etc., so what more extra functionality is…
Karthikgr
  • 81
  • 13
0
votes
1 answer

How to build HIDAPI library in release mode on Linux?

I am trying to create a stripped .so file for the HIDAPI library. I cloned the library from https://github.com/libusb/hidapi.git. I followed the steps for building on a Linux system: ./bootstrap ./configure make sudo make install It is generating…
Yash
  • 9
  • 3
0
votes
1 answer

How do i get data from recursive structure got from C function?

I'm writing a wrapper for usb hid device and want to use hidapi for that. In process of writing/learning got a pointer on recursive structure. How can i get data from this? I tried to get data from contents, but there was only _field_…
bydan14
  • 21
  • 5
0
votes
1 answer

hdiapi how to access device in user mode?

I am running the test script from GitHub, and I can only open the device as root. While calling hid_enumerate(0x0, 0x0), it works in user mode. My udev rule looks like this: SUBSYSTEM=="usb", ATTRS{idVendor}=="1a86", ATTR{idProduct}=="e008", …
Alfredo cubitos
  • 161
  • 2
  • 5
0
votes
1 answer

How to convert hidapi read output to string in python?

I'm reading a simple QR code with json text: {test:Hi} import hid import time h = hid.device() h.open(0x1eab, 0x8003) print("Manufacturer: %s" % h.get_manufacturer_string()) print("Product: %s" % h.get_product_string()) print("Serial No: %s" %…
user1872384
  • 6,886
  • 11
  • 61
  • 103
0
votes
1 answer

HID USB control transfer (Windows)

Recently, I started to learn about communicating with HID devices on Windows 10. But I got a few questions. According to the Window's document, HidD_SetOutputReport must set the first byte of the ReportBuffer parameter to a report ID or 0x00. Since…
Teddy
  • 13
  • 1
  • 5
0
votes
1 answer

How to use HIDAPI in Qt Creator?

I unpacked the entire hidapi-0.7.0 download (https://github.com/downloads/signal11/hidapi/hidapi-0.7.0.zip) into my project directory, then added hidapi-0.7.0/hidapi/hidapi.h and hidapi-0.7.0/linux/hid-libusb.c into my project. So they now appear…
AaronD
  • 503
  • 1
  • 7
  • 23
0
votes
1 answer

node-hid HID.devices() function returning empty array on windows

I am using node-hid library on Windows 10 64 bit. Its installed perfectly but when I attached any USB device. HID.devices() returning empty array ( [] ). The same code shows array of objects perfectly for the device on Linux or MAC platform. What…
simply-put
  • 1,068
  • 1
  • 11
  • 20
0
votes
1 answer

How do I keep polling HIDAPI in wxWidgets?

Moving from the world of Embedded Micro controllers and C, to C++ with wxWidgets. I've created a simple GUI program, using codeblocks and wxWidgets to interface with a USB Hid device I've made using the HIDAPI from signal11. Using simple buttons, I…
Xander
  • 1
  • 1
0
votes
1 answer

STM32f4 HID receive data

How to receive OUT report data from HOST PC in STM32f407 discovery board running as HID(USB) in device mode? Is it possible? I am thinking to send data from host using hidapi.
S Sreejit
  • 21
  • 5
0
votes
1 answer

hidapi on windows - undefined reference to functions

I build the hidapi on windows using Visual Studios. The build generated a 'hidapi.lib' file. Now I am trying to write C code using Eclipse IDE and have included the 'hidapi.lib' by changing it's name to 'libhid.lib'. The hidapi.h is also present and…
the_mamba
  • 119
  • 1
  • 2
  • 10
0
votes
0 answers

Chrome API HID receive not working in Windows

I have a chromapp which sends data to PC using usb, connected as an HID device, working perfectly in Linux. While trying to do the same in windows, the app sees that the device is connected but throws an runtime error : Unchecked runtime.lastError…
S Sreejit
  • 21
  • 5
0
votes
0 answers

Human Interface Device sharing [HIDAPI]

I have a Human Interface Device (HID) that I'm trying to talk to with HIDAPI and I am having some difficulty talking to it when another program has a handle on the device; namely the device vendor's development software. The device in question is an…
audiFanatic
  • 2,296
  • 8
  • 40
  • 56