"Qt" uses LIBUSB and HIDAPI to implement HID communications

Source: Internet
Author: User
Tags emit usleep
Using LIBUSB can achieve universal USB communication, but the implementation is more complex, you can use HIDAPI to encapsulate a layer, through HIDAPI call Libusb.
The specific process is as follows:

1, compile Libusb
Download Address: https://sourceforge.net/projects/libusb/files/libusb-1.0/
When compiling, to add--disable-udev parameters, we do not use this Lib
For this machine use:
./configure--prefix=/opt/libusb1.0--disable-udev make make
install



If you cross-compile to arm Development Board
./configure--build=i686-linux--host=arm-linux--prefix= pwd/install  CC=ARM-LINUX-GCC CXX=arm-linux-g++- Disable-udev make make
install


2. Copy Lib to system Lib environment
cp/opt/libusb1.0/lib/libusb-1.0.so*/usr/lib


3. Use Hidapi
Download address: https://github.com/signal11/hidapi/downloads, download 0.7 version
After decompression will find that there are many folders, such as Hidapi, Hidtest, Linux, Windows, where Hidapi is a shared part of Linux under the makefile, you can compile Linux can be used under the HID read and write Hidtest program, The read-write object is vid-0x04d8,pid for 0x003f.
Compiling the demo requires modifying the makefile to support our compiled LIBUSB path lookup.
The amendment reads as follows:
#LIBS      = ' pkg-config libusb-1.0 libudev--libs '
LIBS =-      l '/usr/lib/x86_64-linux-gnu '-lusb-1.0-lpthread
#INCLUDES? =-I.. /hidapi ' pkg-config libusb-1.0--cflags '
INCLUDES? =-I.. /hidapi  -i/opt/libusb1.0/include/libusb-1.0

Then make & make Install


If QT is used, 3 files are copied and added to the project. Hidapi folder under the Hidapi.h, this is mainly the structure of the package, Linux under the HID-LIBUSB.C, this is mainly some of the main call function, Hidtest hidtest.cpp, this is mainly a test demo, you can directly modify their use, of course, do not modify can also make Use demo.
Compiling is required to use LIBUSB, so add the corresponding header file directory and Lib directory to the. Pro file, adding two lines to the last line as follows:

Includepath + +/opt/libusb1.0/include/libusb-1.0
LIBS + =-L "/usr/lib/x86_64-linux-gnu"-lusb-1.0

The following is the C language
Modify the hidtest.c for mainhidinterface.c and MainHidInterface.h use Pthread to support multithreaded read and write, using callback functions to return the read data. MainHidInterface.h
#ifndef Deltahidinterface_h
#define DELTAHIDINTERFACE_H

#include <stdio.h>
#include <wchar.h>
#include <string.h >
#include <stdlib.h>
#include "hidapi.h"

//Headers needed for sleeping.
#include <unistd.h>

#define HIDDATALENGTH

//hid data receive callback function
typedef void (* Dataarrivecallbackfunc) (unsigned char *recdata,unsigned char length);

Dataarrivecallbackfunc Hiddataarrivecallback;

Hid_device *handle;

unsigned char isOpen;


int Hidapi_init (Dataarrivecallbackfunc dataarrivecallback);

int hidapi_write (unsigned char *data, unsigned char length);

When data arrived, the function would be called
void hidapi_datareceive (unsigned char *recdata,unsigned char length) ;

int hidapi_close (void);

#endif//Hidinterface_h


