Qt下libusb-win32的使用(二)批量读写操作

一、概述

        学习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 

 25 

 26 USB::USB()

 27 {

 28 #ifndef DEBUG_GUI

 29 

 30     usb_init(); /* initialize the library */

 31     //usb_set_debug(255);

 32     usb_find_busses(); /* find all busses */

 33     usb_find_devices(); /* find all connected devices */

 34 

 35     if (!(udev = open_dev())) {

 36         qDebug("error opening device: %s", usb_strerror());

 37         exit(1);

 38     } else

 39         qDebug("open success: device %04X:%04X opened", MY_VID, MY_PID);

 40 

 41     printf_device_descriptor(&dev->descriptor);

 42     my_init_usbdev();

 43 #endif

 44 

 45     textEdit = new QTextEdit(this);

 46     textEdit->setGeometry(50,20,230,40);

 47 

 48     sendButton = new QPushButton(this);

 49     sendButton->setText("send");

 50     sendButton->setGeometry(130,80,60,40);

 51     connect(sendButton,SIGNAL(clicked()),this,SLOT(send_slot()));

 52 

 53     readButton = new QPushButton(this);

 54     readButton->setText("read");

 55     readButton->setGeometry(130,140,60,40);

 56     connect(readButton,SIGNAL(clicked()),this,SLOT(read_slot()));

 57 

 58     recvLabel = new QLabel(this);

 59     recvLabel->setText("recv data:");

 60     recvLabel->setGeometry(20,180,280,40);

 61 

 62     //my_usb_get_device_list();

 63     resize(320, 240);

 64 }

 65 

 66 //关闭程序时被调用

 67 USB::~USB()

 68 {

 69 #ifndef DEBUG_GUI

 70     qDebug("close usb device.");

 71     usb_close(udev);

 72 #endif

 73 }

 74 

 75 //打开指定VID、PID的USB设备

 76 usb_dev_handle *USB::open_dev(void)

 77 {

 78     struct usb_bus *bus;

 79 

 80     for(bus = usb_get_busses(); bus; bus = bus->next) {

 81         for(dev = bus->devices; dev; dev = dev->next) {

 82             if((dev->descriptor.idVendor == MY_VID) && (dev->descriptor.idProduct == MY_PID)) {

 83                     return usb_open(dev);

 84             }

 85         }

 86     }

 87     return 0;

 88 }

 89 

 90 //打印USB设备描述符

 91 void USB::printf_device_descriptor(usb_device_descriptor *desc)

 92 {

 93         qDebug("bLength:             %u",     desc->bLength);

 94         qDebug("bDescriptorType:     %02Xh",  desc->bDescriptorType);

 95         qDebug("bcdUSB:              %04Xh",  desc->bcdUSB);

 96         qDebug("bDeviceClass:        %02Xh",  desc->bDeviceClass);

 97         qDebug("bDeviceSubClass:     %02Xh",  desc->bDeviceSubClass);

 98         qDebug("bDeviceProtocol:     %02Xh",  desc->bDeviceProtocol);

 99         qDebug("bMaxPacketSize0:     %02Xh",  desc->bMaxPacketSize0);

100         qDebug("idVendor:            %04Xh",  desc->idVendor);

101         qDebug("idProduct:           %04Xh",  desc->idProduct);

102         qDebug("bcdDevice:           %04Xh",  desc->bcdDevice);

103         qDebug("iManufacturer:       %u",     desc->iManufacturer);

104         qDebug("iProduct:            %u",     desc->iProduct);

105         qDebug("iSerialNumber:       %u",     desc->iSerialNumber);

106         qDebug("bNumConfigurations:  %u",     desc->bNumConfigurations);

107 }

108 

109 //指定USB设备的配置和接口

110 void USB::my_init_usbdev()

111 {

112     //libusb规定下面这两个函数必须要被调用

113     if (usb_set_configuration(udev, MY_CONFIG) < 0) {

114         qDebug("error setting config #%d: %s", MY_CONFIG, usb_strerror());

115         exit(1);

116     }

117     if (usb_claim_interface(udev, MY_INTF) < 0) {

118         qDebug("error claiming interface #%d:\n%s", MY_INTF, usb_strerror());

119         exit(1);

120     }

121 }

122 

123 //发送按钮响应函数

124 void USB::send_slot()

125 {

126     int ret, num;

127     QString s = textEdit->toPlainText();

128     QByteArray a = s.toLatin1();

129     char *tmp = a.data();

130 

131     num = s.length();

132     //qDebug()<<"text: "<<tmp<<"length: "<<num;

133     //批量写(同步)

134     ret = usb_bulk_write(udev, EP_OUT, tmp, num, 5000);

135     if (ret < 0) {

136             qDebug("error writing: %s", usb_strerror());

137             exit(1);

138     }

139 }

140 

141 //读按钮响应函数

142 void USB::read_slot()

143 {

144     int ret;

145     char readdata[BUF_SIZE];

146 

147     //批量读(同步)

148     ret = usb_bulk_read(udev, EP_IN, readdata, sizeof(readdata), 5000);

149     if (ret < 0) {

150             qDebug("error reading:%s", usb_strerror());

151             exit(1);

152     }

153     readdata[ret] = '\0';

154     //将接收到的数据显示在Label上

155     recvLabel->setText("recv: " + QLatin1String(readdata));

156 }

 

三、结果

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

Qt下libusb-win32的使用(二)批量读写操作

 

你可能感兴趣的:(Win32)