红联Linux门户
Linux帮助

Ubuntu15下Qt+libusb开发

发布时间:2015-06-24 15:13:59来源:blog.csdn.net/desert187作者:desert187

Ubuntu15下Qt5+libusb-1.0开发


下载和安装libusb-1.0

在Ubuntu15中可以从软件仓库安装libusb,当前的libusb版本为1.0。可以使用如下命令安装libusb的全部内容。
$sudo apt-get install libusb*
在Ubuntu15.04中,libusb-1.0的头文件和库文件分别被安装在“/usr/include/libusb-1.0”和“/usr/lib/x86_64-linux-gnu”目录下。


Qt Creator配置和使用libusb-1.0

使用Qt Creator创建项目,然后在工程文件(*.pro)中添加如下内容:

INCLUDEPATH += /usr/include/libusb-1.0
LIBS += -L"/usr/lib/x86_64-linux-gnu" -lusb-1.0


测试程序

一个打开、关闭、读写USB HID设备的示例代码如下。
qtusbtes.h

#ifndef QTUSBTES_H
#define QTUSBTES_H

#include <QMainWindow>
#include <libusb.h>

namespace Ui {
class QtUSBTes;
}

class QtUSBTes : public QMainWindow
{
Q_OBJECT

public:
explicit QtUSBTes(QWidget *parent = 0);
~QtUSBTes();

private slots:
void on_pushButton_clicked();

void on_pushButton_2_clicked();

private:
Ui::QtUSBTes *ui;
struct libusb_config_descriptor *conf_desc;
quint8 endpoint_in, endpoint_out;
libusb_device *dev;
int OutSize;
libusb_device_handle *handle;

void print_devs(libusb_device **devs);
};

#endif // QTUSBTES_H


qtusbtes.cpp

#include "qtusbtes.h"
#include "ui_qtusbtes.h"
#include <QDebug>

QtUSBTes::QtUSBTes(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::QtUSBTes)
{
ui->setupUi(this);

ui->pushButton_2->setText("打开");
}

QtUSBTes::~QtUSBTes()
{
delete ui;
}

void QtUSBTes::on_pushButton_clicked()
{
unsigned char intTransData[2] = {0xAA, 0x55};
OutSize = sizeof(intTransData);
int intTrans = libusb_interrupt_transfer(handle, endpoint_in,  intTransData, OutSize, &OutSize, 5000);
if (intTrans >= 0)
{
;
}
else
{
return;
}
intTransData[0] =  0x01;/**< 如果使用了 REPORT_ID 则 USB 发送数据缓冲区第一个字节必须为 REPORT_ID 以告知系统该数据属于哪个 ID */
intTransData[1] =  0x01;
intTrans = libusb_interrupt_transfer(handle, endpoint_out,  intTransData, OutSize, &OutSize, 5000);
if (intTrans >= 0)
{
;
}
else
{
return;
}
intTransData[0] =  0x02;/**< 如果使用了 REPORT_ID 则 USB 发送数据缓冲区第一个字节必须为 REPORT_ID 以告知系统该数据属于哪个 ID */
intTransData[1] =  0x01;
intTrans = libusb_interrupt_transfer(handle, endpoint_out,  intTransData, OutSize, &OutSize, 5000);
if (intTrans >= 0)
{
;
}
else
{
return;
}
intTrans = intTrans;
OutSize = OutSize;
}

void QtUSBTes::on_pushButton_2_clicked()
{
if("打开"==ui->pushButton_2->text())
{
ui->pushButton_2->setText("关闭");

int r;

r = libusb_init(NULL);
if (r < 0)
return;

handle = libusb_open_device_with_vid_pid(NULL, 1155, 22352);
dev = libusb_get_device(handle);

libusb_get_config_descriptor(dev, 0, &conf_desc);
for(int i=0; i<conf_desc->bNumInterfaces; i++)
{
for (int j=0; j<conf_desc->interface[i].num_altsetting; j++)
{
for (int k=0; k<conf_desc->interface[i].altsetting[j].bNumEndpoints; k++)
{
const struct libusb_endpoint_descriptor *endpoint = &conf_desc->interface[i].altsetting[j].endpoint[k];
if (endpoint->bEndpointAddress & LIBUSB_ENDPOINT_IN)
{
endpoint_in = endpoint->bEndpointAddress;
}
else
{
endpoint_out = endpoint->bEndpointAddress;
}
}
}
}
r = libusb_set_configuration(handle, 1);
if (r < 0)
return;
libusb_get_config_descriptor(dev, 0, &conf_desc);
ui->pushButton->setEnabled(true);
}
else
{
ui->pushButton_2->setText("打开");
libusb_free_config_descriptor(conf_desc);
libusb_close(handle);
libusb_exit(NULL);
ui->pushButton->setEnabled(false);
}
}


运行界面如下:

Ubuntu15下Qt+libusb开发


Ubuntu安装Qt开发环境:http://www.linuxdiyf.com/linux/11232.html

Linux下Qt的安装及用Eclipse开发Qt:http://www.linuxdiyf.com/linux/8816.html

Ubuntu下安装Qt环境及Qt Creator开发工具:http://www.linuxdiyf.com/linux/10958.html

在Ubuntu 15.04下安装git、vpnc、CMake及Qt Creator:http://www.linuxdiyf.com/linux/11810.html

Ubuntu下QT安装越来越方便了:http://www.linuxdiyf.com/linux/11290.html