1.编译好的库文件,库文件链接:
https://download.csdn.net/download/qq_57041630/88800859
将文件夹中文件,放置工程目录,工程文件添加代码:
LIBS += -L$$_PRO_FILE_PWD_/ -lhidapi
2.将下载好的文件夹中的hidapi.h添加到现有文件
3.创建一个线程类,该线程类父类为QThread
下面贴上代码:
usb_thread.h
#ifndef USB_THREAD_H
#define USB_THREAD_H
#include "hidapi.h"
#include <QThread>
extern hid_device *handle;
class USB_Thread : public QThread
{
Q_OBJECT
signals:
void message(const QString& info);
void progress(int present);
void updateString11(QString);
protected:
void run();
public:
USB_Thread();
void closeThread();
unsigned char* getData();
QString getDatastr();
private:
volatile bool isStop; //isStop是易失性变量,需要用volatile进行申明
QString str;
unsigned char data[65] = {'0'};
};
#endif // USB_THREAD_H
usb_thread.cpp
#include "usb_thread.h"
#include <QDebug>
#include <QMutex>
USB_Thread::USB_Thread()
{
isStop = false;
}
void USB_Thread::closeThread()
{
isStop = true;
}
unsigned char* USB_Thread::getData()
{
return data;
}
QString USB_Thread::getDatastr()
{
return str;
}
void USB_Thread::run()
{
unsigned char rbuf[65];
QString str;
while (1)
{
if(isStop)
return;
for(int i=0;i<65;i++)
rbuf[i]=0x0b;
hid_read(handle,rbuf,65);//读一包数据,判断是否有数据进来
// sleep(5);//此处延时不可省略,否则接收数据不完全
if(rbuf[0]!=0x0b){//判断包头包尾是否自己写入的
while(1){
for(int i = 0;i < 65;i++){
str+=tr("%1,").arg(data[i]);
this->data[i] = rbuf[i];
// qDebug()<<"data["<<i<<"]"<<data[i];
}
qDebug()<<"recvstr:"<<str;
break;
}
qDebug()<<str;
this->str = str;
// emit updateString11(str);
}
else
str.clear();
}
}
4.发送主程序如下:
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "hidapi.h"
#include "usb_thread.h"
#ifdef Q_OS_WIN32
#include <windows.h>
#pragma comment(lib, "Setupapi.lib")
#pragma comment(lib, "hidapi.lib")
#endif
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
void run();
void updata();
private slots:
void on_pushButton_clicked();
void on_pushButton_3_clicked();
// void updateStr(QString);
private:
Ui::MainWindow *ui;
USB_Thread *thread1;
bool recvFlag = false;
unsigned char pinNo;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <QMessageBox>
#include <QByteArray>
#include <iostream>
hid_device *handle;
unsigned char buf_IN[65];
bool start_flag=false;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
hid_init();//不初始化也可以。在open里会初始化
qDebug()<<handle;
handle = hid_open(vid, pid, NULL);//模块
qDebug()<<handle;
if(handle==NULL)
QMessageBox::information(this, "warn", "USB连接失败!");
else
{
hid_set_nonblocking(handle, 1);
thread1=new USB_Thread;
thread1->start();
// QMetaObject::Connection connection = connect(thread1,SIGNAL(updateString11(QString)),this,SLOT(updateStr(QString)));
}
}
MainWindow::~MainWindow()
{
thread1->closeThread();
delete ui;
}
void MainWindow::run()
{
//
QByteArray data(65, 0);
data[1] = 0x01;
data[2] = 0x02;
data[3] = 0x03;
data[4] = 0x04;
...
...
data1 = reinterpret_cast<const unsigned char*>(data.constData());
hid_write(handle, data1, 65);
}
void MainWindow::updata()
{
unsigned char* data = thread1->getData();
}
//手动接收
void MainWindow::on_pushButton_clicked()
{
qDebug("hid read start");
int res;
QString show_text;
start_flag=true;
while (start_flag) {
QCoreApplication::processEvents();
QThread::msleep(1000);
res = hid_read(handle,buf_IN,65);
qDebug()<<res;
if(res > 0)
{
for(int i = 0;i < 65;i++){
// qDebug("buf[%d]:0x%02x",i,buf_IN[i]);
show_text+=tr("%1,").arg(buf_IN[i]);
}
ui->textEdit->setText(show_text);
show_text.clear();
}
}
}
//手动发送
void MainWindow::on_pushButton_3_clicked()
{
run();
}
代码中的信号与槽注释了,使用updata函数传递接收到的值,updata函数主要是对接收值进行判断,所以没有具体内容。
6.遇到的问题:
(1).写入的字节数必须要是65,不知道为啥