#include <usb.h>
#include <stdio.h> #define VERSION "0.1.0"
#define VENDOR_ID 0x0925
#define PRODUCT_ID 0x7001
#define INTERFACE 0
const static int reqIntLen=;
const static int endpoint_Int_in=0x81; /* endpoint 0x81 address for IN */
const static int endpoint_Int_out=0x01; /* endpoint 1 address for OUT */ const static int timeout=; /* timeout in ms */ void bad(const char *why) {
fprintf(stderr,"Fatal error> %s\n",why);
exit();
} usb_dev_handle *find_lvr_hid(); usb_dev_handle* setup_libusb_access() {
usb_dev_handle *lvr_hid;
int retval;
char dname[] = {};
usb_set_debug();
usb_init();
usb_find_busses();
usb_find_devices(); if(!(lvr_hid = find_lvr_hid())) {
printf("Couldn't find the USB device, Exiting\n");
return NULL;
} #ifdef LINUX
retval = usb_get_driver_np(lvr_hid, , dname, );
if (!retval)
usb_detach_kernel_driver_np(lvr_hid, ); #endif retval=usb_set_configuration(lvr_hid, );
if ( retval < ) {
printf("Could not set configuration 1 : %d\n", retval);
return NULL;
}
retval = retval=usb_claim_interface(lvr_hid, INTERFACE);
if ( retval < ) {
printf("Could not claim interface: %d\n", retval);
return NULL;
} return lvr_hid;
} usb_dev_handle *find_lvr_hid()
{
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 == VENDOR_ID &&
dev->descriptor.idProduct == PRODUCT_ID ) {
usb_dev_handle *handle;
printf("lvr_hid with Vendor Id: %x and Product Id: %x found.\n", VENDOR_ID, PRODUCT_ID);
if (!(handle = usb_open(dev))) {
printf("Could not open USB device\n");
return NULL;
} return handle;
} }
} return NULL;
} /*
void test_control_transfer(usb_dev_handle *dev)
{
// usb_set_altinterface(dev, 0);
usb_release_interface(dev, 0);
}
*/
void test_interrupt_transfer(usb_dev_handle *dev)
{
int r,i;
char answer[reqIntLen];
char question[reqIntLen];
for (i=;i<reqIntLen; i++) question[i]=i;
r = usb_interrupt_write(dev, endpoint_Int_out, question, reqIntLen, timeout);
if( r < )
{
perror("USB interrupt write"); bad("USB write failed");
}
r = usb_interrupt_read(dev, endpoint_Int_in, answer, reqIntLen, timeout);
if( r != reqIntLen )
{
perror("USB interrupt read"); bad("USB read failed");
}
for (i=;i<reqIntLen; i++) printf("%i, %i, \n",question[i],answer[i]);
// usb_set_altinterface(dev, 0);
usb_release_interface(dev, );
} int main( int argc, char **argv)
{
usb_dev_handle *lvr_hid;
if ((lvr_hid = setup_libusb_access()) == NULL) {
exit(-);
}
// test_control_transfer(lvr_hid); //not implemented yet
test_interrupt_transfer(lvr_hid);
usb_close(lvr_hid); return ;
}

