当前位置: 首页 > 工具软件 > libusb > 使用案例 >

libusb ubuntu

干茂才
2023-12-01

libusb
在官网http://libusb.info/可以下载到,如下图:

参考网址

gcc -o testlibusb testlibusb.c -I/usr/local/include/libusb-1.0 -L/usr/local/lib/ -lusb-1.0 -Wl,-rpath=/usr/local/lib
Linux libusb USB开发(二)—— libusb安装与调试
https://www.pianshen.com/article/4493408433/
https://blog.csdn.net/jiguangfan/article/details/86492698
libusb linux 异步传输,libusb异步接收数据丢包问题
https://blog.csdn.net/weixin_36255898/article/details/116949816
libusb示例
https://www.cnblogs.com/nightnine/p/7355290.html

libusb_handle_events

void monitor_usb_hotplug(void* para) {
int rc =0;

while (1) {
		rc = libusb_handle_events (NULL);
		if (rc < 0)
			bio_print_error(_("libusb_handle_events failed: %s\n"), libusb_error_name(rc));
	}

}

有些帮助了解与libusb-1.0和ctypes的异步USB操作
https://www.pianshen.com/question/7870184286/

二、libusb的安装
1、环境 Ubuntu 16.04

2、安装主要分3个步骤,如下:

    a:执行./configure来配置安装包。

    b:执行make编译该安装包。

    c:执行make install安装

3、具体操作流程:

http://www.javashuo.com/article/p-nalupjdf-vs.html

configure: error: udev support requested but libudev header not installed

如何 直接 使用 libusb库文件呢

makefile如何写呢?

./usr/include/libusb-1.0/libusb.h
sudo apt-get install libudev-dev

编译方法

三、libusb的测试
libusb源码目录下有example文件夹,里面有libusb提供的测试程序listdev.c源文件 (列出usb设备)

#include <stdio.h>
#include <sys/types.h>
#include <libusb.h>
 
static void print_devs(libusb_device **devs)
{
	libusb_device *dev;
	int i = 0;
 
	while ((dev = devs[i++]) != NULL) {
		struct libusb_device_descriptor desc;
		int r = libusb_get_device_descriptor(dev, &desc);
		if (r < 0) {
			fprintf(stderr, "failed to get device descriptor");
			return;
		}
 
		printf("%04x:%04x (bus %d, device %d)\n",
			desc.idVendor, desc.idProduct,
			libusb_get_bus_number(dev), libusb_get_device_address(dev));
	}
}
 
int main(void)
{
	libusb_device **devs;
	int r;
	ssize_t cnt;
 
	r = libusb_init(NULL);
	if (r < 0)
		return r;
 
	cnt = libusb_get_device_list(NULL, &devs);
	if (cnt < 0)
		return (int) cnt;
 
	print_devs(devs);
	libusb_free_device_list(devs, 1);
 
	libusb_exit(NULL);
	return 0;
}

编译的时候我们可以用自带的makefile生成可执行文件,也可以用如下操作进行:

gcc -I/usr/local/include/libusb-1.0 listdevs.c -L/usr/local/lib -lusb-1.0

(-I包含头文件 ;-L链接库)

运行结果:
gcc -I/usr/local/include/libusb-1.0 listdevs.c -L/usr/local/lib -lusb-1.0

export LD_LIBRARY_PATH=/usr/local/lib

gcc -I/usr/include/libusb-1.0/ listdevs.c -L/usr/lib -lusb-1.0

https://github.com/crazybaoli/libusb-test

11


./opt/crossover-depend/lib/i386-linux-gnu/libusb-1.0.so.0.2.0
./opt/crossover-depend/lib/i386-linux-gnu/libusb-1.0.so.0
./opt/crossover-depend/usr/share/doc/libusb-1.0-0
./usr/lib/x86_64-linux-gnu/libusb-1.0.so.0.2.0
./usr/lib/x86_64-linux-gnu/libusb-1.0.a
./usr/lib/x86_64-linux-gnu/libusbmuxd.so.6
./usr/lib/x86_64-linux-gnu/pkgconfig/libusb-1.0.pc
./usr/lib/x86_64-linux-gnu/libusb-1.0.so.0
./usr/lib/x86_64-linux-gnu/libusbmuxd.so.6.0.0
./usr/lib/x86_64-linux-gnu/libusb-1.0.so
./usr/include/libusb-1.0
./usr/include/libusb-1.0/libusb.h
./usr/share/doc/libusbmuxd6
./usr/share/doc/libusb-1.0-0
./usr/share/doc/libusb-1.0-0-dev

./var/cache/apt/archives/libusb-1.0-0-dev_2%3a1.0.23-2build1_amd64.deb
./var/lib/dpkg/info/libusb-1.0-0:amd64.md5sums
./var/lib/dpkg/info/libusbmuxd6:amd64.list
./var/lib/dpkg/info/libusb-1.0-0:amd64.list
./var/lib/dpkg/info/libusb-1.0-0:amd64.triggers
./var/lib/dpkg/info/libusb-1.0-0:amd64.symbols
./var/lib/dpkg/info/libusbmuxd6:amd64.shlibs
./var/lib/dpkg/info/libusbmuxd6:amd64.triggers
./var/lib/dpkg/info/libusb-1.0-0:amd64.shlibs
./var/lib/dpkg/info/libusb-1.0-0-dev:amd64.list
./var/lib/dpkg/info/libusbmuxd6:amd64.md5sums
./var/lib/dpkg/info/libusb-1.0-0-dev:amd64.md5sums
./var/lib/dpkg/info/libusbmuxd6:amd64.symbols

通过libusb_alloc_transfer分配转移。
通过libusb_fill_bulk_transfer填写批量转帐。
通过libusb_detach_kernel_driver分离内核驱动程序。
通过libusb_claim_interface声明接口。
通过libusb_submit_transfer提交转移。
最后,通过libusb_attach_kernel_driver重新附加内核驱动程序。

usb_set_altinterface

函数定义: int usb_set_altinterface(usb_dev_handle *dev, int alternate);

和名字的意思一样,此函数设置当前设备配置的interface descriptor,参数alternate是指interface descriptor中的bAlternateSetting。返回0成功,<0失败
usb_claim_interface

函数定义: int usb_claim_interface(usb_dev_handle *dev, int interface);

注册与操作系统通信的接口,这个函数必须被调用,因为只有注册接口,才能做相应的操作。

Interface 指 bInterfaceNumber. (下面介绍的usb_release_interface 与之相对应,也是必须调用的函数)

usb_release_interface

函数定义: int usb_release_interface(usb_dev_handle *dev, int interface);

注销被usb_claim_interface函数调用后的接口,释放资源,和usb_claim_interface对应使用。

2.4 批传输接口

usb_bulk_write

函数定义: int usb_bulk_write(usb_dev_handle *dev, int ep, char *bytes, int size, int timeout);

usb_interrupt_read

函数定义: int usb_interrupt_read(usb_dev_handle *dev, int ep, char *bytes, int size, int timeout);

libusb阻塞

dpfp.c

 * libusb example program to manipulate U.are.U 4000B fingerprint scanner.
 * Copyright © 2007 Daniel Drake <dsd@gentoo.org>
 * Copyright © 2016 Nathan Hjelm <hjelmn@mac.com>
 * Copyright © 2020 Chris Dickens <christopher.a.dickens@gmail.com>
 *
 * Basic image capture program only, does not consider the powerup quirks or
 * the fact that image encryption may be enabled. Not expected to work
 * flawlessly all of the time.
 *
 类似资料:

相关阅读

相关文章

相关问答