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再使用第三方开源库时,由于库的类型基本上都是标准的 ...
随机推荐
- iOS 开发中的各种证书
公钥和私钥 转载自:http://www.samirchen.com/ios-certificates/ 先简单的介绍一下公钥和私钥.我们常见的加密算法有两类:对称加密算法(Symmetric Cry ...
- 10款免费Bootstrap后台模板演示及下载
自从有了类似Bootstrap这样强大的前端框架之后,无论我们是做静态页面,还是做网站主题,着实方便很多.即便有很多类似的其他国产.海外的前端框架比较,Bootstrap用户量以及功能文档还是比较大的 ...
- JS-运动基础(一)
<title>无标题文档</title> <style> #div1{width:200px;height:200px; background:red; posit ...
- gulp相关知识(2)
将之前的东西的理论部分整理了一下—— gulp 前端构建工具 它可以帮助我们完成一些自动化操作 它需要一些插件的支持 var gulp = require('gulp'); --> gulp命令 ...
- 多校 Babelfish
题目链接:http://acm.hust.edu.cn/vjudge/contest/124435#problem/A 密码:acm Sample Input dog ogday cat atcay ...
- C#无边框窗体移动 将事件绑定到想实现的控件上
[DllImport("user32.dll")] public static extern bool ReleaseCapture(); [DllImport("use ...
- XML回顾
xml加强 一.sax解析 1)原理: 读取xml的某个部分,解析一部分(读取一点,解析一点) 2)步骤: SAXParserFactory factory = SAXPa ...
- Linux通过防火墙禁止IP来防止攻击
1. iptables -I INPUT -s 211.1.0.0 -j DROP 禁止211.1.0.0这个IP访问服务器 2. iptables -I INPUT -s 211.1.0.0 -j ...
- zf-关于业务量图表没有出现统计柱形图问题
是实现类里的 objs[0] = ht.get("BUS_NAME");objs[1] = ht.get("NUM"); 这个写成小写了 改成大写即可,原来这个 ...
- dom4j解析xml实例
dom4j是一个java的XML API,类似jdom,用来读写XML文件,它性能优异.功能强大和极易使用等特点 所用jar包:dom4j-1.6.1.jar 需要解析的xml文件:people.xm ...