USB learning: hidapi library

hidapi is a library open-source operating HID devices, using C language for Windows, Linux and Mac OSX platforms. Note that this library is for HID devices, other USB devices (such as U disk) may not be suitable.

hidapi Introduction

hidapi source repository is: https: //github.com/libusb/hidapi. The main catalog as follows:

hidapi: 头文件(所有平台共用一份头文件)hidapi.h
libusb:Linux系统实现源码文件hid.c,使用libusb库实现的方式
linux:Linux系统实现源码文件hid.c,使用内核接口实现方式
windows:Windows系统实现源码文件hid.c
mac:Mac OSX 系统实现源码文件hid.c
hidtest:测试代码 hidtest.c

Note, Linux systems implemented in two ways, each swing can be selected according to the needs, it uses a kernel interface implementation.

Cross-platform

hidapi source itself according to different platform, but provides the same interface to the external header files. Therefore, direct use of the corresponding platform to realize source files. Windows, dll is recommended to use the official way, but I found a better source files directly, because you can directly trace the source code (to facilitate commissioning phase).
If our project should be cross-platform, you can use _WIN32or __linux__this macro to distinguish platform, to use different source files.

Main Interface introduction

This article does not describe in detail test example, according to the author's practical experience, according to the process list the main interface.

Initialization and exit:
. 1, hid_init: initialization, no parameter may not call, because the call is automatically determined and a subsequent interface.
2, hid_exit: quit, actually destroy structures and so on, if you do not call, will be a memory leak.

Enumeration:
1, hid_enumerate: enumerate the device, returns hid_device_infothe list. Generally used hid_enumerate(0, 0)to enumerate all devices. Enumeration is generally configured to acquire the device ID or the device path. If you know in advance that information, can not enumerate.
2, hid_free_enumeration: release list enumeration used.

Opening and closing device:
. 1, hid_open: open the specified device VID and PID, return structure pointer device, such as hid_device *handle; handle = hid_open(0x4d8, 0x3f, NULL). Read, write and close device functions, require the structure pointer.
2, hid_open_path: open the device, by a route retrieval apparatus according hid_enumerate device path. Such as under Linux /dev/hidraw0.
3, hid_close: Turn off the device.

feature report Transceiver:
1, hid_get_feature_report: get Feature report.
2, hid_send_feature_report: Send Feature report.

Write:
. 1, hid_read: reading data Input report.
2, hid_write: Write Data Output report.

Get the error:
hid_error: Returns the last error, wchar_t type string, note that the string without the caller release.

Others, such as hid_get_manufacturer_string, hid_get_product_stringetc., it seems not enough importance, it is not listed.

Windows uses

Windows system, you need to add setupapi.libthe library, you can add the following ways:

#ifdef _WIN32
#pragma comment (lib,"setupapi.lib")
#endif

Also in the project properties -> Linker -> Input, add the library dependencies.

Use MFC project

Since hidapi using C language source code need hidapi.csuffix to hidapi.cpp, and add headers in front of the file:

#include "stdafx.h"

Also you need to add setupapi.libsupport libraries.

Use Windows system Qt project

Add hidapi file to add the appropriate .pro file dependencies in the project as follows:

win32: LIBS +=  -lsetupapi

Notes development experience

Online concerning introduction hidapi, almost all based on official test code - of course, this also. But this section to talk about the development experience of the author.
1, some USB HID device in the system may correspond to a plurality of devices, in this case, PID, and VID are the same, can not be used, therefore, required to open.

2, some devices use read-write mode opening fails, this case will be opened for reading, using, see hid.c source hid_open_pathfunction. But it does not mean that the device can not write, at least I have encountered this situation. In order to verify, for the Win7 system, for a Linux system testing, debugging probably spend three nights before and after the experiment, finally get to this conclusion, without modifying source code.

3, in general, similar to the write command feature report transmission protocol itself (of course, the band data may also command itself), and write large amounts of data is read. How the truth, depending achieve the next crew. For example, update the firmware can be achieved through a feature report.

4, feature report of the first byte must be the ID value, and must be consistent with the lower machine, or can not establish a transmission, for hidapi terms, namely read and write failure. But also to ensure correct data transmission.
5, write feature reports, you must add an additional 1-byte ID and must be in the 0 position, otherwise it will fail. Acquiring real data after the ID, you can skip to a byte.

summary

This article only hidapi simple to understand and test, and it provides a friendly interface to meet the basic needs of the general application of the project.

Published 481 original articles · won praise 244 · Views 1.1 million +

Guess you like

Origin blog.csdn.net/subfate/article/details/104289106