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学习之旅—第三个示例之三(二十九) ...
随机推荐
- 批量安装Python第三方库
1.首先在python程序的文件夹内,新建一个文本文档,名字自定义,在文档中输入需要安装的第三方库,并用英文半角逗号隔开. import os def getTxt(): txt = open(&qu ...
- C# 生成机器码
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- go学习笔记-基础类型
基础类型 布尔值 布尔值的类型为bool,值是true或false,默认为false. //示例代码 var isActive bool // 全局变量声明 var enabled, disabled ...
- R语言学习笔记(十二):零碎知识点(31-35)
31--round(),floor()和ceiling() round()四舍五入取整 floor()向下取整 ceiling()向上取整 > round(3.5) [1] 4 > flo ...
- stm32--USB(作为U盘)+FatFs的实现
一.USB功能的添加(作为U盘) 添加文件 将官方库中的Library文件夹中的所有有效文件添加到工程中,分为4个文件夹: usb class为硬件相关(Library\Class): usb dri ...
- github简单使用教程(转)
github是一个基于git的代码托管平台,付费用户可以建私人仓库,我们一般的免费用户只能使用公共仓库,也就是代码要公开.对于一般人来说公共仓库就已经足够了,而且我们也没多少代码来管理,O(∩_∩)O ...
- OpenCV入门:(七:OpenCV取随机数以及显示文字)
1.随机颜色 OpenCV中自带了取随机数的方法,使用步骤: RNG rng( 0xFFFFFFFF ); 随机数 = rng.uniform( 下限,上限 ); 2.显示文字 , , bool bo ...
- 【多校联合】(HDU6043)KazaQ's Socks
[多校联合](HDU6043)KazaQ's Socks 一条纯粹的水题,记录下只是因为自己错的太多而已. 原因在于对数据的细节的把握不佳. 原题 KazaQ's Socks Time Limit: ...
- SpriteKit游戏开发适配iPad/iPhone6/7/8/Plus及iPhoneX的尺寸及安全区域
未适配前:Ball球超过屏幕的上下方 适配后:Ball球就在屏幕的可视范围内运动了 一.那么如何适配不同的iPhone.iPhoneX及iPad的屏幕尺寸呢? 我们开发一个App的时候, 通常希望 ...
- 一款代码高亮插件 -- SyntaxHighlighter
SyntaxHighlighter 是当前用得最多的一款代码高亮插件,包括本博客也用到了该插件来显示代码,大家可以看到效果了.只不过这是针对WordPress的一款代码高亮插件,而今天我要给大家介绍的 ...