QT USB HID

本文介绍了如何在Qt环境中利用hidapi库与USBHID设备进行通信。首先在VS2010中编译hidapi源码生成库文件,然后在QtCreator中创建工程并链接hidapi库,接着编写测试代码实现设备的打开、写入和读取操作,最后确保exe运行时携带hidapi.dll。
摘要由CSDN通过智能技术生成

1. 下载uabhid的开源文件用VS2010生成hidapi.dll与hidapi.lib。

2.用Qt Creator建立一个工程USB_HID,将hidapi.h与hidapi.lib拷贝到工程下。

3.在USB_HID.pro最后添加生成的库

LIBS += -L$$_PRO_FILE_PWD_/  -lhidapi

4.简单测试代码

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include "hidapi.h"
const unsigned char Cmdbuf[9]={0x06,0x01,0x03,0x00,0x54,0x00,0x02,0x85,0xdb};
MainWindow::MainWindow(QWidget * parent) :
  QMainWindow(parent),
  ui(new Ui::MainWindow)
{
  ui->setupUi(this);
  int res;
  hid_device * handle;
  res = hid_init();  // 这一句可要可不要
  qDebug("hid init res %d", res);
  handle = hid_open(0xC258,0x3A0A,NULL);// 0x0483, 0x5750, NULL);
  unsigned char buf[128];
  unsigned char readBuf[65];
  memset(buf, 0, sizeof(buf));
  for(int i = 0;i < 9;i++)
      buf[i] = Cmdbuf[i]; //第一个字节是report id需要和下位机保持一致
  res = hid_write(handle, buf, 64);
  qDebug("hid write res %d", res);
  if(64 != res)
  {
    qDebug("%ls ", hid_error(handle));
  }
  else
  {
    res = hid_read_timeout(handle,readBuf, sizeof(readBuf),100);    //返回读取的字节,100为超时
    if(64 != res)
    {
      qDebug("%ls ", hid_error(handle));
    }
  }
  res = hid_exit();
  qDebug("hid exit res %d", res);
}

5.将hidapi.dll放到生成的exe的目录下即可运行。

详见

(1184条消息) Qt 调用USB HID设备读写_qt usb hid_xflySnail的博客-CSDN博客

测试代码,包含dll与lib

(1184条消息) QT用usbhid通讯资源-CSDN文库

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值