Mainhidinterface.c
#include "hidinterface.h" #include <pthread.h> void Hidread_thread (void);
    int Hidapi_init (Dataarrivecallbackfunc dataarrivecallback) {hiddataarrivecallback = NULL;
    Open the device using the VID, PID,//and optionally the serial number.
    Handle = Hid_open (0x4d8, 0x3f, L "12345");
    Handle = Hid_open (0x4d8, 0x3f, NULL);
        if (!handle) {printf ("Unable to open device\n");
        IsOpen = 0;
    return-1;
    printf ("Open device success\n");
    IsOpen = 1;
    Hiddataarrivecallback = Dataarrivecallback;


    Hidread_thread ();
    Set the Hid_read () function to is non-blocking.

    Hid_set_nonblocking (handle, 1);
return 0;
    int hidapi_write (unsigned char *data, unsigned char length) {int res;

    unsigned char realdata[length+1];
    Realdata[0]=length;
    int i;
    for (i=0;i<length;i++) {realdata[i+1]=data[i];
    res = Hid_write (handle, Realdata, length+1); if (Res < 0) {PrinTF ("Unable to write () \ n");
        printf ("Error:%ls\n", Hid_error (handle));
    return-1;
    printf ("Write success\n");
return 0; } void hidapi_datareceive (unsigned char *recdata,unsigned char length) {if (hiddataarrivecallback==null) Retu

    Rn
Hiddataarrivecallback (recdata,length);

    } void Hidapi_read () {unsigned char recdata[hiddatalength];
    int res;
        while (isopen==1) {res = Hid_read (handle, Recdata, hiddatalength);
        if (res = = 0);//printf ("waiting...\n");
            else if (Res < 0) {printf ("unable to read () \ n");
        return-1;

else {int i;
printf ("Data read:\n");
Print out the returned buffer.
for (i = 0; i < res; i++)//printf ("%02HHX", Recdata[i]);

            printf ("\ n");

            unsigned char length = recdata[0];
unsigned char datas[length];            for (i=0;i<length;i++) {datas[i]=recdata[i+1];
        } hidapi_datareceive (Datas,length);

    } usleep (50*1000);
   } void Hidread_thread (void) {pthread_t id;
   int ret, I; Ret=pthread_create (&id,null, (void *) hidapi_read,null);
       Successfully returned 0, error returned error number if (ret!=0) {printf ("Create pthread error!\n");
   Exit (1);

   }//pthread_join (Id,null);
printf ("Create pthread success!\n");
    int Hidapi_close (void) {hid_close (handle);
    IsOpen = 0; /* Free static HIDAPI objects.

    * * Hid_exit ();

return 0;
 }


To use QT C + +, replace pthread with Qthread for reading, and do not use callback functions, because the callback function does not support member functions (with the default pointer this), only the static function, and the use of multiple object instances.  You can use the signal and slot mechanism to transmit the data you receive. The following is an example of a QT C + + rewrite that you can use to connect to receive data.

MainHidInterface.h
#ifndef mmainhidinterface_h
#define MMAINHIDINTERFACE_H

#include <iostream>
#include <qthread >
#include <QCoreApplication>
#include "hidapi.h"

//Headers needed for sleeping.
#include <unistd.h>

using namespace std;

#define HIDDATALENGTH

//typedef Void (*datareccallbackfunc) (unsigned char *recdata,unsigned char length);

Class Mainhidinterface:public Qthread
{
    q_object public

:
    //initial and set HID data receive callback function
    mainhidinterface ();

    int hidapi_write (unsigned char *data, unsigned char length);

    int hidapi_close (void);

Signals:
    void hiddataarrived (unsigned char *data, unsigned char length);

Private:


    hid_device *handle;

    unsigned char isOpen;

Protected:
    void Run ();
#endif//Hidinterface_h



MainHidInterface.cpp
/******************************************************* Windows HID Simplification Alan Ott Signal Software 8/22
 
 /2009 Copyright 2009, all Rights Reserved. This contents of this file may is used by anyone to any reason without any conditions and could be used as a starting POI
NT for your own applications which use HIDAPI. /#include "hidinterface.h" Mainhidinterface::
    Mainhidinterface () {//Open the device using the VID, PID,//and optionally the serial number.
    Handle = Hid_open (0x4d8, 0x3f, L "12345");
    Handle = Hid_open (0x4d8, 0x3f, NULL);
        if (!handle) {printf ("Unable to open device\n");
        IsOpen = 0;
    Return
    printf ("Open device success\n");

    IsOpen = 1;
    Set the Hid_read () function to is non-blocking.

    Hid_set_nonblocking (handle, 1);

    Emit hiddataarrived (null,0);
Return int mainhidinterface::hidapi_write (unsigned char *data,unsigned char length) {int res;

    unsigned char realdata[length+1];
    Realdata[0]=length;
    int i;
    for (i=0;i<length;i++) {realdata[i+1]=data[i];
    res = Hid_write (handle, Realdata, length+1);
        if (Res < 0) {printf ("Unable to write () \ n");
        printf ("Error:%ls\n", Hid_error (handle));
    return-1;
    printf ("Write success\n");
return 0;

    } void Mainhidinterface::run () {unsigned char recdata[hiddatalength];
    int res;
        while (isopen==1) {res = Hid_read (handle, Recdata, hiddatalength);
        if (res = = 0);//printf ("waiting...\n");
            else if (Res < 0) {printf ("unable to read () \ n");
        Return

else {int i;
printf ("Data read:\n");
Print out the returned buffer.
for (i = 0; i < res; i++)//printf ("%02HHX", Recdata[i]);           // printf ("\ n");

            unsigned char length = recdata[0];
            unsigned char datas[length];
            for (i=0;i<length;i++) {datas[i]=recdata[i+1];


        } Emit hiddataarrived (datas,length);

    } usleep (50*1000);
    an int mainhidinterface::hidapi_close (void) {hid_close (handle);
    IsOpen = 0; /* Free static HIDAPI objects.

    * * Hid_exit ();
    Delete handle;

return 0; }



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.