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

Cant open device on raspberry pi 3b+ with hidapi hidtest

pi@led-panel:~/hidapi/hidtest $ sudo ./test hidapi test/example tool. Compiled with hidapi version 0.13.0, runtime version 0.13.0. Compile-time version matches runtime version of hidapi. Device Found type: 17ef 6019 path: 1-1.1.3:1.0 …
0
votes
0 answers

Different number of incoming bytes from STM on Windows and Ubuntu

I have a code, which sending a message to usb device. It returns me the correct answer on Windows, but on ubuntu it gives wrong result. Why is that? import hid import time try: print("Opening the device") h = hid.device() …
qwerty
  • 1
0
votes
1 answer

Reading the output of a HID hand held barcode scanner in Python on Windows

I'm currently trying to write a service that should programmatically intercepts the output of a barcode scanner. I managed to get pyusb to work on Windows (has to install libusb-1.0 as a backend) and I can enumerate all connected USB devices along…
slouchart
  • 72
  • 6
0
votes
1 answer

Reading trigger data from xbox wireless controllers

So I'm using hidapi to reading input packets from a few xbox controllers I have and one thing frustrating me is that it reports the left and right triggers as a single "rudder" axis with a uint16_t. Is there any sort of packet I can send to change…
David Carpenter
  • 1,389
  • 2
  • 16
  • 29
0
votes
0 answers

Using HIDAPI, how can you raw data question

I'm using Trustmaster products. I'm going to bring raw data. I'm going to bring up the data on the throttle. Trustmaster's product doc says that the data on the throttle comes from 14 bits and 16383 data. However, if you use the library to retrieve…
0
votes
0 answers

How to compile file that uses hidapi with g++

I have a single main.cpp file with this content: #include #include #define MAX_STR 255 using namespace std; int main (void) { int res; unsigned char buf[65]; wchar_t wstr[MAX_STR]; hid_device *handle; …
user171780
  • 2,243
  • 1
  • 20
  • 43
0
votes
0 answers

Installing&&Importing pyhidapi and hidapi issues

Here is the error pycharm is throwing: ImportError: Unable to load any of the following libraries:libhidapi-hidraw.so libhidapi-hidraw.so.0 libhidapi-libusb.so libhidapi-libusb.so.0 libhidapi-iohidmanager.so libhidapi-iohidmanager.so.0…
0
votes
1 answer

Python and Hidapi: argument error when trying to write a usb device

I'm using python 3.9.6 to manage a hid relay board enter image description here I can read vendor id 1305 and product id 8216 of my board using enumerate command, then I tried some commands: >>> device = hid.Device(1305,8216) >>>…
mtre
  • 21
  • 4
0
votes
1 answer

Writing data to a USB scanner with Python

I'm leveraging the Python HID API to attempt to write data to a scanner. The specific command I am trying to write is "A1 04 00"(where A1 Is the ID 04 is the command and 00 is the data)and currently I am writing to the scanner following the specific…
basic197
  • 33
  • 3
0
votes
1 answer

HidD_SetOutputReport not resolved

I'm trying to interface with a device I found that uses the hid protocol. I'm using signal11's hidapi, and c++, however I'm writing to the device using hidsdi.h, specifically HidD_SetOutputReport, however, this results in "LNK2019: unresolved…
0
votes
1 answer

Keystroke events using hidapi

I need to receive keystroke events from HID joystick. I opened the device using the hid_open() from hidapi, but I cannot get events when I press a key. Could you give me any idea?
Yusup
  • 9
  • 1
0
votes
1 answer

How to differentiate between devices in the windows HID API?

I need to detect when a gamepad is plugged in for my game (I'm not using a higher-level input API for reasons), how can I do this? I've already enumerated all HID devices and opened files on them (except keyboard, mouse ofc) and can get all the info…
Red
  • 435
  • 1
  • 5
  • 13
0
votes
1 answer

STM32 USB Custom HID only 1 byte per transaction

I know that maximum speed of USB HID device is 64 kbps, but on oscilloscope I get transactions every 1 ms, which contain only ONE byte. My HID report descriptor listed below. What i must change to achieve 64Kbps? Currently my bInterval = 0x01 (1 ms…
0
votes
2 answers

the characters in a c++ string are being ignored

I'm trying to write to a hid device using signal11's hidapi (here). In my troubleshooting, I've noticed that part of a string isn't being displayed to the console. Here is a sample of my code //device is a hid device and is assigned to in another…
0
votes
1 answer

How to detach the system USB driver for HID?

I need to communicate with a home-made controller over USB. I'd like to use Python. The controller 'speaks' HID, and is working (detected by Linux when plugged in). The problem is that linux helpfully attaches the hid driver when the controller is…
jcoppens
  • 5,306
  • 6
  • 27
  • 47