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
2
votes
0 answers

Issues with cython-hidapi, send_feature_report or chr()

I have this snippet of code which works fine on Windows and Mac OS X using Python 2.7.6. I am using the cython-hidapi intereface to read data from a Voltcraft VC870 power meter. To get the data, if first need to send a feature report to the device…
oche
  • 939
  • 10
  • 19
2
votes
1 answer

hidapi: Sending packet smaller than caps.OutputReportByteLength

I am working with a device (the wiimote) that takes commands through the DATA pipe, and only accepts command packets that are EXACTLY as long as the command itself. For example, it will accept: 0x11 0x10 but it will not accept: 0x11 0x10 0x00 0x00…
Flafla2
  • 545
  • 7
  • 12
2
votes
1 answer

Two threads, one object

I'm writing a Linux driver for a USB HID device in Python. The device has two ways it sends data, both of which are needed: feature reports (synchronous) and input reports (asynchronous). Using the hidapi Cython library I have only one instance of…
Devin Young
  • 841
  • 7
  • 21
2
votes
0 answers

JAVA hid api and usb keyboard

I have a UIC 680 contactless card reader which has hid and RS 232 interfaces. Now I want to interact with the device using java and hid interface. I started exploring JAVAHID API and was able to get the device list. But when I try to get the device…
red devil
  • 113
  • 8
2
votes
0 answers

Java HIDAPI - Asynchronous reading

Sort of a follow up to this question, which I have now had a measure of success with (the LED turns on and off, hooray!), I now have another question. Besides writing to the device, I also have a need to read from the device. I would prefer NOT to…
Will
  • 3,413
  • 7
  • 50
  • 107
2
votes
2 answers

Java and HID Communication

I'm looking to write a Linux/Windows/Mac Java HID controller for a simple wireless HID interface device. I've tinkered around with the USB4Java LibUsb library to no avail, and I've been steered in the direction of the JavaHIDAPI. Unfortunately for…
Will
  • 3,413
  • 7
  • 50
  • 107
2
votes
1 answer

Hidapi unable to compile any code under ubuntu

I am trying to use the hidapi library under Ubuntu 12.04. I have followed the tutorial in github, however, not even the test code that comes with it is working. It always complains about undefined reference. I cannot find any solution. I cannot find…
Fred
  • 417
  • 6
  • 14
2
votes
3 answers

How can a daemon user access HID devices without getting kIOReturnNotPrivileged when calling IOHIDDeviceOpen

I'm using hidapi for accessing a HID device (which is not a mouse or a keyboard). The oldest OS version to be supported is 10.6, I'm currently using Mac OX X 10.6.8. If my Mac OS X executable is running with normal user rights it can enumerate the…
ur.
  • 2,890
  • 1
  • 18
  • 21
1
vote
2 answers

Can't load hidapi with Python library "hid" on Windows

sorry if the question is not well asked) I'm trying to use the Python library hid, which rely on the hidapi library. hid seems to not be able to load hidapi, as it's telling me here: $ python Python 3.10.1 (tags/v3.10.1:2cd268a, Dec 6 2021,…
Neozetare
  • 11
  • 1
  • 3
1
vote
0 answers

how to install hidapi on windows with cygwin

I downloaded the source code of the back-end from https://github.com/libusb/hidapi I used to install it cygwin following the instruction ./bootstrap ./configure make make install I installed hid using pip install hid but once I try import hid in…
1
vote
2 answers

I can't see a bluetooth device (Joy-Con) using cython-hidapi library

Device/OS: Jetson Nano / Ubuntu 18.04 Python 3 There is a Bluetooth dongle attached. Joy-Con is connected as seen in UI: As well as when I do this: $ hcitool con Connections: > ACL B8:78:26:19:C1:8C handle 11 state 1 lm MASTER AUTH…
Aleksandr Motsjonov
  • 1,230
  • 3
  • 14
  • 25
1
vote
0 answers

Where can I find the IBM SurePOS Protocol?

I am working on a driver for scanners. I have a Honeywell, Datalogic, and Symbol scanner. Honeywell uses HID POS DataLogic and Symbol use IBM SurePOS I'm having a very hard time finding any information on the protocol. I have found bits on forms,…
Austen Stone
  • 948
  • 1
  • 10
  • 21
1
vote
1 answer

How to use the hidapi library from Signal11?

I installed the hidapi library from Signall11 on my windows10 pc (using minGW). But now I'm having some trouble actually getting it to work with gcc. I have some main.c file in which I include the hidapi.h file. My gcc command looks like gcc…
1
vote
0 answers

How to set pyhidapi library to search hidapi.dll in project folder?

I'm trying to compile my application with cx_Freeze. On development machine (with Windows 10) the program is working well. But when i try it on other machine (with Windows 7) I get an error message. Strange that when i try it on another machine with…
bydan14
  • 21
  • 5
1
vote
0 answers

HIDAPI on linux doesn't seem to recognize descriptors

Got a device reported in dmesg as: [ 7679.312788] hid-generic 0003:16D0:0E70.0012: hiddev0,hidraw0: USB HID v1.01 Device [.de.nonchip TinyStick HIDSTM1640] on usb-0000:00:13.0-3/input0 the actual HID descriptor (as I put it into the device's…
nonchip
  • 1,084
  • 1
  • 18
  • 36