• QT Creater与libusb使用


    新建一个C项目,然后修改.pro文件,添加LIBS一行

    TEMPLATE = app
    CONFIG += console
    CONFIG -= app_bundle
    CONFIG -= qt
    
    LIBS += "D:Projectsqtusbtestlibusb.a"
    
    SOURCES += main.c

    源码:

    #include <stdio.h>
    #include "lusb0_usb.h"
    
    #define MY_VID 0x04f3
    #define MY_PID 0x0103
    #define m_dev_DATA_SIZE 8       /* Number of bytes to get at once */
    
    usb_dev_handle * my_usb_get_device_list(void)
    {
        char str[64];
        struct usb_bus *bus;
        struct usb_device *dev;
        usb_dev_handle *udh;
    
        for (bus = usb_get_busses(); bus; bus = bus->next) {
            for (dev = bus->devices; dev; dev = dev->next) {
                printf("VID:PID = %x:%x
    ", dev->descriptor.idVendor, dev->descriptor.idProduct);
                if ((dev->descriptor.idVendor == MY_VID) && (dev->descriptor.idProduct == MY_PID))
                {
                    udh = usb_open(dev);
                    printf("dev opened: %s
    ", dev->filename);
                    printf("Device Protocol : %d
    ",dev->descriptor.bDeviceProtocol);
                    printf("Report Length : %d
    ",dev->descriptor.bLength);
                    printf("Decriptor Type : %d
    ",dev->descriptor.bDescriptorType);
                    printf("End Points : %d
    ",dev->config->interface->altsetting->bNumEndpoints);
                    printf("Interface Class : %d
    ",dev->config->interface->altsetting->bInterfaceClass);
                    printf("Protocol : %d
    ",dev->config->interface->altsetting->bInterfaceProtocol);
                    printf("Interface Number: %d
    ",dev->config->interface->altsetting->bInterfaceNumber);
                    printf("Device Filename : %s
    ",dev->filename);
    
                    usb_get_string_simple(udh, dev->descriptor.iManufacturer, str, sizeof(str));
                    printf("Device Manfucaturer : %s
    ", str);
                    usb_get_string_simple(udh, dev->descriptor.iProduct, str, sizeof(str));
                    printf("Product Name : %s
    ", str);
                    usb_get_string_simple(udh, dev->descriptor.iSerialNumber, str, sizeof(str));
                    printf("Device Serial Number: %s
    ", str);
                    printf("End point addresses : 0x%x
    ",dev->config->interface->altsetting->endpoint->bEndpointAddress);
    
    
                    return udh;
                }
            }
        }
    
        return NULL;
    }
    
    int main(void)
    {
        int ret, i;
        char data[m_dev_DATA_SIZE] = {0};
        char data_zero[m_dev_DATA_SIZE] = {0};
        usb_dev_handle *udh;
        //char ShiftMouseScrollDown1[] = {0x02,0xf3,0xff};
    
        memset(data_zero, 0, m_dev_DATA_SIZE);
    
        usb_init(); /* initialize the library */
        printf("buses:%d
    ", usb_find_busses()); /* find all busses */
        printf("devices:%d
    ", usb_find_devices()); /* find all connected devices */
    
        udh = my_usb_get_device_list();
        if (!udh) {
            printf("usb dev handle is NULL
    ");
        }
    
        if(usb_set_configuration(udh, 1) < 0)
        {
            printf("Could not set configuration
    ");
            usb_close(udh);
            return 1;
        }
    
        if(usb_claim_interface(udh, 0) < 0) //claim_interface 0指向第一个设备
        {
            printf("Could not claim interface
    ");
            usb_close(udh);
            return 1;
        }
    
        while(1)
        {
            ret = usb_interrupt_read(udh, 0x81, data, m_dev_DATA_SIZE, 0);
            if (!memcmp(data, data_zero, m_dev_DATA_SIZE)) {
                continue;
            }
            printf("%02X ", (unsigned char)(data[2]));
            //printf("%c", (unsigned char)(data[2])-4+'a');
        }
    
        usb_release_interface(udh, 0);
        usb_close(udh);
        printf("m_dev closed
    ");
    
    
    
        printf("END!
    ");
    
        return 0;
    }

    需要将libusb0.dll和libusb0.sys添加到Sytem32目录下面去,否则运行就列机。

    这时上面的程序还不能打印出USB设备来,是因为还需要安装USB的驱动。

    运行下载文件夹中的bin/inf-wizard应用程序,选中你的USB设备然后一步一步地next,最后install,就可以了。

    再次运行上面的源码编译得的程序,就可以看到真正的枚举输出了。

    输入abcdefg时的反应:

    另外参考:http://www.embed-net.com/thread-18-1-1.html

  • 相关阅读:
    http://www.kankanews.com/ICkengine/archives/18078.shtml
    c# ArrayList 的排序问题!
    MVC各种传值方式
    MVC3学习第五章 排山倒海第一变母版页,模型
    MVC3学习第三章 剑出鞘之前奏控制器,URL路由
    MVC3学习第二章 剑出鞘之看剑vs2010安装MVC3和建立你的第一个MVC3项目
    MVC3学习第四章 剑出鞘之后续MVC3的新特性之Razor视图解析
    MVC3学习第一章 掀起它的盖头来
    有关匿名函数执行与传参
    ubuntu12.04安装jdk7u79linuxi586.tar.gz
  • 原文地址:https://www.cnblogs.com/tfanalysis/p/4438022.html
Copyright © 2020-2023  润新知