libusb的简单使用

使用libusb开发库可以很方便的与usb通信。

在使用这个库之前需要做几件事情

1.libusb的安装

在ubuntu中使用sudo apt-get isntall libusb* 可以快速的安装,如果想选择性的安装则使用sudo apt-cache search libusb查看要安装的软件包,然后sudo apt-get install ...进行安装

2.确定供应商id 和 产品id,通过这两个id可以确定我们的usb设备

 使用lsusb命令查看,然后使用libusb -v查看具体信息

3 确定传输方式、输入端点、输出端点

命令同2

我的usb是bulk传输方式、输入端点0x86,输出端点0x02

4.在项目中添加添加库 -lusb

  不添加的话会出现找不到函数的问题,如果提示找不到usb.h文件很可能是你没安装好libusb库

5.软件完成之后要使用sudo运行程序,不然usb_bulk_write之类的函数会调用失败。

6.附上官网地址和帮助文档

官网:http://www.libusb.org/

帮助文档:http://libusb.sourceforge.net/doc/

下面看一下bulk的使用例子:

usbhelper.h

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <usb.h>
 4 #include <QtCore/QObject>
 5 #include <QtCore/QString>
 6 #include <QtCore/QStringList>
 7 class UsbHelper : public QObject
 8 {
 9     Q_OBJECT
10 public:
11     UsbHelper();
12     ~UsbHelper();
13
14     bool InitUsb();
15
16     bool WriteData(const QString& pBuffer);
17
18     bool ReadData(QString &pBuffer);
19
20     bool WaitResponse(int pTimeout, QStringList &pResponseList);
21
22     void CloseUsb();
23
24     bool SendCommand(const QString &pCommand, QStringList &pResponseList);
25 private:
26     usb_dev_handle *mUsbHandle;
27     struct usb_device        *mUsbDev;
28 };

usbhelper.cpp

#include "usbhelper.h"
#include <QtCore/QThread>
#include <QtCore/QByteArray>
#include <QtCore/QTime>

#define IDVENDOR     0x10EE
#define IDPRODUCT     0xAA55

#define ENDPOINT_OUT    0x02
#define ENDPOINT_IN        0x86

UsbHelper::UsbHelper() :
    mUsbHandle(NULL),
    mUsbDev(NULL)
{
}

UsbHelper::~UsbHelper()
{
    CloseUsb();
}

bool UsbHelper::InitUsb()
{
    usb_init();
    usb_find_busses();
    usb_find_devices();

    struct usb_bus *pbus = NULL;
    struct usb_device *pdev = NULL;

     usb_set_debug(255);

    for(pbus=usb_busses;pbus!=NULL;pbus=pbus->next){
        for(pdev=pbus->devices;pdev!=NULL;pdev=pdev->next){
            struct usb_device_descriptor *desc=&pdev->descriptor;
            if(desc->idVendor==0x10EE && desc->idProduct==0xAA55){
                mUsbDev = pdev;
            }
        }
    }
    if(mUsbDev)
    {
        mUsbHandle = usb_open(mUsbDev);
        if(mUsbHandle)
        {
            usb_claim_interface(mUsbHandle,0);
            return true;
        }

    }
    return false;
}

void UsbHelper::CloseUsb()
{
    if(mUsbHandle)
    {
        usb_release_interface(mUsbHandle,0);
        usb_close(mUsbHandle);
    }
}

bool UsbHelper::ReadData(QString &pBuffer)
{
    return true;
}

bool UsbHelper::WriteData(const QString &pBuffer)
{

    char buff[1024];
    memset(buff, 0, sizeof(buff));
    int ret = usb_bulk_read(mUsbHandle, ENDPOINT_IN, buff, sizeof(buff), 100);
/*    if(ret < 0)
    {
        qDebug("usb_bulk_read error");
        return false;
    }
*/
    qDebug("pre read:");

    char buffer[1024];
    QString command = pBuffer;
    while(command.length() > 0)
    {
        QString sendcommand = command.left(sizeof(buffer));
        command.remove(0, sizeof(buffer));
        int sendlen = sendcommand.length();
        QByteArray ba = sendcommand.toLatin1();
        memcpy(buffer, ba.data(), sendcommand.length());
        ret = usb_bulk_write(mUsbHandle, ENDPOINT_OUT, buffer, sendlen, 500);
        if(ret < 0)
        {
            qDebug("usb_bulk_write failed");
            return false;
        }
        else
        {
            qDebug("write %s success", buffer);
        }
//        QThread::sleep(1);
    }
    return true;
}

