Instrument Neutral Distributed Interface INDI  2.0.2
hidtest.cpp
Go to the documentation of this file.
1 /*******************************************************
2  Windows HID simplification
3 
4  Alan Ott
5  Signal 11 Software
6 
7  8/22/2009
8 
9  Copyright 2009
10 
11  This contents of this file may be used by anyone
12  for any reason without any conditions and may be
13  used as a starting point for your own applications
14  which use HIDAPI.
15 ********************************************************/
16 
17 #include <stdio.h>
18 #include <wchar.h>
19 #include <string.h>
20 #include <stdlib.h>
21 #include "hidapi.h"
22 
23 // Headers needed for sleeping.
24 #ifdef _WIN32
25 #include <windows.h>
26 #else
27 #include <unistd.h>
28 #endif
29 
30 int main(int argc, char* argv[])
31 {
32  (void)argc;
33  (void)argv;
34  int res;
35  unsigned char buf[256];
36 #define MAX_STR 255
37  wchar_t wstr[MAX_STR];
38  hid_device *handle;
39  int i;
40 
41 #ifdef WIN32
42  UNREFERENCED_PARAMETER(argc);
43  UNREFERENCED_PARAMETER(argv);
44 #endif
45 
46  struct hid_device_info *devs, *cur_dev;
47 
48  if (hid_init())
49  return -1;
50 
51  devs = hid_enumerate(0x0, 0x0);
52  cur_dev = devs;
53  while (cur_dev)
54  {
55  printf("Device Found\n type: %04hx %04hx\n path: %s\n serial_number: %ls", cur_dev->vendor_id, cur_dev->product_id,
56  cur_dev->path, cur_dev->serial_number);
57  printf("\n");
58  printf(" Manufacturer: %ls\n", cur_dev->manufacturer_string);
59  printf(" Product: %ls\n", cur_dev->product_string);
60  printf(" Release: %hx\n", cur_dev->release_number);
61  printf(" Interface: %d\n", cur_dev->interface_number);
62  printf("\n");
63  cur_dev = cur_dev->next;
64  }
66 
67  // Set up the command buffer.
68  memset(buf, 0x00, sizeof(buf));
69  buf[0] = 0x01;
70  buf[1] = 0x81;
71 
72 
73  // Open the device using the VID, PID,
74  // and optionally the Serial number.
76  handle = hid_open(0x4d8, 0x3f, nullptr);
77  if (!handle)
78  {
79  printf("unable to open device\n");
80  return 1;
81  }
82 
83  // Read the Manufacturer String
84  wstr[0] = 0x0000;
85  res = hid_get_manufacturer_string(handle, wstr, MAX_STR);
86  if (res < 0)
87  printf("Unable to read manufacturer string\n");
88  printf("Manufacturer String: %ls\n", wstr);
89 
90  // Read the Product String
91  wstr[0] = 0x0000;
92  res = hid_get_product_string(handle, wstr, MAX_STR);
93  if (res < 0)
94  printf("Unable to read product string\n");
95  printf("Product String: %ls\n", wstr);
96 
97  // Read the Serial Number String
98  wstr[0] = 0x0000;
99  res = hid_get_serial_number_string(handle, wstr, MAX_STR);
100  if (res < 0)
101  printf("Unable to read serial number string\n");
102  printf("Serial Number String: (%d) %ls", wstr[0], wstr);
103  printf("\n");
104 
105  // Read Indexed String 1
106  wstr[0] = 0x0000;
107  res = hid_get_indexed_string(handle, 1, wstr, MAX_STR);
108  if (res < 0)
109  printf("Unable to read indexed string 1\n");
110  printf("Indexed String 1: %ls\n", wstr);
111 
112  // Set the hid_read() function to be non-blocking.
113  hid_set_nonblocking(handle, 1);
114 
115  // Try to read from the device. There shoud be no
116  // data here, but execution should not block.
117  res = hid_read(handle, buf, 17);
118 
119  // Send a Feature Report to the device
120  buf[0] = 0x2;
121  buf[1] = 0xa0;
122  buf[2] = 0x0a;
123  buf[3] = 0x00;
124  buf[4] = 0x00;
125  res = hid_send_feature_report(handle, buf, 17);
126  if (res < 0)
127  {
128  printf("Unable to send a feature report.\n");
129  }
130 
131  memset(buf, 0, sizeof(buf));
132 
133  // Read a Feature Report from the device
134  buf[0] = 0x2;
135  res = hid_get_feature_report(handle, buf, sizeof(buf));
136  if (res < 0)
137  {
138  printf("Unable to get a feature report.\n");
139  printf("%ls", hid_error(handle));
140  }
141  else
142  {
143  // Print out the returned buffer.
144  printf("Feature Report\n ");
145  for (i = 0; i < res; i++)
146  printf("%02hhx ", buf[i]);
147  printf("\n");
148  }
149 
150  memset(buf, 0, sizeof(buf));
151 
152  // Toggle LED (cmd 0x80). The first byte is the report number (0x1).
153  buf[0] = 0x1;
154  buf[1] = 0x80;
155  res = hid_write(handle, buf, 17);
156  if (res < 0)
157  {
158  printf("Unable to write()\n");
159  printf("Error: %ls\n", hid_error(handle));
160  }
161 
162 
163  // Request state (cmd 0x81). The first byte is the report number (0x1).
164  buf[0] = 0x1;
165  buf[1] = 0x81;
166  hid_write(handle, buf, 17);
167  if (res < 0)
168  printf("Unable to write() (2)\n");
169 
170  // Read requested state. hid_read() has been set to be
171  // non-blocking by the call to hid_set_nonblocking() above.
172  // This loop demonstrates the non-blocking nature of hid_read().
173  res = 0;
174  while (res == 0)
175  {
176  res = hid_read(handle, buf, sizeof(buf));
177  if (res == 0)
178  printf("waiting...\n");
179  if (res < 0)
180  printf("Unable to read()\n");
181 #ifdef WIN32
182  Sleep(500);
183 #else
184  usleep(500 * 1000);
185 #endif
186  }
187 
188  printf("Data read:\n ");
189  // Print out the returned buffer.
190  for (i = 0; i < res; i++)
191  printf("%02hhx ", buf[i]);
192  printf("\n");
193 
194  hid_close(handle);
195 
196  /* Free static HIDAPI objects. */
197  hid_exit();
198 
199 #ifdef WIN32
200  system("pause");
201 #endif
202 
203  return 0;
204 }
int HID_API_EXPORT hid_read(hid_device *dev, unsigned char *data, size_t length)
Read an Input report from a HID device.
Definition: hid_libusb.c:1095
hid_device * 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.
Definition: hid_libusb.c:612
int HID_API_EXPORT hid_init(void)
Initialize the HIDAPI library.
Definition: hid_libusb.c:388
void HID_API_EXPORT hid_free_enumeration(struct hid_device_info *devs)
Free an enumeration Linked List.
Definition: hid_libusb.c:597
int HID_API_EXPORT_CALL hid_get_indexed_string(hid_device *dev, int string_index, wchar_t *string, size_t maxlen)
Get a string from a HID device, based on its string index.
Definition: hid_libusb.c:1211
int HID_API_EXPORT hid_write(hid_device *dev, const unsigned char *data, size_t length)
Write an Output report to a HID device.
Definition: hid_libusb.c:929
int HID_API_EXPORT_CALL hid_get_serial_number_string(hid_device *dev, wchar_t *string, size_t maxlen)
Get The Serial Number String from a HID device.
Definition: hid_libusb.c:1206
int HID_API_EXPORT hid_get_feature_report(hid_device *dev, unsigned char *data, size_t length)
Get a feature report from a HID device.
Definition: hid_libusb.c:1135
int HID_API_EXPORT_CALL hid_get_product_string(hid_device *dev, wchar_t *string, size_t maxlen)
Get The Product String from a HID device.
Definition: hid_libusb.c:1201
int HID_API_EXPORT_CALL hid_get_manufacturer_string(hid_device *dev, wchar_t *string, size_t maxlen)
Get The Manufacturer String from a HID device.
Definition: hid_libusb.c:1196
void HID_API_EXPORT hid_close(hid_device *dev)
Close a HID device.
Definition: hid_libusb.c:1163
int HID_API_EXPORT hid_send_feature_report(hid_device *dev, const unsigned char *data, size_t length)
Send a Feature report to the device.
Definition: hid_libusb.c:1107
HID_API_EXPORT const wchar_t *HID_API_CALL hid_error(hid_device *dev)
Get a string describing the last error which occurred.
Definition: hid_libusb.c:1227
int HID_API_EXPORT hid_set_nonblocking(hid_device *dev, int nonblock)
Set the device handle to be non-blocking.
Definition: hid_libusb.c:1100
int HID_API_EXPORT hid_exit(void)
Finalize the HIDAPI library.
Definition: hid_libusb.c:407
struct hid_device_info HID_API_EXPORT * hid_enumerate(unsigned short vendor_id, unsigned short product_id)
Enumerate the HID Devices.
Definition: hid_libusb.c:418
int main(int argc, char *argv[])
Definition: hidtest.cpp:30
#define MAX_STR
unsigned short product_id
Definition: hidapi.h:62
struct hid_device_info * next
Definition: hidapi.h:85
wchar_t * manufacturer_string
Definition: hidapi.h:69
unsigned short vendor_id
Definition: hidapi.h:60
char * path
Definition: hidapi.h:58
unsigned short release_number
Definition: hidapi.h:67
wchar_t * serial_number
Definition: hidapi.h:64
int interface_number
Definition: hidapi.h:82
wchar_t * product_string
Definition: hidapi.h:71