1
1
/*******************************************************
2
- Windows HID simplification
2
+ HIDAPI - Multi-Platform library for
3
+ communication with HID devices.
3
4
4
5
Alan Ott
5
6
Signal 11 Software
6
7
7
- 8/22/2009
8
+ libusb/hidapi Team
8
9
9
- Copyright 2009
10
+ Copyright 2022.
10
11
11
12
This contents of this file may be used by anyone
12
13
for any reason without any conditions and may be
@@ -179,8 +180,7 @@ int main(int argc, char* argv[])
179
180
buf [0 ] = 0x2 ;
180
181
res = hid_get_feature_report (handle , buf , sizeof (buf ));
181
182
if (res < 0 ) {
182
- printf ("Unable to get a feature report.\n" );
183
- printf ("%ls" , hid_error (handle ));
183
+ printf ("Unable to get a feature report: %ls\n" , hid_error (handle ));
184
184
}
185
185
else {
186
186
// Print out the returned buffer.
@@ -197,40 +197,53 @@ int main(int argc, char* argv[])
197
197
buf [1 ] = 0x80 ;
198
198
res = hid_write (handle , buf , 17 );
199
199
if (res < 0 ) {
200
- printf ("Unable to write()\n" );
201
- printf ("Error: %ls\n" , hid_error (handle ));
200
+ printf ("Unable to write(): %ls\n" , hid_error (handle ));
202
201
}
203
202
204
203
205
204
// Request state (cmd 0x81). The first byte is the report number (0x1).
206
205
buf [0 ] = 0x1 ;
207
206
buf [1 ] = 0x81 ;
208
207
hid_write (handle , buf , 17 );
209
- if (res < 0 )
210
- printf ("Unable to write() (2)\n" );
208
+ if (res < 0 ) {
209
+ printf ("Unable to write()/2: %ls\n" , hid_error (handle ));
210
+ }
211
211
212
212
// Read requested state. hid_read() has been set to be
213
213
// non-blocking by the call to hid_set_nonblocking() above.
214
214
// This loop demonstrates the non-blocking nature of hid_read().
215
215
res = 0 ;
216
+ i = 0 ;
216
217
while (res == 0 ) {
217
218
res = hid_read (handle , buf , sizeof (buf ));
218
- if (res == 0 )
219
+ if (res == 0 ) {
219
220
printf ("waiting...\n" );
220
- if (res < 0 )
221
- printf ("Unable to read()\n" );
222
- #ifdef _WIN32
221
+ }
222
+ if (res < 0 ) {
223
+ printf ("Unable to read(): %ls\n" , hid_error (handle ));
224
+ break ;
225
+ }
226
+
227
+ i ++ ;
228
+ if (i >= 10 ) { /* 10 tries by 500 ms - 5 seconds of waiting*/
229
+ printf ("read() timeout\n" );
230
+ break ;
231
+ }
232
+
233
+ #ifdef _WIN32
223
234
Sleep (500 );
224
- #else
235
+ #else
225
236
usleep (500 * 1000 );
226
- #endif
237
+ #endif
227
238
}
228
239
229
- printf ("Data read:\n " );
230
- // Print out the returned buffer.
231
- for (i = 0 ; i < res ; i ++ )
232
- printf ("%02x " , (unsigned int ) buf [i ]);
233
- printf ("\n" );
240
+ if (res > 0 ) {
241
+ printf ("Data read:\n " );
242
+ // Print out the returned buffer.
243
+ for (i = 0 ; i < res ; i ++ )
244
+ printf ("%02x " , (unsigned int ) buf [i ]);
245
+ printf ("\n" );
246
+ }
234
247
235
248
hid_close (handle );
236
249
0 commit comments