Qt下libusb-win32的使用(转)
源:Qt下libusb-win32的使用(一)打印设备描述符
主要是在前一篇的基础上,学习libusb-win32的API使用。程序很简单,就是打印指定USB设备的设备描述符(当然其他描述符也是可以的)。
#include "testlibusb.h" #define MY_VID 0x5345
#define MY_PID 0x1234 USB::USB()
{
usb_init(); /* initialize the library */
//usb_set_debug(255);
usb_find_busses(); /* find all busses */
usb_find_devices(); /* find all connected devices */ if (!(udev = open_dev())) {
qDebug("error opening device: %s", usb_strerror());
exit();
} else
qDebug("open success: device %04X:%04X opened", MY_VID, MY_PID); printf_device_descriptor(&dev->descriptor);
usb_close(udev); //my_usb_get_device_list();
resize(, );
} usb_dev_handle *USB::open_dev(void)
{
struct usb_bus *bus;
//struct usb_device *dev; for(bus = usb_get_busses(); bus; bus = bus->next) {
for(dev = bus->devices; dev; dev = dev->next) {
if((dev->descriptor.idVendor == MY_VID) && (dev->descriptor.idProduct == MY_PID)) {
return usb_open(dev);
}
}
}
return ;
} void USB::printf_device_descriptor(usb_device_descriptor *desc)
{
qDebug("bLength: %u", desc->bLength);
qDebug("bDescriptorType: %02Xh", desc->bDescriptorType);
qDebug("bcdUSB: %04Xh", desc->bcdUSB);
qDebug("bDeviceClass: %02Xh", desc->bDeviceClass);
qDebug("bDeviceSubClass: %02Xh", desc->bDeviceSubClass);
qDebug("bDeviceProtocol: %02Xh", desc->bDeviceProtocol);
qDebug("bMaxPacketSize0: %02Xh", desc->bMaxPacketSize0);
qDebug("idVendor: %04Xh", desc->idVendor);
qDebug("idProduct: %04Xh", desc->idProduct);
qDebug("bcdDevice: %04Xh", desc->bcdDevice);
qDebug("iManufacturer: %u", desc->iManufacturer);
qDebug("iProduct: %u", desc->iProduct);
qDebug("iSerialNumber: %u", desc->iSerialNumber);
qDebug("bNumConfigurations: %u", desc->bNumConfigurations);
}
这里我指定的USB设备是Tiny6410开发板。运行效果如下:

