源:Qt下libusb-win32的使用(一)打印设备描述符

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

#include "testlibusb.h"

#define MY_VID 0x5345
#define MY_PID 0x1234 USB::USB()
{
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);
usb_close(udev); //my_usb_get_device_list();
resize(, );
} usb_dev_handle *USB::open_dev(void)
{
struct usb_bus *bus;
//struct usb_device *dev; 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 ;
} 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设备是Tiny6410开发板。运行效果如下:

下一步就是上位机与开发板进行数据互传。

源: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的使用(转)的更多相关文章

  1. 【转】Qt下使用glut库

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

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

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

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

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

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

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

  5. delphi中formatFloat代码初探(在qt下实现floatformat的函数)

    由于项目需要,需要在qt下实现floatformat的函数.之前写过一个,但是写得不好.决定重新写一个,参考delphi xe2下的实现.把xe2下的相关代码都看了一遍,xe2的代码思路在这里贴出来. ...

  6. QT下资源使用和资源占用…(可以动态加载资源文件,这样不占内存)

    原文地址:关于QT下资源使用和资源占用内存过多的问题作者:技术成就梦想     最近研究了一下如何从外部动态调用图片的问题,从而研究了图片资源的使用方法.网上最常见的帖子是这个,感觉总结的还不错. h ...

  7. windows下实现win32俄罗斯方块练手,编程的几点心得

    编程珠玑2阅读笔记: 1.使用c语言性能监视器,完成对代码的调优工作 2.关联数组:  拓扑排序算法,可以用于当存在遮挡的时候决定三维场景的绘制顺序. 3.小型算法中的测试与调试工具 脚手架程序:&l ...

  8. 关于QT下资源使用和资源占用…

    原文地址:关于QT下资源使用和资源占用内存过多的问题作者:技术成就梦想     最近研究了一下如何从外部动态调用图片的问题,从而研究了图片资源的使用方法.网上最常见的帖子是这个,感觉总结的还不错. h ...

  9. Qt下QString转char*

    Qt下面,字符串都用QString,确实给开发者提供了方便,想想VC里面定义的各种变量类型,而且函数参数类型五花八门,经常需要今年新那个类型转换 Qt再使用第三方开源库时,由于库的类型基本上都是标准的 ...

随机推荐

  1. toad执行存储过程

    选中存储过程,右键执行,根据存储过程中的sql语句,输入参数, 执行结果的out值点击connection output,鼠标放在statement标签,就会显示下边的out值. 在db2cmd下执行 ...

  2. 灵感闪现 篇 (一) 2d场景 3d 效果

    中途打断一下 ,框架文档的 更新. 另开一篇主题为 灵感闪现的 板块. 在工作生活中,总有发现新事物或新东西 而让自己突然 灵感闪现的时候,那么这个时候,我必须要抓住,并尽快把 这份灵感实现下来. 之 ...

  3. list遍历

     一.对List的遍历有三种方式            List<String>    list    =    new    ArrayList<String>();    ...

  4. stray '/241' in program 错误

    意思是c/c++中的编译错误. 该错误是指源程序中有非法字符,需要去掉非法字符.一般是由于从别的地方粘贴过来造成的. 方法:1.把所粘的文字放到记事本里就行了 2.把出错行的空格删掉重新打一下试试.

  5. AngularJS 基础用法

    判断语句: <li ng-repeat=”person in persons”> <span ng-switch on=”person.sex”> <span ng-sw ...

  6. ViewHolder最简洁的写法

    通用viewHolder工具类: public class ViewHolder { // I added a generic return type to reduce the casting no ...

  7. HDU 2609 How many

    最小表示法+Map或者字典树,最小表示法找了个模板,还没学习呢... #include<cstdio> #include<cstring> #include<cmath& ...

  8. php获取Linux网卡信息

    $data = exec("/sbin/ifconfig"); var_dump($data); 注意:有时候这种方式获取不到,应该是权限问题 在/var/rootP文件中添加ro ...

  9. android 以不规则图片为边框切割另外图片

    转自:http://blog.sina.com.cn/s/blog_474928c90101dkvf.html 最近工作上遇到了一个将一个图片按照相框进行裁剪的问题,花了一个下午研究了下,在此整理一下 ...

  10. backtrack种子

    下载链接:(种子文件) BT5R3-GNOME-64.torrent (md5: 8cd98b693ce542b671edecaed48ab06d) BT5R3-GNOME-32.torrent (m ...