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

How to include hidapi in visual studio 2015 c++

I'm trying to use the hidapi library on Visual Studio 2015 for a c++ console app. I've included hidapi.h as a header file, but can't seem to figure out how to link the dll. I've scoured this site looking for solutions to including other libraries…
C Banana
  • 45
  • 9
1
vote
1 answer

HIDAPI hid_open_path() how to determine which path to use

HIDAPI+Qt5.4+ubuntu14.04LTS + Barcode scanning gun I use the HIDAPI from here https://github.com/signal11/hidapi This is the HIDAPI…
DayByDay
  • 19
  • 6
1
vote
1 answer

getting 'ValueError: not open' when trying to open a connection with hidapi/hid on OSX

I am using python to open a hid/hidapi (the error is the same either way) connection on OSX El Capitan. However, It gives me the following error stacktrace: Traceback (most recent call last): File…
Oscar Courchaine
  • 346
  • 3
  • 14
1
vote
1 answer

Python PyUSB HID Feature Report

I am accessing a USB HID Device using python hidapi from a Mac OSX 10.10.5 doing: import hid import time hidraw = hid.device(0x1a67, 0x0004) hidraw.open(0x1a67, 0x0004) # Rpt, GnS, Tgt, Size, Index LSB, Index MSB, Data #…
faturita
  • 115
  • 4
  • 9
1
vote
1 answer

hid4java HidServices.getHidDevice( ... ) returns NULL

I've built the hid4java Maven project and successfully managed to put the library into a class library. This is the code I'm using trying to open the device with which I am working : public class JR3Controller implements HidServicesListener { …
Will
  • 3,413
  • 7
  • 50
  • 107
1
vote
2 answers

hidapi Windows 8.1 hid_write fail

hw : lpc1549 eval board with usb hid test firmware ... endpoint size 64 bytes endpoint intr reads out-report buf and displays len and data in hex ... copies out_report into in_report buf and echoes back with write funct and len host : using qt5 ,…
1
vote
1 answer

cabal install fails because missing C library

I want to install hemokit driver by the command cabal install hemokit command, but it's tells me that it fails when it's going to install the dependency 'hidapi'. And this is the message, which gives to me `Configuring hidapi-0.1.3... cabal:…
hbak
  • 1,333
  • 3
  • 10
  • 22
1
vote
1 answer

Compiling hidapi with libusb instead of hidraw

I am trying to compile hidapi on ubuntu 14.04 for use with node-hid. Node-hid cannot see any devices for some reason when hidapi uses hidraw as this issue describes. The fourth post also offers a solution: So, with my limited gcc/waf knowledge I…
Jabbath
  • 334
  • 1
  • 3
  • 11
1
vote
0 answers

Using HIDAPI to receive different sized data packages

I am using the HIDAPI with Windows 8.1 to read out an audio stream from a HID device. This stream sends ADPCM data consisting of 259 bytes, where the first 12 packages are 20 bytes large, and the 13th is 19 bytes. The problem is that HIDAPI always…
chwi
  • 2,752
  • 2
  • 41
  • 66
1
vote
1 answer

Confused by the report ID when using HIDAPI through USB

I am a USB HID newbie and I am trying to use the HIDAPI for my application. I have a question about using HIDAPI (in Visual Studio) regarding the report ID. When I try to use the HIDAPI and connect to the Microchip Custom Demo, I am confused about…
Wang Leader
  • 13
  • 1
  • 5
1
vote
0 answers

How to read temperature on usb device with HIDAPI?

I have to recover the value of temperature from a usb device TEMPer and I found the code on this blogpost: import java.io.IOException; import org.apache.log4j.Logger; import com.codeminders.hidapi.HIDDevice; import…
Maaz
  • 2,405
  • 1
  • 15
  • 21
1
vote
2 answers

Options for HIDAPI on OS X

I have been working with HIDAPI with C and trying to get Mono to communicate with the HIDAPI using interop. I have done a lot of searching and have not been able to find anyone who has gotten HIDAPI to work with Mono on OS X. Does anyone know if I…
Joe Pitz
  • 2,434
  • 3
  • 25
  • 30
1
vote
1 answer

how to create libhidapi.dylib from source?

I have ongoing cross platform project (Win and Mac) where I need to access USB device. I have found HIDAPI library that perfectly fits all my requirements. Since there is no Delphi Firemonkey wrapper for HIDAPI library (at least I don’t know for…
A.Z.
  • 69
  • 3
  • 10
1
vote
1 answer

HIDAPI in two threads

According to https://github.com/signal11/hidapi/issues/72 HIDAPI ought to be thread safe on Linux machines. However, I can't get it working at all. This is what I do: #ifdef WIN32 #include #endif #include #include…
Miro Kropacek
  • 2,742
  • 4
  • 26
  • 41
0
votes
0 answers

hidapi vs WIndows Driver API (hidsdi.h)

I am trying to write an Winodws application that reads and writes a HID device. One way for me to do so is to use the hidapi. I also came across some Winddows Driver API on HID. It seems hidapi is quite straightforward to use in applications, but,…