windows 通过hidapi库读取usb端口的报文数据

(1)这里使用的是第三方的hidapi库,我已经用VS编译源码,现在把hidapi.h和hidapi.dll和hidapi.lib直接分享下。
    链接:https://pan.baidu.com/s/1JxDqJhWstC34qxZTMeOqiA
    提取码:kqrj
    当然也可以自己去下载。
(2)使用qt调用hidapi库的。
(3)在源码里面引用lib文件和h文件,源码如下
    #include "../../../../Users/Administrator/Personal/123123/card/hidapi.h"
    #pragma comment(lib, "C:/Users/Administrator/Personal/123123/card/hidapi.lib")
(3) 因为接收数据是采用阻塞的方式,为了不影响主程序,我新建一个线程去接收数据,主要具体代码如下:
    #define MAX_STR 255
    void UBKAERThread::run()
    {
        int res;
        res = hid_init();
        wchar_t wstr[MAX_STR];
        int i;

        //0x0483和0x5750 是指定usb设备的vid和pid  在系统的硬件设备里面可以找到对应的值
        handle = hid_open(0x0483, 0x5750, NULL);
        if(handle == NULL)
        {
            return;
        }
        else
        {
        
        }

        res = hid_get_manufacturer_string(handle, wstr, MAX_STR);
        wprintf(L"Manufacturer String: %s\n", wstr);

        res = hid_get_product_string(handle, wstr, MAX_STR);
        wprintf(L"Product String: %s\n", wstr);

        res = hid_get_serial_number_string(handle, wstr, MAX_STR);
        wprintf(L"Serial Number String: (%d) %s\n", wstr[0], wstr);

        res = hid_get_indexed_string(handle, 1, wstr, MAX_STR);
        wprintf(L"Indexed String 1: %s\n", wstr);

        updataUsb();
    }


    void UBKAERThread::updataUsb()
    {
        qDebug("hid read start");
        int        res = hid_set_nonblocking(handle, 0);
        
        QString asd ;
        while (1)
        {
            res = hid_read(handle,buf,sizeof(buf));
            
            //16进制转换成10进制  
            for(int i = 0;i < sizeof(buf);i++)
            {
                char str[20];
                sprintf(str , "%02x",buf[i]);
                asd+=str ;
            }
        }
    }

原创文章 13 获赞 12 访问量 18万+

猜你喜欢

转载自blog.csdn.net/qq_14874791/article/details/106091058