一、概述

学习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. 用OpenGL绘制平滑着色的三角形与相交区域的混合着色

    一.三角形的绘制 在OpenGL中,面是由多边形构成的.三角形可能是最简单的多边形,它有三条边.可以使用GL_TRIANGLES模式通过把三个顶点连接到一起而绘出三角形. 使用GL_TRIANGLE_ ...

  2. chrome 下改动 agent 的方法

    前言 这篇文章和 tiankonguse 的个人站点里的文章保持同步. 非常早之前,在 chrome 下改动 agent 的方法是使用 chrome 插件. 后来 chrome 的某一个版本号中自带这 ...

  3. LigerUI可编辑表格左下角出现白色小方块遮罩层问题解决办法

    LigerUI已经研究了一段时间,总体感觉还不错,基于jQuery开发,框架提供了丰富的UI组件,尤其LigerUI表格,功能还是挺强大的 在使用LigerUI可编辑表格的时候,发现一个小小的问题 当 ...

  4. css 阻止元素中的文本。双击选中

    //firefox -moz-user-select: none; //chrome.safari -webkit-user-select: none; //ie -ms-user-select: n ...

  5. fork 至 “sys_clone" SyS_clone

    注:glibc-2.17中fork的相应系统调用是sys_clone及SyS_clone.有人说调用的是sys_fork,但是我持否定意见,如果我们向真的来发起系统调用可以使用syscall. for ...

  6. Python的Beautiful Soup简单使用

    Beautiful Soup是python的一个库,最主要的功能是从网页抓取数据 Beautiful Soup提供一些简单的.python式的函数用来处理导航.搜索.修改分析树等功能 它是一个工具箱, ...

  7. C语言的结构体

    举例,一个结构体的定义如下: typedef struct _foo { ]; int age; int sex; } foo; 对齐 如果直接对上面的结构体作sizeof()运算: printf( ...

  8. re.match re.search re.findall区别

    re正则表达式里面,常用的三种方法的区别. re.macth和search匹配得到的是match对象,findall得到的是一个列表. match从字符串开头开始匹配,search返回与正则表达式匹配 ...

  9. [原]C# 常用函数统计

    1.获取MD5 string MD5Compute(string strPwd) { MD5CryptoServiceProvider m5 = new MD5CryptoServiceProvide ...

  10. [转]WPF入口Application

    1.WPF和 传统的WinForm 类似, WPF 同样需要一个 Application 来统领一些全局的行为和操作,并且每个 Domain (应用程序域)中只能有一个 Application 实例存 ...