下一步就是上位机与开发板进行数据互传。
一、概述
学习libusb-win32的使用。使用批量传输方式与USB开发板进行数据读、写操作。上位机使用Qt做界面, 使用USB开发板的端点2作为批量传输端点。
二、实现
代码比较简单,直接给出,如下:
#include "testlibusb.h" //for Tiny6410
//#define MY_VID 0x5345
//#define MY_PID 0x1234 //for 51 USB Board
#define MY_VID 0x8888
#define MY_PID 0x0001 // Device configuration and interface id.
#define MY_CONFIG 1
#define MY_INTF 0 // Device endpoint 2
#define EP_IN 0x82
#define EP_OUT 0x02 // Device of bytes to transfer.
#define BUF_SIZE 64 //#define DEBUG_GUI USB::USB()
{
#ifndef DEBUG_GUI usb_init(); /* initialize the library */
//usb_set_debug(255);
usb_find_busses(); /* find all busses */
usb_find_devices(); /* find all connected devices */ if (!(udev = open_dev())) {
qDebug("error opening device: %s", usb_strerror());
exit();
} else
qDebug("open success: device %04X:%04X opened", MY_VID, MY_PID); printf_device_descriptor(&dev->descriptor);
my_init_usbdev();
#endif textEdit = new QTextEdit(this);
textEdit->setGeometry(,,,); sendButton = new QPushButton(this);
sendButton->setText("send");
sendButton->setGeometry(,,,);
connect(sendButton,SIGNAL(clicked()),this,SLOT(send_slot())); readButton = new QPushButton(this);
readButton->setText("read");
readButton->setGeometry(,,,);
connect(readButton,SIGNAL(clicked()),this,SLOT(read_slot())); recvLabel = new QLabel(this);
recvLabel->setText("recv data:");
recvLabel->setGeometry(,,,); //my_usb_get_device_list();
resize(, );
} //关闭程序时被调用
USB::~USB()
{
#ifndef DEBUG_GUI
qDebug("close usb device.");
usb_close(udev);
#endif
} //打开指定VID、PID的USB设备
usb_dev_handle *USB::open_dev(void)
{
struct usb_bus *bus; for(bus = usb_get_busses(); bus; bus = bus->next) {
for(dev = bus->devices; dev; dev = dev->next) {
if((dev->descriptor.idVendor == MY_VID) && (dev->descriptor.idProduct == MY_PID)) {
return usb_open(dev);
}
}
}
return ;
} //打印USB设备描述符
void USB::printf_device_descriptor(usb_device_descriptor *desc)
{
qDebug("bLength: %u", desc->bLength);
qDebug("bDescriptorType: %02Xh", desc->bDescriptorType);
qDebug("bcdUSB: %04Xh", desc->bcdUSB);
qDebug("bDeviceClass: %02Xh", desc->bDeviceClass);
qDebug("bDeviceSubClass: %02Xh", desc->bDeviceSubClass);
qDebug("bDeviceProtocol: %02Xh", desc->bDeviceProtocol);
qDebug("bMaxPacketSize0: %02Xh", desc->bMaxPacketSize0);
qDebug("idVendor: %04Xh", desc->idVendor);
qDebug("idProduct: %04Xh", desc->idProduct);
qDebug("bcdDevice: %04Xh", desc->bcdDevice);
qDebug("iManufacturer: %u", desc->iManufacturer);
qDebug("iProduct: %u", desc->iProduct);
qDebug("iSerialNumber: %u", desc->iSerialNumber);
qDebug("bNumConfigurations: %u", desc->bNumConfigurations);
} //指定USB设备的配置和接口
void USB::my_init_usbdev()
{
//libusb规定下面这两个函数必须要被调用
if (usb_set_configuration(udev, MY_CONFIG) < ) {
qDebug("error setting config #%d: %s", MY_CONFIG, usb_strerror());
exit();
}
if (usb_claim_interface(udev, MY_INTF) < ) {
qDebug("error claiming interface #%d:\n%s", MY_INTF, usb_strerror());
exit();
}
} //发送按钮响应函数
void USB::send_slot()
{
int ret, num;
QString s = textEdit->toPlainText();
QByteArray a = s.toLatin1();
char *tmp = a.data(); num = s.length();
//qDebug()<<"text: "<<tmp<<"length: "<<num;
//批量写(同步)
ret = usb_bulk_write(udev, EP_OUT, tmp, num, );
if (ret < ) {
qDebug("error writing: %s", usb_strerror());
exit();
}
} //读按钮响应函数
void USB::read_slot()
{
int ret;
char readdata[BUF_SIZE]; //批量读(同步)
ret = usb_bulk_read(udev, EP_IN, readdata, sizeof(readdata), );
if (ret < ) {
qDebug("error reading:%s", usb_strerror());
exit();
}
readdata[ret] = '\0';
//将接收到的数据显示在Label上
recvLabel->setText("recv: " + QLatin1String(readdata));
}
三、结果
运行上位机程序,在编辑框输入一些字符(数字),然后点击“send”按钮将数据发送给USB设备,点击“read”按钮将USB设备接收到的数据读回到上位机并显示在界面上,效果如下:

