Note that there are some explanatory texts on larger screens.

plurals
  1. POHIDAPI in two threads
    text
    copied!<p>According to <a href="https://github.com/signal11/hidapi/issues/72" rel="nofollow">https://github.com/signal11/hidapi/issues/72</a> HIDAPI ought to be thread safe on Linux machines. However, I can't get it working at all. This is what I do:</p> <pre><code>#ifdef WIN32 #include &lt;windows.h&gt; #endif #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;string.h&gt; #include &lt;pthread.h&gt; #include &lt;stdlib.h&gt; #include &lt;assert.h&gt; #include "hidapi.h" hid_device *handle; static void *TaskCode(void *argument) { int res; //hid_device *handle; unsigned char buf[64]; // res = hid_init(); // if( res == -1 ) // { // return (void*)1; // } // // handle = hid_open(0x0911, 0x251c, NULL); // if( handle == NULL ) // { // return (void*)2; // } printf( "while 2\n"); while( 1 ) { memset( buf, 64, 0 ); res = hid_read(handle, buf, 0); if( res == -1 ) { return (void*)3; } printf( "received %d bytes\n", res); for (int i = 0; i &lt; res; i++) printf("Byte %d: %02x ", i+1, buf[i]); //printf( "%02x ", buf[0]); fflush(stdout); } return (void*)0; } int main(int argc, char* argv[]) { int res; //hid_device *handle; unsigned char buf[65]; res = hid_init(); if( res == -1 ) { return 1; } handle = hid_open(0x0911, 0x251c, NULL); if( handle == NULL ) { return 2; } hid_set_nonblocking( handle, 0 ); pthread_t thread; int rc = pthread_create(&amp;thread, NULL, TaskCode, NULL); printf( "while 1\n"); while(1) { int a = getchar(); if( a == 'a') { // Get Device Type (cmd 0x82). The first byte is the report number (0x0). buf[0] = 0x0; buf[1] = 0x82; res = hid_write(handle, buf, 65); if( res != -1 ) printf( "write ok, transferred %d bytes\n", res ); else { printf( "write error\n" ); char* str = hid_error(handle); printf( "error: %s\n", str ); return 1; } } else if( a== 'b') break; } void* trc; rc = pthread_join(thread, &amp;trc); printf( "rc code: %d\n", (int)trc ); // Finalize the hidapi library res = hid_exit(); return 0; } </code></pre> <p>If I don't use the global handle, I get 'write error' every time. If I do, as in the example, formally everything works but hid_read always returns 0 bytes... Of course, if I do simple hid_write() followed by hid_read(), I'll get the correct reply to the command 0x82 as intended. I'm really lost here, am I overlooking something?</p> <p>EDIT: to clarify, zero bytes return also for everything, incl. buttons on mouse etc. So it <em>seems</em> to work but the data buffer is always zero bytes.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload