hidtest.cpp File Reference
`#include <stdio.h>`
`#include <wchar.h>`
`#include <string.h>`
`#include <stdlib.h>`
`#include "hidapi.h"`
`#include <windows.h>`
Include dependency graph for hidtest.cpp:

Go to the source code of this file.

## Macros

#define | MAX_STR 255

## Functions

int | main (int argc, char *argv[])

## Macro Definition Documentation

## MAX_STR

#define MAX_STR 255

## Function Documentation

## main()

int main | ( | int | argc,
|  | char * | argv[]
| ) |  |

Definition at line 30 of file hidtest.cpp.

{
int res;
unsigned char buf[256];
#define MAX_STR 255
wchar_t wstr[MAX_STR];
hid_device *handle;
int i;
#ifdef WIN32
UNREFERENCED_PARAMETER(argc);
UNREFERENCED_PARAMETER(argv);
#endif
struct hid_device_info *devs, *cur_dev;
if (hid_init())
return -1;
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(" Release: %hx\n", cur_dev->release_number);
printf(" Interface: %d\n", cur_dev->interface_number);
printf("\n");
cur_dev = cur_dev->next;
}
hid_free_enumeration(devs);
// Set up the command buffer.
memset(buf,0x00,sizeof(buf));
buf[0] = 0x01;
buf[1] = 0x81;
// Open the device using the VID, PID,
// and optionally the Serial number.
////handle = hid_open(0x4d8, 0x3f, L"12345");
handle = hid_open(0x4d8, 0x3f, NULL);
if (!handle) {
printf("unable to open device\n");
return 1;
}
// Read the Manufacturer String
wstr[0] = 0x0000;
res = hid_get_manufacturer_string(handle, wstr, MAX_STR);
if (res < 0)
printf("Unable to read manufacturer string\n");
printf("Manufacturer String: %ls\n", wstr);
// Read the Product String
wstr[0] = 0x0000;
res = hid_get_product_string(handle, wstr, MAX_STR);
if (res < 0)
printf("Unable to read product string\n");
printf("Product String: %ls\n", wstr);
// Read the Serial Number String
wstr[0] = 0x0000;
res = hid_get_serial_number_string(handle, wstr, MAX_STR);
if (res < 0)
printf("Unable to read serial number string\n");
printf("Serial Number String: (%d) %ls", wstr[0], wstr);
printf("\n");
// Read Indexed String 1
wstr[0] = 0x0000;
res = hid_get_indexed_string(handle, 1, wstr, MAX_STR);
if (res < 0)
printf("Unable to read indexed string 1\n");
printf("Indexed String 1: %ls\n", wstr);
// Set the hid_read() function to be non-blocking.
hid_set_nonblocking(handle, 1);
// Try to read from the device. There shoud be no
// data here, but execution should not block.
res = hid_read(handle, buf, 17);
// Send a Feature Report to the device
buf[0] = 0x2;
buf[1] = 0xa0;
buf[2] = 0x0a;
buf[3] = 0x00;
buf[4] = 0x00;
res = hid_send_feature_report(handle, buf, 17);
if (res < 0) {
printf("Unable to send a feature report.\n");
}
memset(buf,0,sizeof(buf));
// Read a Feature Report from the device
buf[0] = 0x2;
res = hid_get_feature_report(handle, buf, sizeof(buf));
if (res < 0) {
printf("Unable to get a feature report.\n");
printf("%ls", hid_error(handle));
}
else {
// Print out the returned buffer.
printf("Feature Report\n ");
for (i = 0; i < res; i++)
printf("%02hhx ", buf[i]);
printf("\n");
}
memset(buf,0,sizeof(buf));
// Toggle LED (cmd 0x80). The first byte is the report number (0x1).
buf[0] = 0x1;
buf[1] = 0x80;
res = hid_write(handle, buf, 17);
if (res < 0) {
printf("Unable to write()\n");
printf("Error: %ls\n", hid_error(handle));
}
// Request state (cmd 0x81). The first byte is the report number (0x1).
buf[0] = 0x1;
buf[1] = 0x81;
hid_write(handle, buf, 17);
if (res < 0)
printf("Unable to write() (2)\n");
// Read requested state. hid_read() has been set to be
// non-blocking by the call to hid_set_nonblocking() above.
// This loop demonstrates the non-blocking nature of hid_read().
res = 0;
while (res == 0) {
res = hid_read(handle, buf, sizeof(buf));
if (res == 0)
printf("waiting...\n");
if (res < 0)
printf("Unable to read()\n");
#ifdef WIN32
Sleep(500);
#else
usleep(500*1000);
#endif
}
printf("Data read:\n ");
// Print out the returned buffer.
for (i = 0; i < res; i++)
printf("%02hhx ", buf[i]);
printf("\n");
hid_close(handle);
/* Free static HIDAPI objects. */
hid_exit();
#ifdef WIN32
system("pause");
#endif
return 0;
}