Qt下libusb-win32的使用(转)的更多相关文章
- 【转】Qt下使用glut库
ps:这个说的很明白,尤其是win10环境下用mingw环境时编程时碰到的问题, 1.加 windows.h 2.在.pro 添加libs 博文地址:Qt下使用glut库 本人使用的环境 ...
- VC++或QT下 高精度 多媒体定时器
在VC编程中,用SetTimer可以定义一个定时器,到时间了,就响应OnTimer消息,但这种定时器精度太低了.如果需要精度更高一些的定时器(精 确到1ms),可以使用下面的高精度多媒体定时器进行代码 ...
- Qt下libusb-win32的使用方法(转)
源:Qt下libusb-win32的使用方法 之前一直找不到适合WIN7下的Tiny6410的USB下载软件,正好这几天开始学习USB,所以打算自己写一个专门用于Tiny6410的WIN7下的USB下 ...
- Qt下libusb-win32的使用方法
之前一直找不到适合WIN7下的Tiny6410的USB下载软件,正好这几天开始学习USB,所以打算自己写一个专门用于Tiny6410的WIN7下的USB下载软件. 发现了libusb这个库可以用作无驱 ...
- delphi中formatFloat代码初探(在qt下实现floatformat的函数)
由于项目需要,需要在qt下实现floatformat的函数.之前写过一个,但是写得不好.决定重新写一个,参考delphi xe2下的实现.把xe2下的相关代码都看了一遍,xe2的代码思路在这里贴出来. ...
- QT下资源使用和资源占用…(可以动态加载资源文件,这样不占内存)
原文地址:关于QT下资源使用和资源占用内存过多的问题作者:技术成就梦想 最近研究了一下如何从外部动态调用图片的问题,从而研究了图片资源的使用方法.网上最常见的帖子是这个,感觉总结的还不错. h ...
- windows下实现win32俄罗斯方块练手,编程的几点心得
编程珠玑2阅读笔记: 1.使用c语言性能监视器,完成对代码的调优工作 2.关联数组: 拓扑排序算法,可以用于当存在遮挡的时候决定三维场景的绘制顺序. 3.小型算法中的测试与调试工具 脚手架程序:&l ...
- 关于QT下资源使用和资源占用…
原文地址:关于QT下资源使用和资源占用内存过多的问题作者:技术成就梦想 最近研究了一下如何从外部动态调用图片的问题,从而研究了图片资源的使用方法.网上最常见的帖子是这个,感觉总结的还不错. h ...
- Qt下QString转char*
Qt下面,字符串都用QString,确实给开发者提供了方便,想想VC里面定义的各种变量类型,而且函数参数类型五花八门,经常需要今年新那个类型转换 Qt再使用第三方开源库时,由于库的类型基本上都是标准的 ...
随机推荐
- 【语法】修饰符 static extern const
转载自:http://my.oschina.net/u/2560887/blog/552683 一.C语言中的static的作用 在C语言中,static的字面意思很容易把我们导入歧途,其实它的作用有 ...
- Form类的KeyPreview属性
首先需要知道一个知识点,Form控件,Panel控件和GroupBox控件等容器类控件默认是不接收焦点的,而是负责管理容器中控件的焦点.当容器控件被选中时,默认把焦点传送至容器内Tab顺序为0的控件. ...
- android cts 命令的说明
Host help showthis message 帮助文档 exit exitcts command line 退出CTS ls 全部用l替代,--plan直接用p替代,也即 l p .其他类似 ...
- POJ2299--树状数组求逆序数
Description In this problem, you have to analyze a particular sorting algorithm. The algorithm proce ...
- Oracle sql优化之分析函数优化标量子查询
待优化语句如下 select a.code as code, a.m_code as m_code,a.stktype as f_stype,a.e_year as e_year, b.sname a ...
- git bash退回上一个文件夹
cd ..\ a@w3311 MINGW32 /f/Projects/crm (master) $ cd..\ > bash: cd..: command not found a@w3311 M ...
- 如何获取url访问历史记录
在院里的群里,有人问了这么一个问题: A页面提交表单到B页面,然后在B页面点了后退,如果在A页面上判断是直接访问的还是后退进去的呢?我不想改B页面. 于是乎本着热心人的想法,我就帮他搞了搞,首先我想到 ...
- table固定宽度高度, 及overflow省略号
整体设置标签为:td {text-overflow: ellipsis; white-space: nowrap; overflow: hidden; } table fix设置 <table ...
- android AsyncTask介绍 转载
http://www.cnblogs.com/devinzhang/archive/2012/02/13/2350070.html AsyncTask和Handler对比 1 ) AsyncTask实 ...
- 设置DIV根据内容自动调整高度的三个方法
Div即父容器在Firefox.Chrome.Safari中不会根据内容自动调节高度,我们看下面的HTML代码: <divid="main"><divid=&qu ...