HIDAPI HID 设备的 C 语言通用库开源项目

我要开发同款
匿名用户2018年01月15日
37阅读
开发技术C/C++Object-c
所属分类硬件驱动/工具、其他开源
授权协议GPL

作品详情

HIDAPI是一个USB和蓝牙的hid类设备在Windows、Linux、FreeBSD和Mac的C语言通用库,使用HIDAPI可以在Windows、Linux、FreeBSD和Mac平台进行USB和蓝牙的hid类设备通信,非常简单实用。

示例代码:

#include <stdio.h>#include <stdlib.h>#include "hidapi.h"int main(int argc, char* argv[]){int res;unsigned char buf[65];#define MAX_STR 255wchar_t wstr[MAX_STR];hid_device *handle;int i;// Enumerate and print the HID devices on the systemstruct 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 Stringres = hid_get_manufacturer_string(handle, wstr, MAX_STR);printf("Manufacturer String: %ls\n", wstr);// Read the Product Stringres = hid_get_product_string(handle, wstr, MAX_STR);printf("Product String: %ls\n", wstr);// Read the Serial Number Stringres = hid_get_serial_number_string(handle, wstr, MAX_STR);printf("Serial Number String: %ls", wstr);printf("\n");// Send a Feature Report to the devicebuf[0] = 0x2; // First byte is report numberbuf[1] = 0xa0;buf[2] = 0x0a;res = hid_send_feature_report(handle, buf, 17);// Read a Feature Report from the devicebuf[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 numberbuf[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 stateres = 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;}

HIDAPI有四个后端:

Windows(usinghid.dll)

Linux/hidraw(usingtheKernel'shidrawdriver)

Linux/libusb(usinglibusb-1.0)

FreeBSD(usinglibusb-1.0)

Mac(usingIOHidManager)

查看全文
声明:本文仅代表作者观点,不代表本站立场。如果侵犯到您的合法权益,请联系我们删除侵权资源!如果遇到资源链接失效,请您通过评论或工单的方式通知管理员。未经允许,不得转载,本站所有资源文章禁止商业使用运营!
下载安装【程序员客栈】APP
实时对接需求、及时收发消息、丰富的开放项目需求、随时随地查看项目状态

评论