一、概述

学习libusb-win32的使用。使用批量传输方式与USB开发板进行数据读、写操作。上位机使用Qt做界面, 使用USB开发板的端点2作为批量传输端点。

二、实现

代码比较简单,直接给出,如下:

  1. #include "testlibusb.h"
  2.  
  3. //for Tiny6410
  4. //#define MY_VID 0x5345
  5. //#define MY_PID 0x1234
  6.  
  7. //for 51 USB Board
  8. #define MY_VID 0x8888
  9. #define MY_PID 0x0001
  10.  
  11. // Device configuration and interface id.
  12. #define MY_CONFIG 1
  13. #define MY_INTF 0
  14.  
  15. // Device endpoint 2
  16. #define EP_IN 0x82
  17. #define EP_OUT 0x02
  18.  
  19. // Device of bytes to transfer.
  20. #define BUF_SIZE 64
  21.  
  22. //#define DEBUG_GUI
  23.  
  24. USB::USB()
  25. {
  26. #ifndef DEBUG_GUI
  27.  
  28. usb_init(); /* initialize the library */
  29. //usb_set_debug(255);
  30. usb_find_busses(); /* find all busses */
  31. usb_find_devices(); /* find all connected devices */
  32.  
  33. if (!(udev = open_dev())) {
  34. qDebug("error opening device: %s", usb_strerror());
  35. exit();
  36. } else
  37. qDebug("open success: device %04X:%04X opened", MY_VID, MY_PID);
  38.  
  39. printf_device_descriptor(&dev->descriptor);
  40. my_init_usbdev();
  41. #endif
  42.  
  43. textEdit = new QTextEdit(this);
  44. textEdit->setGeometry(,,,);
  45.  
  46. sendButton = new QPushButton(this);
  47. sendButton->setText("send");
  48. sendButton->setGeometry(,,,);
  49. connect(sendButton,SIGNAL(clicked()),this,SLOT(send_slot()));
  50.  
  51. readButton = new QPushButton(this);
  52. readButton->setText("read");
  53. readButton->setGeometry(,,,);
  54. connect(readButton,SIGNAL(clicked()),this,SLOT(read_slot()));
  55.  
  56. recvLabel = new QLabel(this);
  57. recvLabel->setText("recv data:");
  58. recvLabel->setGeometry(,,,);
  59.  
  60. //my_usb_get_device_list();
  61. resize(, );
  62. }
  63.  
  64. //关闭程序时被调用
  65. USB::~USB()
  66. {
  67. #ifndef DEBUG_GUI
  68. qDebug("close usb device.");
  69. usb_close(udev);
  70. #endif
  71. }
  72.  
  73. //打开指定VID、PID的USB设备
  74. usb_dev_handle *USB::open_dev(void)
  75. {
  76. struct usb_bus *bus;
  77.  
  78. for(bus = usb_get_busses(); bus; bus = bus->next) {
  79. for(dev = bus->devices; dev; dev = dev->next) {
  80. if((dev->descriptor.idVendor == MY_VID) && (dev->descriptor.idProduct == MY_PID)) {
  81. return usb_open(dev);
  82. }
  83. }
  84. }
  85. return ;
  86. }
  87.  
  88. //打印USB设备描述符
  89. void USB::printf_device_descriptor(usb_device_descriptor *desc)
  90. {
  91. qDebug("bLength: %u", desc->bLength);
  92. qDebug("bDescriptorType: %02Xh", desc->bDescriptorType);
  93. qDebug("bcdUSB: %04Xh", desc->bcdUSB);
  94. qDebug("bDeviceClass: %02Xh", desc->bDeviceClass);
  95. qDebug("bDeviceSubClass: %02Xh", desc->bDeviceSubClass);
  96. qDebug("bDeviceProtocol: %02Xh", desc->bDeviceProtocol);
  97. qDebug("bMaxPacketSize0: %02Xh", desc->bMaxPacketSize0);
  98. qDebug("idVendor: %04Xh", desc->idVendor);
  99. qDebug("idProduct: %04Xh", desc->idProduct);
  100. qDebug("bcdDevice: %04Xh", desc->bcdDevice);
  101. qDebug("iManufacturer: %u", desc->iManufacturer);
  102. qDebug("iProduct: %u", desc->iProduct);
  103. qDebug("iSerialNumber: %u", desc->iSerialNumber);
  104. qDebug("bNumConfigurations: %u", desc->bNumConfigurations);
  105. }
  106.  
  107. //指定USB设备的配置和接口
  108. void USB::my_init_usbdev()
  109. {
  110. //libusb规定下面这两个函数必须要被调用
  111. if (usb_set_configuration(udev, MY_CONFIG) < ) {
  112. qDebug("error setting config #%d: %s", MY_CONFIG, usb_strerror());
  113. exit();
  114. }
  115. if (usb_claim_interface(udev, MY_INTF) < ) {
  116. qDebug("error claiming interface #%d:\n%s", MY_INTF, usb_strerror());
  117. exit();
  118. }
  119. }
  120.  
  121. //发送按钮响应函数
  122. void USB::send_slot()
  123. {
  124. int ret, num;
  125. QString s = textEdit->toPlainText();
  126. QByteArray a = s.toLatin1();
  127. char *tmp = a.data();
  128.  
  129. num = s.length();
  130. //qDebug()<<"text: "<<tmp<<"length: "<<num;
  131. //批量写(同步)
  132. ret = usb_bulk_write(udev, EP_OUT, tmp, num, );
  133. if (ret < ) {
  134. qDebug("error writing: %s", usb_strerror());
  135. exit();
  136. }
  137. }
  138.  
  139. //读按钮响应函数
  140. void USB::read_slot()
  141. {
  142. int ret;
  143. char readdata[BUF_SIZE];
  144.  
  145. //批量读(同步)
  146. ret = usb_bulk_read(udev, EP_IN, readdata, sizeof(readdata), );
  147. if (ret < ) {
  148. qDebug("error reading:%s", usb_strerror());
  149. exit();
  150. }
  151. readdata[ret] = '\0';
  152. //将接收到的数据显示在Label上
  153. recvLabel->setText("recv: " + QLatin1String(readdata));
  154. }

