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学习之旅—第三个示例之三(二十九) ...
随机推荐
- css常用样式属性详细介绍
对于初学css的来说,肯定会觉得这么多样式不好记,而且记住了也容易忘,其实刚开始我们不用去记这么多的样式,确实是记了也会忘,刚开始只需记住一些常用的就可以了,然后在慢慢的使用过程当中接触并学习一些高级 ...
- STL——vector和list
vector和list为STL中的顺序容器,顺序容器会依次维护第一个到最后一个元素,在顺序容器上,我们主要的操作就是迭代. 头文件: #include<vector> #include&l ...
- git之解决冲突
前面几次使用git,一直对于冲突的这个问题不是很理解,感觉有些时候就会产生冲突,在此记录一下解决冲突的流程 1.git bash上面冲突显示 2.在idea上面可以看到冲突的文件 3.去解决冲突 4. ...
- 转载:Linux系统和Linux系统之间如何实现文件传输
两台Linux系统之间传输文件 听语音 | 浏览:13183 | 更新:2014-07-15 15:22 | 标签:linux 1 2 3 4 5 6 分步阅读 如何在Linux系统之间传输文件及文件 ...
- 《.NET 微服务:适用于容器化 .NET 应用的体系结构》关键结论
作为总结和要点,以下是本指南中最重要的结论.1 使用容器的好处: 基于容器的解决方案有节约成本的好处,因为容器是针对生产环境中缺少依赖而导致的部署问题提出的解决方案.容器能够显著改善devops和生产 ...
- es6中类的注意事项
class Circle { constructor(radius) { this.radius = radius; Circle.circlesMade++; }; static draw(circ ...
- Linux-Shell脚本编程-学习-4-Shell编程-操作数字-加减乘除计算
对于任何一种编程语言都很重要的特性就是操作数字的能力,遗憾的是,对于shell脚本来说,这个过程比较麻烦,在shell脚本中有两种途径来进行数学运算操作. 1.expr 最开始的时候,shell提供了 ...
- [leetcode-640-Solve the Equation]
Solve a given equation and return the value of x in the form of string "x=#value". The equ ...
- edgeboxes proposal 和dpm 连接
经过多天阅读文献和代码,我对edgeboxes 和 dpm的有了一定了解. 现在决定把它们简单地连接起来 也就是edgeboxes proposal 推荐窗口 然后 dpm去判断 但按照我理解的DPM ...
- PokeCats开发者日志(三)
现在是PokeCats游戏开发的第四天的晚上,明天要过周末了,所以提前写一下开发者日志吧! day4 day4主要是优化界面和增加游戏可玩性. (1)感觉只有三只喵喵的话,玩家只需要无脑点 ...