hidtest.cpp
Go to the documentation of this file.
00001 /*
00002  * hidtest.cpp
00003  *
00004  *  Created on: Oct 24, 2011
00005  *      Author: mriedel
00006  */
00007 
00008 
00009 #include <stdio.h>
00010 #include <wchar.h>
00011 #include <string.h>
00012 #include <stdlib.h>
00013 #include <hidapi.h>
00014 
00015 
00016 #include <unistd.h>
00017 
00018 
00019 int main(int argc, char* argv[])
00020 {
00021         int res;
00022         unsigned char buf[256];
00023         #define MAX_STR 255
00024         wchar_t wstr[MAX_STR];
00025         hid_device *handle;
00026         int i;
00027 
00028         struct hid_device_info *devs, *cur_dev;
00029 
00030         devs = hid_enumerate(0x0, 0x0);
00031         cur_dev = devs;
00032         while (cur_dev) {
00033                 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);
00034                 printf("\n");
00035                 printf("  Manufacturer: %ls\n", cur_dev->manufacturer_string);
00036                 printf("  Product:      %ls\n", cur_dev->product_string);
00037                 printf("  Release:      %hx\n", cur_dev->release_number);
00038                 printf("  Interface:    %d\n",  cur_dev->interface_number);
00039                 printf("\n");
00040                 cur_dev = cur_dev->next;
00041         }
00042         hid_free_enumeration(devs);
00043 
00044         // Set up the command buffer.
00045         memset(buf,0x00,sizeof(buf));
00046         buf[0] = 0x01;
00047         buf[1] = 0x81;
00048 
00049 
00050         // Open the device using the VID, PID,
00051         // and optionally the Serial number.
00053         handle = hid_open(0x054c, 0x0268, NULL);
00054         if (!handle) {
00055                 printf("unable to open device\n");
00056                 return 1;
00057         }
00058 
00059         // Read the Manufacturer String
00060         wstr[0] = 0x0000;
00061         res = hid_get_manufacturer_string(handle, wstr, MAX_STR);
00062         if (res < 0)
00063                 printf("Unable to read manufacturer string\n");
00064         printf("Manufacturer String: %ls\n", wstr);
00065 
00066         // Read the Product String
00067         wstr[0] = 0x0000;
00068         res = hid_get_product_string(handle, wstr, MAX_STR);
00069         if (res < 0)
00070                 printf("Unable to read product string\n");
00071         printf("Product String: %ls\n", wstr);
00072 
00073         // Read the Serial Number String
00074         wstr[0] = 0x0000;
00075         res = hid_get_serial_number_string(handle, wstr, MAX_STR);
00076         if (res < 0)
00077                 printf("Unable to read serial number string\n");
00078         printf("Serial Number String: (%d) %ls", wstr[0], wstr);
00079         printf("\n");
00080 
00081         // Read Indexed String 1
00082         wstr[0] = 0x0000;
00083         res = hid_get_indexed_string(handle, 1, wstr, MAX_STR);
00084         if (res < 0)
00085                 printf("Unable to read indexed string 1\n");
00086         printf("Indexed String 1: %ls\n", wstr);
00087 
00088         // Set the hid_read() function to be non-blocking.
00089         hid_set_nonblocking(handle, 1);
00090 
00091         // Try to read from the device. There shoud be no
00092         // data here, but execution should not block.
00093         res = hid_read(handle, buf, 17);
00094 
00095 //      // Send a Feature Report to the device
00096 //      buf[0] = 0x2;
00097 //      buf[1] = 0xa0;
00098 //      buf[2] = 0x0a;
00099 //      buf[3] = 0x00;
00100 //      buf[4] = 0x00;
00101 //      res = hid_send_feature_report(handle, buf, 17);
00102 //      if (res < 0) {
00103 //              printf("Unable to send a feature report.\n");
00104 //      }
00105 
00106         memset(buf,0,sizeof(buf));
00107 
00108         // Read a Feature Report from the device
00109         //buf[0] = 0x01;
00110 //      res = hid_get_feature_report(handle, buf, sizeof(buf));
00111 //      if (res < 0) {
00112 //              printf("Unable to get a feature report.\n");
00113 //              printf("%ls\n", hid_error(handle));
00114 //      }
00115 //      else {
00116 //              // Print out the returned buffer.
00117 //              printf("Feature Report\n   ");
00118 //              for (i = 0; i < res; i++)
00119 //                      printf("%02hhx ", buf[i]);
00120 //              printf("\n");
00121 //      }
00122 
00123 //      memset(buf,0,sizeof(buf));
00124 //
00125 //      // Toggle LED (cmd 0x80). The first byte is the report number (0x1).
00126 //      buf[0] = 0x1;
00127 //      buf[1] = 0x80;
00128 //      res = hid_write(handle, buf, 17);
00129 //      if (res < 0) {
00130 //              printf("Unable to write()\n");
00131 //              printf("Error: %ls\n", hid_error(handle));
00132 //      }
00133 //
00134 //
00135 //      // Request state (cmd 0x81). The first byte is the report number (0x1).
00136 //      buf[0] = 0x1;
00137 //      buf[1] = 0x81;
00138 //      hid_write(handle, buf, 17);
00139 //      if (res < 0)
00140 //              printf("Unable to write() (2)\n");
00141 
00142         // Read requested state. hid_read() has been set to be
00143         // non-blocking by the call to hid_set_nonblocking() above.
00144         // This loop demonstrates the non-blocking nature of hid_read().
00145         res = 0;
00146         while (true) {
00147                 res = hid_read(handle, buf, sizeof(buf));
00148                 if (res == 0)
00149                         printf("waiting...\n");
00150                 if (res < 0)
00151                         printf("Unable to read()\n");
00152                 if (res > 0) {
00153                         printf("Data read:\n   ");
00154                         // Print out the returned buffer.
00155                         for (i = 0; i < res; i++)
00156                                 printf("%02hhx ", buf[i]);
00157                         printf("\n");
00158                 }
00159 
00160                 usleep(50*1000);
00161         }
00162 
00163 
00164 
00165 
00166         return 0;
00167 }
00168 
00169 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Defines


telekyb_hid
Author(s): mriedel
autogenerated on Mon Nov 11 2013 11:14:51