bool UsbHelper::WaitResponse(int pTimeout, QStringList &pResponseList)
{

    qDebug("Enter WaitResponse");
    pResponseList.clear();
    QString line;
    QTime time;
    time.start();
//    while (time.elapsed() < pTimeout) {
    char readedbuffer[1000];
    int readedSize = 1000;
    int ret = usb_bulk_read(mUsbHandle, ENDPOINT_IN, readedbuffer, sizeof(readedbuffer), 300);
    if (ret < 0) {
        qDebug("WaitResponse sub_bulk_read error");
        return false;
    }
    for (int i=0; i<readedSize; i++)
     {
        quint8 byte = readedbuffer[i];
        if (byte == 0x0D) {
            if (! line.isEmpty()) {
                pResponseList.append(line);
                line = "";
            }
        }
        else {
            line += byte;
        }
    }
    if (! line.isEmpty()) {
        pResponseList.append(line);
    }

    //´òÓ¡ÊÕµ½µÄÐÅÏ¢¡£
    foreach (QString command, pResponseList) {
        qDebug("**> %s", qPrintable(command));
    }
    return pResponseList.count() > 0;
}

bool UsbHelper::SendCommand(const QString &pCommand, QStringList &pResponseList)
{
    if(! WriteData(pCommand)) {
        qDebug("Write Data failed");
        return false;
    }
    if(! WaitResponse(100, pResponseList)) {
        qDebug("WaitResponse failed");
        return false;
    }
    if (pResponseList[0] != pCommand) {
        return false;
    }
    return true;
}

main.cpp

#include <stdio.h>
#include <stdlib.h>
#include <usb.h>
#include <QtCore/QCoreApplication>
#include "usbhelper.h"

int main(int argc, char* argv[])
{
    QCoreApplication app(argc, argv);

    UsbHelper *helper = new UsbHelper;

    if(! helper->InitUsb())
    {
        qDebug("InitUsb failed");
        return -1;
    }
    QStringList strList;
    if(! helper->SendCommand("W", strList))
    {
        qDebug("Send W failed");
        return -1;
    }

    if(! helper->SendCommand("X", strList))
    {
        qDebug("Send X failed");
        return -1;
    }

    if(! helper->SendCommand("Y", strList))
    {
        qDebug("Send Y failed");
        return -1;
    }
    QString command = "C1,00,01,FF,FF,00,00,20,02,20,02,00,04,08,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00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F,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,00,FD,02,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,02,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,02,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,02,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00";
    if(! helper->WriteData(command)) {
        qDebug("Write Data failed");
        return false;
    }
    if(! helper->WaitResponse(100, strList)) {
        qDebug("WaitResponse failed");
        return false;
    }
    if (strList[0] != "OK") {
        qDebug("send brigntness error");
        return false;
    }
    else
    {
        qDebug("send brigntness success");
    }
    app.exec();
    return 0;
}

usb.pro

TEMPLATE        = app
LANGUAGE        = c++

QT += gui core

LIBS            += -lusb -ldl

HEADERS =   pvcmbasethread.h             pvdvdbstardevicecommunicatethread.h

SOURCES =     main.cpp             pvcmbasethread.cpp             pvdvdbstardevicecommunicatethread.cpp

unix {
    MOC_DIR = .moc
    OBJECTS_DIR = .obj
}

  

时间: 2024-10-05 23:50:14

libusb的简单使用的相关文章

Linux libusb 安装及简单使用

Linux libusb 安装及简单使用 一.参考文档: 1. libusb1 fails do_configure task with “udev support requested but libudev not installed” http://stackoverflow.com/questions/23712859/libusb1-fails-do-configure-task-with-udev-support-requested-but-libudev-not-ins 2. lib

libusb(.NET)开源项目使用小结