libusb 示例的更多相关文章

  1. libusb示例

    #include <stdio.h> #include <libusb-1.0/libusb.h> #include <stdint.h> #include < ...

  2. 基于libUSB的USB设备固件更新程序(下载数据)(转)

    源:基于libUSB的USB设备固件更新程序(下载数据) 本文紧接上一篇日志:基于libUSB-Win32的USB设备固件更新程序(前言),相关背景以及起因等,此处不再赘述,如感兴趣请移步. libU ...

  3. libusb: android上集成libusb库

    1. 下载libusb库. 可以到libusb库的官网(https://libusb.info/)或者是其官方的github仓库(https://github.com/libusb/libusb/re ...

  4. Ubuntu15下Qt+libusb开发

    下载和安装libusb-1.0 在Ubuntu15中可以从软件仓库安装libusb,当前的libusb版本为1.0.可以使用如下命令安装libusb的全部内容. $sudo apt-get insta ...

  5. 树莓派Zero 2 W(ubuntu-22.04)通过.NET6和libusb操作USB读写

    有这个想法的初衷 喜欢电子和DIY硬件的朋友对稚晖君应该都不陌生,他定期都会分享一些自己做的好玩的硬件,他之前做了一个ElectronBot桌面机器人我就很感兴趣,所以就自己也做了一个. 起初我只是自 ...

  6. Swift3.0服务端开发(一) 完整示例概述及Perfect环境搭建与配置(服务端+iOS端)

    本篇博客算是一个开头,接下来会持续更新使用Swift3.0开发服务端相关的博客.当然,我们使用目前使用Swift开发服务端较为成熟的框架Perfect来实现.Perfect框架是加拿大一个创业团队开发 ...

  7. .NET跨平台之旅:将示例站点升级至 ASP.NET Core 1.1

    微软今天在 Connect(); // 2016 上发布了 .NET Core 1.1 ,ASP.NET Core 1.1 以及 Entity Framework Core 1.1.紧跟这次发布,我们 ...

  8. 通过Jexus 部署 dotnetcore版本MusicStore 示例程序

    ASPNET Music Store application 是一个展示最新的.NET 平台(包括.NET Core/Mono等)上使用MVC 和Entity Framework的示例程序,本文将展示 ...

  9. WCF学习之旅—第三个示例之四(三十)

           上接WCF学习之旅—第三个示例之一(二十七)               WCF学习之旅—第三个示例之二(二十八)              WCF学习之旅—第三个示例之三(二十九)   ...

随机推荐

  1. linux下磁盘分区、格式化、挂载

    (1)fdisk /dev/sdb进行分区 (2)选择n表示添加一个分区,选择d表示删除一个分区.可通过m获取帮助信息 (3)选择p表示主分区,然后输入分区大小 (4)分区完成后,可通过fdisk - ...

  2. Android面试收集录 蓝牙与WiFi

    1.打开手机中的蓝牙功能有哪些方法? 法1:使用Intent  ==>new Intent(BluetoothAdaper.ACTION_REQUEST_ENABLE); startActivi ...

  3. P1107 最大整数

    P1107 最大整数 题目描述 设有n个正整数 (n<=20), 将它们连接成一排, 组成一个最大的多位整数. 例如: n=3时, 3个整数13, 312, 343连接成的最大整数为: 3433 ...

  4. HBase全网最佳学习资料汇总

    HBase全网最佳学习资料汇总 摘要: HBase这几年在国内使用的越来越广泛,在一定规模的企业中几乎是必备存储引擎,互联网企业阿里巴巴.百度.腾讯.京东.小米都有数千台的HBase集群,中国电信的话 ...

  5. 26、js阶段性复习

    1.一元运算符 Operator + 可用于将变量转换为数字: <!DOCTYPE html> <html> <body> <p> typeof 操作符 ...

  6. QC的使用学习(一)

    今天学习的时间很少,就利用睡前的一点时间来学习一下刚安装好的QC. 1.后台站点管理.主要是对八大选项的了解: site project:顾名思义,就站点项目管理,管理域和项目. site user: ...

  7. 梳理 Opengl ES 3.0 (五)shader运行原理

    先来看看一张图 shader都是在运行时编译和执行的,每个shader都有一个main函数作为它的入口. vertex shader的功能有两个:一个是计算顶点坐标变换,另一个就是为片元shader计 ...

  8. Leetcode 680.验证回文字符串

    验证回文字符串 给定一个非空字符串 s,最多删除一个字符.判断是否能成为回文字符串. 示例 1: 输入: "aba" 输出: True 示例 2: 输入: "abca&q ...

  9. LeetCode 410——分割数组的最大值

    1. 题目 2. 解答 此题目为 今日头条 2018 AI Camp 5 月 26 日在线笔试编程题第二道--最小分割分数. class Solution { public: // 若分割数组的最大值 ...

  10. 条件随机场CRF

    条件随机场(CRF)是给定一组输入随机变量X的条件下另一组输出随机变量Y的条件概率分布模型,其特点是假设输出随机变量构成马尔科夫随机场.实际上是定义在时序数据上的对数线性模型.条件随机场属于判别模型. ...