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

HIDAPI - using in a c# project

A high level question about HIDAPI, if anyone has any experience with it - http://www.signal11.us/oss/hidapi/ - The visual studio solution is written in C++, and I have a C# project that I want to use the library on. Is there any way to use the C++…
jacksonSD
  • 677
  • 2
  • 13
  • 27
0
votes
1 answer

how to tare a cheap load cell scale thru Python HIDAPI?

The good news is this cheap Xiamen ELANE.NET load cell powers-up on USB into Report 3 mode; barfing its current weight in grams, constantly. Here's its…
Phlip
  • 5,253
  • 5
  • 32
  • 48
0
votes
0 answers

Java OS X app not running correctly from finder

I am building a Java app using Netbeans that is targeted for both Windows and OS X. It uses HIDAPI to communicate with a HID device. When we package for OS X if we use the terminal to launch the app: open app.app It opens and works…
Sam Foot
  • 126
  • 4
0
votes
0 answers

How to call a function in .c file from .m or .swift files in ios?

here my question was, i have a .swift file and i have .c ('C' file) with some functions. I want to call those functions from .swift file how can i do that? Example: In my product.c file i have this function static unsigned short…
iosLearner
  • 1,312
  • 1
  • 16
  • 30
0
votes
1 answer

Issue with building HIDAPI on mac

I'm trying to use the HIDAPI lib to aid in building some hid drivers for mac. Following the readme I'm told to go into the mac directory and run the make command. This unfortunately does not do much and I don't see in the readme any of the commands…
Stavros_S
  • 2,145
  • 7
  • 31
  • 75
0
votes
2 answers

Windows error messages

I am developing a program using the HIDAPI for USB communications and it appears that the API function hid_error() returns the last error message direct from windows, not a message generated by the API itself. Where can I find a list of all possible…
Michel Feinstein
  • 13,416
  • 16
  • 91
  • 173
0
votes
1 answer

HID API unable to build test gui

After following the whole readme procedure for the hid api, when trying to run 'make' from the 'hidtest/' directory I get the "make: Nothing to be done for `all'." error. I have no idea what is causing this.
-2
votes
1 answer

cython hidapi write error

I am trying to write to a USB device attached to raspberry pi 3 installed with raspbian. Error: File "", line 1 h.write([81 80 73 71 83 183 169 13]) ^ SyntaxError: invalid syntax code #!/usr/bin/python import hid h =…
infinite_loop
  • 131
  • 1
  • 10
1 2 3 4 5 6
7