hidtest.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*******************************************************
  2. Windows HID simplification
  3. Alan Ott
  4. Signal 11 Software
  5. 8/22/2009
  6. Copyright 2009, All Rights Reserved.
  7. This contents of this file may be used by anyone
  8. for any reason without any conditions and may be
  9. used as a starting point for your own applications
  10. which use HIDAPI.
  11. ********************************************************/
  12. #include <stdio.h>
  13. #include <wchar.h>
  14. #include <string.h>
  15. #include <stdlib.h>
  16. #include "hidapi.h"
  17. // Headers needed for sleeping.
  18. #ifdef _WIN32
  19. #include <windows.h>
  20. #else
  21. #include <unistd.h>
  22. #endif
  23. int main(int argc, char* argv[])
  24. {
  25. int res;
  26. unsigned char buf[256];
  27. #define MAX_STR 255
  28. wchar_t wstr[MAX_STR];
  29. hid_device *handle;
  30. int i;
  31. #ifdef WIN32
  32. UNREFERENCED_PARAMETER(argc);
  33. UNREFERENCED_PARAMETER(argv);
  34. #endif
  35. struct hid_device_info *devs, *cur_dev;
  36. if (hid_init())
  37. return -1;
  38. devs = hid_enumerate(0x0, 0x0);
  39. cur_dev = devs;
  40. while (cur_dev) {
  41. 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);
  42. printf("\n");
  43. printf(" Manufacturer: %ls\n", cur_dev->manufacturer_string);
  44. printf(" Product: %ls\n", cur_dev->product_string);
  45. printf(" Release: %hx\n", cur_dev->release_number);
  46. printf(" Interface: %d\n", cur_dev->interface_number);
  47. printf("\n");
  48. cur_dev = cur_dev->next;
  49. }
  50. hid_free_enumeration(devs);
  51. // Set up the command buffer.
  52. memset(buf,0x00,sizeof(buf));
  53. buf[0] = 0x01;
  54. buf[1] = 0x81;
  55. // Open the device using the VID, PID,
  56. // and optionally the Serial number.
  57. ////handle = hid_open(0x4d8, 0x3f, L"12345");
  58. handle = hid_open(0x4d8, 0x3f, NULL);
  59. if (!handle) {
  60. printf("unable to open device\n");
  61. return 1;
  62. }
  63. // Read the Manufacturer String
  64. wstr[0] = 0x0000;
  65. res = hid_get_manufacturer_string(handle, wstr, MAX_STR);
  66. if (res < 0)
  67. printf("Unable to read manufacturer string\n");
  68. printf("Manufacturer String: %ls\n", wstr);
  69. // Read the Product String
  70. wstr[0] = 0x0000;
  71. res = hid_get_product_string(handle, wstr, MAX_STR);
  72. if (res < 0)
  73. printf("Unable to read product string\n");
  74. printf("Product String: %ls\n", wstr);
  75. // Read the Serial Number String
  76. wstr[0] = 0x0000;
  77. res = hid_get_serial_number_string(handle, wstr, MAX_STR);
  78. if (res < 0)
  79. printf("Unable to read serial number string\n");
  80. printf("Serial Number String: (%d) %ls", wstr[0], wstr);
  81. printf("\n");
  82. // Read Indexed String 1
  83. wstr[0] = 0x0000;
  84. res = hid_get_indexed_string(handle, 1, wstr, MAX_STR);
  85. if (res < 0)
  86. printf("Unable to read indexed string 1\n");
  87. printf("Indexed String 1: %ls\n", wstr);
  88. // Set the hid_read() function to be non-blocking.
  89. hid_set_nonblocking(handle, 1);
  90. // Try to read from the device. There shoud be no
  91. // data here, but execution should not block.
  92. res = hid_read(handle, buf, 17);
  93. // Send a Feature Report to the device
  94. buf[0] = 0x2;
  95. buf[1] = 0xa0;
  96. buf[2] = 0x0a;
  97. buf[3] = 0x00;
  98. buf[4] = 0x00;
  99. res = hid_send_feature_report(handle, buf, 17);
  100. if (res < 0) {
  101. printf("Unable to send a feature report.\n");
  102. }
  103. memset(buf,0,sizeof(buf));
  104. // Read a Feature Report from the device
  105. buf[0] = 0x2;
  106. res = hid_get_feature_report(handle, buf, sizeof(buf));
  107. if (res < 0) {
  108. printf("Unable to get a feature report.\n");
  109. printf("%ls", hid_error(handle));
  110. }
  111. else {
  112. // Print out the returned buffer.
  113. printf("Feature Report\n ");
  114. for (i = 0; i < res; i++)
  115. printf("%02hhx ", buf[i]);
  116. printf("\n");
  117. }
  118. memset(buf,0,sizeof(buf));
  119. // Toggle LED (cmd 0x80). The first byte is the report number (0x1).
  120. buf[0] = 0x1;
  121. buf[1] = 0x80;
  122. res = hid_write(handle, buf, 17);
  123. if (res < 0) {
  124. printf("Unable to write()\n");
  125. printf("Error: %ls\n", hid_error(handle));
  126. }
  127. // Request state (cmd 0x81). The first byte is the report number (0x1).
  128. buf[0] = 0x1;
  129. buf[1] = 0x81;
  130. hid_write(handle, buf, 17);
  131. if (res < 0)
  132. printf("Unable to write() (2)\n");
  133. // Read requested state. hid_read() has been set to be
  134. // non-blocking by the call to hid_set_nonblocking() above.
  135. // This loop demonstrates the non-blocking nature of hid_read().
  136. res = 0;
  137. while (res == 0) {
  138. res = hid_read(handle, buf, sizeof(buf));
  139. if (res == 0)
  140. printf("waiting...\n");
  141. if (res < 0)
  142. printf("Unable to read()\n");
  143. #ifdef WIN32
  144. Sleep(500);
  145. #else
  146. usleep(500*1000);
  147. #endif
  148. }
  149. printf("Data read:\n ");
  150. // Print out the returned buffer.
  151. for (i = 0; i < res; i++)
  152. printf("%02hhx ", buf[i]);
  153. printf("\n");
  154. hid_close(handle);
  155. /* Free static HIDAPI objects. */
  156. hid_exit();
  157. #ifdef WIN32
  158. system("pause");
  159. #endif
  160. return 0;
  161. }