主要是在前一篇的基础上,学习libusb-win32的API使用。程序很简单,就是打印指定USB设备的设备描述符(当然其他描述符也是可以的)。
1 #include "testlibusb.h" 2 3 4 #define MY_VID 0x5345 5 #define MY_PID 0x1234 6 7 8 USB::USB() 9 { 10 usb_init(); /* initialize the library */ 11 //usb_set_debug(255); 12 usb_find_busses(); /* find all busses */ 13 usb_find_devices(); /* find all connected devices */ 14 15 if (!(udev = open_dev())) { 16 qDebug("error opening device: %s", usb_strerror()); 17 exit(1); 18 } else 19 qDebug("open success: device %04X:%04X opened", MY_VID, MY_PID); 20 21 printf_device_descriptor(&dev->descriptor); 22 usb_close(udev); 23 24 //my_usb_get_device_list(); 25 resize(320, 240); 26 } 27 28 usb_dev_handle *USB::open_dev(void) 29 { 30 struct usb_bus *bus; 31 //struct usb_device *dev; 32 33 for(bus = usb_get_busses(); bus; bus = bus->next) { 34 for(dev = bus->devices; dev; dev = dev->next) { 35 if((dev->descriptor.idVendor == MY_VID) && (dev->descriptor.idProduct == MY_PID)) { 36 return usb_open(dev); 37 } 38 } 39 } 40 return 0; 41 } 42 43 void USB::printf_device_descriptor(usb_device_descriptor *desc) 44 { 45 qDebug("bLength: %u", desc->bLength); 46 qDebug("bDescriptorType: %02Xh", desc->bDescriptorType); 47 qDebug("bcdUSB: %04Xh", desc->bcdUSB); 48 qDebug("bDeviceClass: %02Xh", desc->bDeviceClass); 49 qDebug("bDeviceSubClass: %02Xh", desc->bDeviceSubClass); 50 qDebug("bDeviceProtocol: %02Xh", desc->bDeviceProtocol); 51 qDebug("bMaxPacketSize0: %02Xh", desc->bMaxPacketSize0); 52 qDebug("idVendor: %04Xh", desc->idVendor); 53 qDebug("idProduct: %04Xh", desc->idProduct); 54 qDebug("bcdDevice: %04Xh", desc->bcdDevice); 55 qDebug("iManufacturer: %u", desc->iManufacturer); 56 qDebug("iProduct: %u", desc->iProduct); 57 qDebug("iSerialNumber: %u", desc->iSerialNumber); 58 qDebug("bNumConfigurations: %u", desc->bNumConfigurations); 59 }
这里我指定的USB设备是Tiny6410开发板。运行效果如下:
下一步就是上位机与开发板进行数据互传。