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这个库可以用作无驱 ...
随机推荐
- 用OpenGL绘制平滑着色的三角形与相交区域的混合着色
一.三角形的绘制 在OpenGL中,面是由多边形构成的.三角形可能是最简单的多边形,它有三条边.可以使用GL_TRIANGLES模式通过把三个顶点连接到一起而绘出三角形. 使用GL_TRIANGLE_ ...
- chrome 下改动 agent 的方法
前言 这篇文章和 tiankonguse 的个人站点里的文章保持同步. 非常早之前,在 chrome 下改动 agent 的方法是使用 chrome 插件. 后来 chrome 的某一个版本号中自带这 ...
- LigerUI可编辑表格左下角出现白色小方块遮罩层问题解决办法
LigerUI已经研究了一段时间,总体感觉还不错,基于jQuery开发,框架提供了丰富的UI组件,尤其LigerUI表格,功能还是挺强大的 在使用LigerUI可编辑表格的时候,发现一个小小的问题 当 ...
- css 阻止元素中的文本。双击选中
//firefox -moz-user-select: none; //chrome.safari -webkit-user-select: none; //ie -ms-user-select: n ...
- fork 至 “sys_clone" SyS_clone
注:glibc-2.17中fork的相应系统调用是sys_clone及SyS_clone.有人说调用的是sys_fork,但是我持否定意见,如果我们向真的来发起系统调用可以使用syscall. for ...
- Python的Beautiful Soup简单使用
Beautiful Soup是python的一个库,最主要的功能是从网页抓取数据 Beautiful Soup提供一些简单的.python式的函数用来处理导航.搜索.修改分析树等功能 它是一个工具箱, ...
- C语言的结构体
举例,一个结构体的定义如下: typedef struct _foo { ]; int age; int sex; } foo; 对齐 如果直接对上面的结构体作sizeof()运算: printf( ...
- re.match re.search re.findall区别
re正则表达式里面,常用的三种方法的区别. re.macth和search匹配得到的是match对象,findall得到的是一个列表. match从字符串开头开始匹配,search返回与正则表达式匹配 ...
- [原]C# 常用函数统计
1.获取MD5 string MD5Compute(string strPwd) { MD5CryptoServiceProvider m5 = new MD5CryptoServiceProvide ...
- [转]WPF入口Application
1.WPF和 传统的WinForm 类似, WPF 同样需要一个 Application 来统领一些全局的行为和操作,并且每个 Domain (应用程序域)中只能有一个 Application 实例存 ...