libusb 示例
#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 示例的更多相关文章
- libusb示例
#include <stdio.h> #include <libusb-1.0/libusb.h> #include <stdint.h> #include < ...
- 基于libUSB的USB设备固件更新程序(下载数据)(转)
源:基于libUSB的USB设备固件更新程序(下载数据) 本文紧接上一篇日志:基于libUSB-Win32的USB设备固件更新程序(前言),相关背景以及起因等,此处不再赘述,如感兴趣请移步. libU ...
- libusb: android上集成libusb库
1. 下载libusb库. 可以到libusb库的官网(https://libusb.info/)或者是其官方的github仓库(https://github.com/libusb/libusb/re ...
- Ubuntu15下Qt+libusb开发
下载和安装libusb-1.0 在Ubuntu15中可以从软件仓库安装libusb,当前的libusb版本为1.0.可以使用如下命令安装libusb的全部内容. $sudo apt-get insta ...
- 树莓派Zero 2 W(ubuntu-22.04)通过.NET6和libusb操作USB读写
有这个想法的初衷 喜欢电子和DIY硬件的朋友对稚晖君应该都不陌生,他定期都会分享一些自己做的好玩的硬件,他之前做了一个ElectronBot桌面机器人我就很感兴趣,所以就自己也做了一个. 起初我只是自 ...
- Swift3.0服务端开发(一) 完整示例概述及Perfect环境搭建与配置(服务端+iOS端)
本篇博客算是一个开头,接下来会持续更新使用Swift3.0开发服务端相关的博客.当然,我们使用目前使用Swift开发服务端较为成熟的框架Perfect来实现.Perfect框架是加拿大一个创业团队开发 ...
- .NET跨平台之旅:将示例站点升级至 ASP.NET Core 1.1
微软今天在 Connect(); // 2016 上发布了 .NET Core 1.1 ,ASP.NET Core 1.1 以及 Entity Framework Core 1.1.紧跟这次发布,我们 ...
- 通过Jexus 部署 dotnetcore版本MusicStore 示例程序
ASPNET Music Store application 是一个展示最新的.NET 平台(包括.NET Core/Mono等)上使用MVC 和Entity Framework的示例程序,本文将展示 ...
- WCF学习之旅—第三个示例之四(三十)
上接WCF学习之旅—第三个示例之一(二十七) WCF学习之旅—第三个示例之二(二十八) WCF学习之旅—第三个示例之三(二十九) ...
随机推荐
- 数据库 MySQL part2
表记录的操作 增 1.插入一条记录 语法:insert [into] tab_name (field1,filed2,.......) values (value1,value2,.......); ...
- JavaScript---设计模式简介
概念 设计模式(Design pattern)是一套被反复使用.思想成熟.经过分类和无数次实战设计经验的总结的.使用设计模式是为了让系统代码可重用.可扩展.可解耦.更容易被人理解且能保证代码的可靠性. ...
- idea离线安装lombock插件
技术交流群:233513714 安装过程 1.首先找到插件包 插件包可以在两个地方下载,分别是IDEA的官方插件仓库和GitHub里lombok-intellij-plugin仓库中的release包 ...
- 【APUE】Chapter5 Standard I/O Library
5.1 Introduction 这章介绍的standard I/O都是ISOC标准的.用这些standard I/O可以不用考虑一些buffer allocation.I/O optimal-siz ...
- Qt 在Label上面绘制罗盘
自己写的一个小小的电子罗盘的一个小程序,不过是项目的一部分,只可以贴绘制部分代码 效果如下图 首先开始自己写的时候,虽然知道Qt 的坐标系是从左上角开始的,所以,使用了算法,在绘制后,在移动回来,但是 ...
- 单链表无head各种操作及操作实验
#encoding=utf-8 class ListNode: def __init__(self,x): self.val=x; self.next=None; #链表逆序 def revers ...
- python中logging的常用方法
logging常用 # -*- coding:utf-8 -*- __author__ = "lgj" import os import sys import time impor ...
- 容器基础(八): 使用docker swarm部署程序
环境 基于上一节的env/server:v0.1, env/worker:v0.1镜像, 在基于debian8.2的两台机器上测试部署docker swarm. docker service部署 ➜ ...
- wangEditor编辑器中解析html图文信息问题
在JS中,有一种方法:innerHTML 属性设置或返回表格行的开始和结束标签之间的 HTML. 也就是说,我们可以利用这个属性,把字符串转换为html代码,这样就可以被解析了. 其次,我们是需要在页 ...
- 并查集——poj1703(带权并查集入门)
传送门:Find them, Catch them 题意:警察抓获N个罪犯,这些罪犯只可能属于两个团伙中的一个,现在给出M个条件(D a b表示a和b不在同一团伙),对于每一个询问(A a b)确定a ...