更多细节请参考官方帮助文档 1,修改设备类型为自己的标识 InfWizard项目里,改掉资源文件LibUsb-Win32-LUDN.Driver.Resources. 原来的三处libusb-win32 devices改为自己想显示的名词,由于是资源文件,故字符数相同是最简单的改法. 然后编译好这个驱动配置工具,发布给客户用,测试通过. 设备类型下的设备名和制造商,如果是中文,则会乱码, 可能是编码问题,待以后需要的时候解决.目前设置为英文+拼音保护自己版权即可. 使用此驱动配置工具可以强制把H

在 QT 中使用 libusb 检测 MAC 上的 USB 设备

最近在用 QT 做一个 MAC 上的 Kindle 批注管理软件,遇到的第一个问题就是检测 MAC 上连接的 USB 设备的状态.如果是在 Cocoa 进行开发,会有对应的系统 API 可供使用,但是由于我是在 QT 平台进行的开发,所以无形中加大了一点难度.就在这时,我发现了一个库:libusb libusb 介绍 libusb 设计了一系列的外部API 为应用程序所调用,通过这些API应用程序可以操作硬件,从libusb的源代码可以看出,这些API 调用了内核的底层接口,和kernel dr

libusb 开发者指南-牛胜超(转)

源:libusb 开发者指南 libusb Developers Guidelibusb 开发者指南 原作者:Johannes Erdfelt翻译者:牛胜超 Table of Contents目录 Preface序言 I. Introduction            引言   1. Overview      概述   2. Current OS support      流行的操作系统支持 II. API                         应用程序接口   3. Device

libusb简介

概述 libusb是一个C库,它提供了通用的访问USB设备. 它的目的是供开发人员使用方便的生产与USB通信硬件的应用程序. 可移植的: 使用一个跨平台API,它提供了访问USB设备在Linux上,OS X Windows,Android,OpenBSD等等. 用户模式: 不需要特权或高程应用程序与设备进行通信. version-agnostic: 所有版本的USB协议,从1.0到3.0(最新),都受支持. 支持哪些平台? Linux.mac OS X.Windows Windows CE,An

libusb开发者指南(转)

源:libusb开发者指南 译者: gashero 作者: Johannes Erdfelt 日期: 2010-04-17 地址: http://libusb.sourceforge.net/doc/ 目录 1   介绍 1.1   概览 1.2   当前OS支持 2   API 2.1   设备与接口 2.2   超时 2.3   数据类型 2.4   同步 2.5   返回值 3   函数 3.1   核心函数 3.2   设备操作 3.3   控制传输 3.4   块传输 3.5   中断

[转]android下编译libusb和libhackrf

libhackrf是上层应用程序操作hackrf的入口库,是软件操作硬件的中间件,在android上使用hackrf当然也需要使用 libhackrf.操作系统与hackrf之间的通信是通过USB2.0完成的,libhackrf使用了libusb进行USB通信.android 并没有自带libusb,所以我们需要自行编译. 整体分为两个部分,首先是libusb的编译,然后是libhackrf的编译. libusb libusb的编译十分简单,因为其没有过多的依赖,而麻烦的是android对usb

Firefox OS简单介绍

Firefox OS系统架构框图 一些Firefox相关的术语简单介绍: B2G Boot to Gecko 的简称. Boot to Gecko Firefox OS 操作系统的project代号. 由于在该项目拥有官方名称之前B2G已经使用了非常久的原因.它经经常使用于指代 Firefox OS. Firefox OS FIrefox OS 基本上是指 Mozilla及合作伙伴应用在 B2G上的品牌和服务支持. 终于将创建一个公布的产品. Gaia Firefox OS 平台的用户接口层.

C# Ping 简单使用

编程过程中,有时候需要判断主机是否在线,最简单的方法就是使用Windows的Ping命令看看能否ping通.看到网上很多文章,说用C#去调用windows的ping.exe,然后解析返回的字符串.我觉得这种方式太麻烦了,就做一下简单判断,不想弄那么麻烦. 查了一下,C#专门提供了一个Ping类,与Windows下的ping命令类似: 命令空间: System.Net.NetworkInformation; 使用方法: bool online = false; //是否在线 Ping ping =