三、结果

运行上位机程序,在编辑框输入一些字符(数字),然后点击“send”按钮将数据发送给USB设备,点击“read”按钮将USB设备接收到的数据读回到上位机并显示在界面上,效果如下:

Qt下libusb-win32的使用(二)批量读写操作的更多相关文章

  1. Maven 工程下 Spring MVC 站点配置 (二) Mybatis数据操作

    详细的Spring MVC框架搭配在这个连接中: Maven 工程下 Spring MVC 站点配置 (一) Maven 工程下 Spring MVC 站点配置 (二) Mybatis数据操作 这篇主 ...

  2. Qt应用程序主窗口之二:拖放操作与打印文档

    一.拖放操作 对于一个实用的应用程序,不仅希望能从文件菜单中打开一个文件,更希望可以通过拖动直接将桌面上的文件拖入程序界面上来打开,就像可以将.pro文件拖入Creator中来打开整个项目一样.Qt中 ...

  3. mac下对NTFS格式的磁盘进行读写操作

    mac对NTFS格式的分区读写有很大的限制,网上看到很多相关的文章,都表明了一个信息:需要购买类似NTFS for mac这样的软件才能实现对NTFS格式的分区读写的权限,其实不然,mac自带的hdi ...

  4. Qt下libusb-win32的使用(转)

    源:Qt下libusb-win32的使用(一)打印设备描述符 主要是在前一篇的基础上,学习libusb-win32的API使用.程序很简单,就是打印指定USB设备的设备描述符(当然其他描述符也是可以的 ...

  5. 一篇文章快速搞懂Qt文件读写操作

    导读:Qt当中使用QFile类对文件进行读写操作,对文本文件也可以与QTextStream一起使用,这样读写操作会更加简便.QFileInfo可以用来获取文件的信息.QDir可以用于对文件夹进行操作. ...

  6. 【转】Qt下使用glut库

    ps:这个说的很明白,尤其是win10环境下用mingw环境时编程时碰到的问题, 1.加 windows.h 2.在.pro 添加libs     博文地址:Qt下使用glut库   本人使用的环境 ...

  7. VC++或QT下 高精度 多媒体定时器

    在VC编程中,用SetTimer可以定义一个定时器,到时间了,就响应OnTimer消息,但这种定时器精度太低了.如果需要精度更高一些的定时器(精 确到1ms),可以使用下面的高精度多媒体定时器进行代码 ...

  8. Qt下libusb-win32的使用方法(转)

    源:Qt下libusb-win32的使用方法 之前一直找不到适合WIN7下的Tiny6410的USB下载软件,正好这几天开始学习USB,所以打算自己写一个专门用于Tiny6410的WIN7下的USB下 ...

  9. Qt下libusb-win32的使用方法

    之前一直找不到适合WIN7下的Tiny6410的USB下载软件,正好这几天开始学习USB,所以打算自己写一个专门用于Tiny6410的WIN7下的USB下载软件. 发现了libusb这个库可以用作无驱 ...

随机推荐

  1. TPshop标签

    很多cms 中有很多 标签, 商品标签 文章标签  列表标签  几十个标签, 让开发者头疼, 难记,  TPshop开发者考虑到这点, 用了一个万能标签, 开发者非常方便实用 TPshop万能标签只要 ...

  2. Sql Server Snapshot和mysql MVCC

    mysql 在一个事务A中插入一条数据 在B事务中查询到的还是以前的数据,可以select *from table,不被锁住   Sql Server 默认级别 读已提交 所以A事务在 X表插入数据, ...

  3. AJAX简单介绍

     什么是AJAX Ajax 是 AsynchronousJavaScript and XML(以及 DHTML 等)的缩写. HTML 用于建立 Web表单并确定应用程序其它部分使用的字段. ·J ...

  4. linux 系统安装mysql (rpm)

    其实按照本文安装成功,但是启动依然有问题:最好参考链接配置. http://blog.csdn.net/xiaoxiaoxuewen/article/details/7550107 我用的是ubunt ...

  5. 手机程序的app包名查找,可以在手机上查到

    获取pkgname(安卓软件包名) 1. 先下载pkgName安装文件(pkgName.apk )并在手机上安装2. 打开刚刚安装的pkgName软件,软件会自动生成你手机上软件的包名列表,同时会在手 ...

  6. 【VirtualBox】ubuntu虚拟机与windows设置共享文件夹

    第一步:配置 http://blog.csdn.net/a962804835/article/details/72820355 第二步:解决ubuntu下共享文件夹无访问权限的问题 http://bl ...

  7. VC6.0在win 8.1中的安装使用

    http://blog.csdn.net/liups/article/details/14646663 一.首先是win8.1的安装 本人选择的是win 8.1简体中文专业N版,文件名: cn_win ...

  8. 五步整理你的css文件

    鉴于实在无法忍受那种写一句就换一行的css写法,有个项目中的一个css文件竟然高达6000多行,看着实在蛋疼,无实今天下定决心整理一下,在DW里可以用正则很好的进行替换,步骤如下: 一:\r => ...

  9. Centos修改时间显示的时区,将UTC修改为CST

    问题说明: 今天一同事反应,系统的时间不对和正常的时间差8个小时.就登录主机看了下时间 系统时间显示为: # date Fri Dec :: UTC # 备注:查看了下,正好和当前的时间差了8个小时. ...

  10. 开发kendo-ui弹窗组件

    摘要: kendo-ui中只是提供了windwo插件,并没有提供页内弹窗插件.现在分享项目中自己定制的基于window组件的弹窗插件,如果你的项目也是用的kendo-ui,只需要将组件代码引到项目中即 ...