Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9007293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:38:57+00:00 2026-06-16T01:38:57+00:00

According to https://github.com/signal11/hidapi/issues/72 HIDAPI ought to be thread safe on Linux machines. However, I

  • 0

According to https://github.com/signal11/hidapi/issues/72 HIDAPI ought to be thread safe on Linux machines. However, I can’t get it working at all. This is what I do:

#ifdef WIN32
#include <windows.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <stdlib.h>
#include <assert.h>

#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 < 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(&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, &trc);

    printf( "rc code: %d\n", (int)trc );

    // Finalize the hidapi library
    res = hid_exit();

    return 0;
}

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?

EDIT: to clarify, zero bytes return also for everything, incl. buttons on mouse etc. So it seems to work but the data buffer is always zero bytes.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T01:38:58+00:00Added an answer on June 16, 2026 at 1:38 am

    Shame on me, a dumb mistake. The code should be:

    memset( buf, 0, 64 );
    res = hid_read(handle, buf, 64);
    

    and then it works. Should sleep more and write less!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

According to https://github.com/technomancy/leiningen/blob/master/sample.project.clj I can attach a :init inside of a :repl-options. However, when
According to https://github.com/mperham/dalli , we can configure multiple Memcache servers. But I'm not sure
According to https://github.com/clojure/core.logic/wiki/Differences-from-The-Reasoned-Schemer core.logic supports listo. However, the following piece of code does not
According to the documentation here: https://github.com/playframework/Play20/wiki/AssetsGoogleClosureCompiler it says: Any JavaScript file present in app/assets
According to the documentation found in http://ask.github.com/celery/userguide/routing.html#manual-routing i can pass a queue parameter to
I'm going according to the documentation at https://github.com/felixge/node-airbrake But when I try it, I
I've been trying to build live555 according to this guide: https://github.com/boltonli/ohbee/tree/master/android/streamer/jni as well as
I've installed social_stream according to the README ( https://github.com/ging/social_stream ) and it works fine,
Following the instructions here: https://github.com/ry/node/wiki/Building-node.js-on-Cygwin-(Windows ) I've tried installing on two machines, either of
(A minimal non-compiling example can be found at https://gist.github.com/4044467 , see more background below.)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.