Qt下libusb-win32的使用(二)批量读写操作
一、概述
学习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的使用(二)批量读写操作的更多相关文章
- Maven 工程下 Spring MVC 站点配置 (二) Mybatis数据操作
详细的Spring MVC框架搭配在这个连接中: Maven 工程下 Spring MVC 站点配置 (一) Maven 工程下 Spring MVC 站点配置 (二) Mybatis数据操作 这篇主 ...
- Qt应用程序主窗口之二:拖放操作与打印文档
一.拖放操作 对于一个实用的应用程序,不仅希望能从文件菜单中打开一个文件,更希望可以通过拖动直接将桌面上的文件拖入程序界面上来打开,就像可以将.pro文件拖入Creator中来打开整个项目一样.Qt中 ...
- mac下对NTFS格式的磁盘进行读写操作
mac对NTFS格式的分区读写有很大的限制,网上看到很多相关的文章,都表明了一个信息:需要购买类似NTFS for mac这样的软件才能实现对NTFS格式的分区读写的权限,其实不然,mac自带的hdi ...
- Qt下libusb-win32的使用(转)
源:Qt下libusb-win32的使用(一)打印设备描述符 主要是在前一篇的基础上,学习libusb-win32的API使用.程序很简单,就是打印指定USB设备的设备描述符(当然其他描述符也是可以的 ...
- 一篇文章快速搞懂Qt文件读写操作
导读:Qt当中使用QFile类对文件进行读写操作,对文本文件也可以与QTextStream一起使用,这样读写操作会更加简便.QFileInfo可以用来获取文件的信息.QDir可以用于对文件夹进行操作. ...
- 【转】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这个库可以用作无驱 ...
随机推荐
- TPshop标签
很多cms 中有很多 标签, 商品标签 文章标签 列表标签 几十个标签, 让开发者头疼, 难记, TPshop开发者考虑到这点, 用了一个万能标签, 开发者非常方便实用 TPshop万能标签只要 ...
- Sql Server Snapshot和mysql MVCC
mysql 在一个事务A中插入一条数据 在B事务中查询到的还是以前的数据,可以select *from table,不被锁住 Sql Server 默认级别 读已提交 所以A事务在 X表插入数据, ...
- AJAX简单介绍
什么是AJAX Ajax 是 AsynchronousJavaScript and XML(以及 DHTML 等)的缩写. HTML 用于建立 Web表单并确定应用程序其它部分使用的字段. ·J ...
- linux 系统安装mysql (rpm)
其实按照本文安装成功,但是启动依然有问题:最好参考链接配置. http://blog.csdn.net/xiaoxiaoxuewen/article/details/7550107 我用的是ubunt ...
- 手机程序的app包名查找,可以在手机上查到
获取pkgname(安卓软件包名) 1. 先下载pkgName安装文件(pkgName.apk )并在手机上安装2. 打开刚刚安装的pkgName软件,软件会自动生成你手机上软件的包名列表,同时会在手 ...
- 【VirtualBox】ubuntu虚拟机与windows设置共享文件夹
第一步:配置 http://blog.csdn.net/a962804835/article/details/72820355 第二步:解决ubuntu下共享文件夹无访问权限的问题 http://bl ...
- VC6.0在win 8.1中的安装使用
http://blog.csdn.net/liups/article/details/14646663 一.首先是win8.1的安装 本人选择的是win 8.1简体中文专业N版,文件名: cn_win ...
- 五步整理你的css文件
鉴于实在无法忍受那种写一句就换一行的css写法,有个项目中的一个css文件竟然高达6000多行,看着实在蛋疼,无实今天下定决心整理一下,在DW里可以用正则很好的进行替换,步骤如下: 一:\r => ...
- Centos修改时间显示的时区,将UTC修改为CST
问题说明: 今天一同事反应,系统的时间不对和正常的时间差8个小时.就登录主机看了下时间 系统时间显示为: # date Fri Dec :: UTC # 备注:查看了下,正好和当前的时间差了8个小时. ...
- 开发kendo-ui弹窗组件
摘要: kendo-ui中只是提供了windwo插件,并没有提供页内弹窗插件.现在分享项目中自己定制的基于window组件的弹窗插件,如果你的项目也是用的kendo-ui,只需要将组件代码引到项目中即 ...