You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

98 lines
2.2 KiB

  1. /* HID Listen, http://www.pjrc.com/teensy/hid_listen.html
  2. * Listens (and prints) all communication received from a USB HID device,
  3. * which is useful for view debug messages from the Teensy USB Board.
  4. * Copyright 2008, PJRC.COM, LLC
  5. *
  6. * You may redistribute this program and/or modify it under the terms
  7. * of the GNU General Public License as published by the Free Software
  8. * Foundation, either version 3 of the License, or (at your option)
  9. * any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see http://www.gnu.org/licenses/
  18. */
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include "rawhid.h"
  23. static void delay_ms(unsigned int msec);
  24. int main(void)
  25. {
  26. char buf[64], *in, *out;
  27. rawhid_t *hid;
  28. int num, count;
  29. int vid = 0x2a7a;//0x13ba;
  30. int pid = 0x0d03;//0x0018;
  31. //printf("Waiting for device:");
  32. fflush(stdout);
  33. while (1) {
  34. hid = rawhid_open_only1(vid, pid, 0, 0);
  35. if (hid == NULL) {
  36. printf(".");
  37. fflush(stdout);
  38. delay_ms(1000);
  39. continue;
  40. }
  41. //printf("\nListening:\n");
  42. while (1) {
  43. num = rawhid_read(hid, buf, sizeof(buf), 200);
  44. if (num < 0) break;
  45. if (num == 0) continue;
  46. in = out = buf;
  47. for (count=0; count<num; count++) {
  48. if (*in) {
  49. *out++ = *in;
  50. }
  51. in++;
  52. }
  53. count = out - buf;
  54. //printf("read %d bytes, %d actual\n", num, count);
  55. printf("%d", count);
  56. if (count) {
  57. for (int i = 0; i < count; i++) {
  58. printf("%c", buf[i]);
  59. }
  60. //num = fwrite(buf, 1, count, stdout);
  61. //fflush(stdout);
  62. }
  63. fflush(stdout);
  64. }
  65. rawhid_close(hid);
  66. //printf("\nDevice disconnected.\nWaiting for new device:");
  67. }
  68. return 0;
  69. }
  70. #if (defined(WIN32) || defined(WINDOWS) || defined(__WINDOWS__))
  71. #include <windows.h>
  72. static void delay_ms(unsigned int msec)
  73. {
  74. Sleep(msec);
  75. }
  76. #else
  77. #include <unistd.h>
  78. static void delay_ms(unsigned int msec)
  79. {
  80. usleep(msec * 1000);
  81. }
  82. #endif