References hid_close(), hid_enumerate(), hid_error(), hid_exit(), hid_free_enumeration(), hid_get_feature_report(), hid_get_indexed_string(), hid_get_manufacturer_string(), hid_get_product_string(), hid_get_serial_number_string(), hid_init(), hid_open(), hid_read(), hid_send_feature_report(), hid_set_nonblocking(), hid_write(), i, hid_device_info::interface_number, hid_device_info::manufacturer_string, MAX_STR, memset, hid_device_info::next, NULL, hid_device_info::path, hid_device_info::product_id, hid_device_info::product_string, hid_device_info::release_number, hid_device_info::serial_number, and hid_device_info::vendor_id.

hid_error
const HID_API_EXPORT wchar_t *HID_API_CALL hid_error(hid_device *device)
Get a string describing the last error which occurred.
hid_device_info::interface_number
int interface_number
Definition: hidapi.h:79
hid_write
int HID_API_EXPORT HID_API_CALL hid_write(hid_device *device, const unsigned char *data, size_t length)
Write an Output report to a HID device.
hid_send_feature_report
int HID_API_EXPORT HID_API_CALL hid_send_feature_report(hid_device *device, const unsigned char *data, size_t length)
Send a Feature report to the device.
NULL
#define NULL
Definition: begin_code.h:167
handle
EGLImageKHR EGLint EGLint * handle
Definition: eglext.h:937
hid_exit
int HID_API_EXPORT HID_API_CALL hid_exit(void)
Finalize the HIDAPI library.
hid_close
void HID_API_EXPORT HID_API_CALL hid_close(hid_device *device)
Close a HID device.
hid_free_enumeration
void HID_API_EXPORT HID_API_CALL hid_free_enumeration(struct hid_device_info *devs)
Free an enumeration Linked List.
x0
GLuint GLfloat x0
Definition: SDL_opengl_glext.h:8583
hid_init
int HID_API_EXPORT HID_API_CALL hid_init(void)
Initialize the HIDAPI library.
hid_get_indexed_string
int HID_API_EXPORT_CALL hid_get_indexed_string(hid_device *device, int string_index, wchar_t *string, size_t maxlen)
Get a string from a HID device, based on its string index.
hid_device_info::manufacturer_string
wchar_t * manufacturer_string
Definition: hidapi.h:66
hid_open
HID_API_EXPORT hid_device *HID_API_CALL hid_open(unsigned short vendor_id, unsigned short product_id, const wchar_t *serial_number)
Open a HID device using a Vendor ID (VID), Product ID (PID) and optionally a serial number.
hid_device_info::next
struct hid_device_info * next
Definition: hidapi.h:82
hid_device_info
Definition: hidapi.h:53
buf
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: SDL_opengl_glext.h:2480
hid_enumerate
struct hid_device_info HID_API_EXPORT *HID_API_CALL hid_enumerate(unsigned short vendor_id, unsigned short product_id)
Enumerate the HID Devices.
hid_device_info::product_id
unsigned short product_id
Definition: hidapi.h:59
hid_device_info::serial_number
wchar_t * serial_number
Definition: hidapi.h:61
MAX_STR
#define MAX_STR
hid_device_info::release_number
unsigned short release_number
Definition: hidapi.h:64
hid_get_serial_number_string
int HID_API_EXPORT_CALL hid_get_serial_number_string(hid_device *device, wchar_t *string, size_t maxlen)
Get The Serial Number String from a HID device.
memset
#define memset
Definition: SDL_malloc.c:627
hid_get_feature_report
int HID_API_EXPORT HID_API_CALL hid_get_feature_report(hid_device *device, unsigned char *data, size_t length)
Get a feature report from a HID device.
hid_set_nonblocking
int HID_API_EXPORT HID_API_CALL hid_set_nonblocking(hid_device *device, int nonblock)
Set the device handle to be non-blocking.
hid_read
int HID_API_EXPORT HID_API_CALL hid_read(hid_device *device, unsigned char *data, size_t length)
Read an Input report from a HID device.
hid_get_product_string
int HID_API_EXPORT_CALL hid_get_product_string(hid_device *device, wchar_t *string, size_t maxlen)
Get The Product String from a HID device.
hid_device
struct hid_device_ hid_device
Definition: hidapi.h:50
res
GLuint res
Definition: SDL_opengl_glext.h:7937
hid_device_info::path
char * path
Definition: hidapi.h:55
hid_get_manufacturer_string
int HID_API_EXPORT_CALL hid_get_manufacturer_string(hid_device *device, wchar_t *string, size_t maxlen)
Get The Manufacturer String from a HID device.
i
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50
hid_device_info::vendor_id
unsigned short vendor_id
Definition: hidapi.h:57
hid_device_info::product_string
wchar_t * product_string
Definition: hidapi.h:68