首页 > 系统相关 >Linux+libusb开发用户USB无驱通讯

Linux+libusb开发用户USB无驱通讯

时间:2023-02-01 14:12:34浏览次数:49  
标签:1.0 USB debug libusb usr Linux device local

0.libusb的介绍:参考[1]

1.环境:vmware_fedora_10(linux-2.6.x)

2.获取源代码:http://sourceforge.net/projects/libusb/ (最好选择libusb-1.0.9版本,下载次数最多,自是有它的道理)

3.解压源码tar xjvf libusb-1.0.9.tar.bz2 按照INSTALL文件给出的提示进行安装:主要分为./configure ->make -> make install

4.安装过程如下:

configure略去......(看不懂......)

/* make的动作主要是编译libusb的源代码 */

[root@zx libusb-1.0.9]# make
make all-recursive
make[1]: Entering directory `/share/libusb-1.0.9'
Making all in libusb
make[2]: Entering directory `/share/libusb-1.0.9/libusb'
CC libusb_1_0_la-core.lo
CC libusb_1_0_la-descriptor.lo
CC libusb_1_0_la-io.lo
CC libusb_1_0_la-sync.lo
CC libusb_1_0_la-linux_usbfs.lo
CC libusb_1_0_la-threads_posix.lo
CCLD libusb-1.0.la
make[2]: Leaving directory `/share/libusb-1.0.9/libusb'
Making all in doc
make[2]: Entering directory `/share/libusb-1.0.9/doc'
make[2]: Nothing to be done for `all'.
make[2]: Leaving directory `/share/libusb-1.0.9/doc'
make[2]: Entering directory `/share/libusb-1.0.9'
make[2]: Nothing to be done for `all-am'.
make[2]: Leaving directory `/share/libusb-1.0.9'
make[1]: Leaving directory `/share/libusb-1.0.9'

/* make install的动作主要是编译出libusb库并加入到系统文件夹下 */
[root@zx libusb-1.0.9]# make install
Making install in libusb
make[1]: Entering directory `/share/libusb-1.0.9/libusb'
make[2]: Entering directory `/share/libusb-1.0.9/libusb'
test -z "/usr/local/lib" || /bin/mkdir -p "/usr/local/lib"
/bin/sh ../libtool --mode=install /usr/bin/install -c libusb-1.0.la '/usr/local/lib'
libtool: install: /usr/bin/install -c .libs/libusb-1.0.so.0.1.0 /usr/local/lib/libusb-1.0.so.0.1.0
libtool: install: (cd /usr/local/lib && { ln -s -f libusb-1.0.so.0.1.0 libusb-1.0.so.0 || { rm -f libusb-1.0.so.0 && ln -s libusb-1.0.so.0.1.0 libusb-1.0.so.0; }; })
libtool: install: (cd /usr/local/lib && { ln -s -f libusb-1.0.so.0.1.0 libusb-1.0.so || { rm -f libusb-1.0.so && ln -s libusb-1.0.so.0.1.0 libusb-1.0.so; }; })
libtool: install: /usr/bin/install -c .libs/libusb-1.0.lai /usr/local/lib/libusb-1.0.la
libtool: install: /usr/bin/install -c .libs/libusb-1.0.a /usr/local/lib/libusb-1.0.a
libtool: install: chmod 644 /usr/local/lib/libusb-1.0.a
libtool: install: ranlib /usr/local/lib/libusb-1.0.a
libtool: finish: PATH="/usr/lib/qt-3.3/bin:/usr/kerberos/sbin:/usr/kerberos/bin:/usr/lib/ccache:/opt/EmbedSky/4.3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/sbin" ldconfig -n /usr/local/lib
----------------------------------------------------------------------
Libraries have been installed in:
/usr/local/lib /* 这里提示已经将库加入到/usr/local/lib目录,所以我们基于libusb编程的时候,需要包含这个库 */

If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the `-LLIBDIR'
flag during linking and do at least one of the following:
- add LIBDIR to the `LD_LIBRARY_PATH' environment variable
during execution
- add LIBDIR to the `LD_RUN_PATH' environment variable
during linking
- use the `-Wl,-rpath -Wl,LIBDIR' linker flag
- have your system administrator add LIBDIR to `/etc/ld.so.conf'

See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------
test -z "/usr/local/include/libusb-1.0" || /bin/mkdir -p "/usr/local/include/libusb-1.0"
/usr/bin/install -c -m 644 libusb.h '/usr/local/include/libusb-1.0'
make[2]: Leaving directory `/share/libusb-1.0.9/libusb'
make[1]: Leaving directory `/share/libusb-1.0.9/libusb'
Making install in doc
make[1]: Entering directory `/share/libusb-1.0.9/doc'
make[2]: Entering directory `/share/libusb-1.0.9/doc'
make[2]: Nothing to be done for `install-exec-am'.
make[2]: Nothing to be done for `install-data-am'.
make[2]: Leaving directory `/share/libusb-1.0.9/doc'
make[1]: Leaving directory `/share/libusb-1.0.9/doc'
make[1]: Entering directory `/share/libusb-1.0.9'
make[2]: Entering directory `/share/libusb-1.0.9'
make[2]: Nothing to be done for `install-exec-am'.
test -z "/usr/local/lib/pkgconfig" || /bin/mkdir -p "/usr/local/lib/pkgconfig"
/usr/bin/install -c -m 644 libusb-1.0.pc '/usr/local/lib/pkgconfig'
make[2]: Leaving directory `/share/libusb-1.0.9'
make[1]: Leaving directory `/share/libusb-1.0.9'

5.源码目录下有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生成可执行文件,也可以用如下操作进行:(参考[2])

[root@zx examples]# gcc -I/usr/local/include/libusb-1.0 listdevs.c -L/usr/local/lib -lusb-1.0

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

6.listdev的运行结果如下:

[root@zx examples]# gcc -I/usr/local/include/libusb-1.0 listdevs.c -L/usr/local/lib -lusb-1.0
[root@zx examples]# ./a.out
1d6b:0002 (bus 1, device 1)
1d6b:0001 (bus 2, device 1)
0e0f:0003 (bus 2, device 2)
0e0f:0002 (bus 2, device 3)
0cf3:1006 (bus 1, device 3)
[root@zx examples]# lsusb /* 与lsusb命令得出的是一致 */
Bus 001 Device 003: ID 0cf3:1006 Atheros Communications, Inc.
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 003: ID 0e0f:0002
Bus 002 Device 002: ID 0e0f:0003
Bus 002 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hubReference:

7.若想打开调试开关,看出libusb内部的运行情况

在libusb/libusbi.h中有如下代码

#ifdef ENABLE_DEBUG_LOGGING
#define usbi_dbg(...) _usbi_log(NULL, LOG_LEVEL_DEBUG, __VA_ARGS__)
#else
#define usbi_dbg(...) do {} while(0)
#endif
所以说在makefile加上-DENABLE_DEBUG_LOGGING=1这个编译选项即可.

在libusb的目录下找到makefile(configure之后才会生成),找到CFLAGS = -g -O2,替换为CFLAGS = -g -O2 -DENABLE_DEBUG_LOGGING=1

再次make和make install,切换到之前的listdev程序,可以看见已经打出libusb内部的调用顺序:

[root@zx examples]# ./listdevs
libusb: 0.000000 debug [libusb_init] libusb-1.0.9
libusb: 0.003326 debug [find_usbfs_path] found usbfs at /dev/bus/usb
libusb: 0.004064 debug [op_init] found usb devices in sysfs
libusb: 0.004939 debug [usbi_add_pollfd] add fd 3 events 1
libusb: 0.005519 debug [usbi_io_init] using timerfd for timeouts
libusb: 0.006147 debug [usbi_add_pollfd] add fd 5 events 1
libusb: 0.006710 debug [libusb_init] created default context
libusb: 0.007273 debug [libusb_get_device_list]
libusb: 0.011264 debug [sysfs_scan_device] scan usb1
libusb: 0.012133 debug [sysfs_scan_device] bus=1 dev=1
libusb: 0.012681 debug [enumerate_device] busnum 1 devaddr 1 session_id 257
libusb: 0.013244 debug [enumerate_device] allocating new device for 1/1 (session 257)
libusb: 0.013956 debug [sysfs_scan_device] scan usb2
libusb: 0.014740 debug [sysfs_scan_device] bus=2 dev=1
libusb: 0.015685 debug [enumerate_device] busnum 2 devaddr 1 session_id 513
libusb: 0.016251 debug [enumerate_device] allocating new device for 2/1 (session 513)
libusb: 0.016984 debug [sysfs_scan_device] scan 2-1
libusb: 0.017761 debug [sysfs_scan_device] bus=2 dev=2
libusb: 0.018467 debug [enumerate_device] busnum 2 devaddr 2 session_id 514
libusb: 0.018988 debug [enumerate_device] allocating new device for 2/2 (session 514)
libusb: 0.019789 debug [sysfs_scan_device] scan 2-2
libusb: 0.020473 debug [sysfs_scan_device] bus=2 dev=3
libusb: 0.021145 debug [enumerate_device] busnum 2 devaddr 3 session_id 515
libusb: 0.021742 debug [enumerate_device] allocating new device for 2/3 (session 515)
libusb: 0.022440 debug [sysfs_scan_device] scan 1-1
libusb: 0.023203 debug [sysfs_scan_device] bus=1 dev=3
libusb: 0.023986 debug [enumerate_device] busnum 1 devaddr 3 session_id 259
libusb: 0.024548 debug [enumerate_device] allocating new device for 1/3 (session 259)
libusb: 0.025266 debug [libusb_get_device_descriptor]
1d6b:0002 (bus 1, device 1)
libusb: 0.026017 debug [libusb_get_device_descriptor]
1d6b:0001 (bus 2, device 1)
libusb: 0.026479 debug [libusb_get_device_descriptor]
0e0f:0003 (bus 2, device 2)
libusb: 0.026516 debug [libusb_get_device_descriptor]
0e0f:0002 (bus 2, device 3)
libusb: 0.026576 debug [libusb_get_device_descriptor]
0cf3:1006 (bus 1, device 3)
libusb: 0.026616 debug [libusb_unref_device] destroy device 1.1
libusb: 0.026636 debug [libusb_unref_device] destroy device 2.1
libusb: 0.026652 debug [libusb_unref_device] destroy device 2.2
libusb: 0.026667 debug [libusb_unref_device] destroy device 2.3
libusb: 0.026682 debug [libusb_unref_device] destroy device 1.3
libusb: 0.026698 debug [libusb_exit]
libusb: 0.026731 debug [libusb_exit] destroying default context
libusb: 0.026747 debug [usbi_remove_pollfd] remove fd 3
libusb: 0.026778 debug [usbi_remove_pollfd] remove fd 5

PS:这里还有一个问题:

[root@zx test]# ./a.out
./a.out: error while loading shared libraries: libusb-1.0.so.0: cannot open shared object file: No such file or directory
[root@zx test]# /* 运行的时候若出现这个错误,只要先运行下面一句话即可 */
[root@zx test]# export LD_LIBRARY_PATH=/usr/local/lib

标签:1.0,USB,debug,libusb,usr,Linux,device,local
From: https://www.cnblogs.com/kn-zheng/p/17082371.html

相关文章

  • 通用usb驱动libusb介绍和使用示例
    小知识:sudoinsmod/lib/modules/2.6.22-14-generic/kernel/drivers/usb/serial/usbserial.kovendor=0x8086product=0xd001同时插上ttyUSB0和ttyUSB1(ch341),obm可以将dkb......
  • 简单分析USB设备驱动框架
    在生活、工作中经常会接触到USB设备,如鼠标、键盘、摄像头、可移动硬盘、扫码枪等。这些设备通过USB接口连接到电脑上后,电脑会立刻提示“检测到新硬件...”、安装驱动等。这......
  • Linux 安装 CentOS7
    1、linux下载http://mirrors.aliyun.com/centos/7/isos/x86_64/centos-7-isos-x86_64安装包下载_开源镜像站-阿里云(aliyun.com)选择绝大多数人下载的版本就可以了,使......
  • Linux逻辑卷
    Linux逻辑卷架构与相关命令1.为什么要设计逻辑卷(lvm)Linux操作系统中的存储/文件系统,一般都是基于磁盘分区策略来实现的。这过程类似聚会上切蛋糕,一块蛋糕切出来,再分给......
  • linux
    linuxubantu中放大终端窗口的字体显示ctrl+shift+=缩小ctrl+-自动补全tab曾经使用的命令向上键另起一行ctrl+c常见命令命令作用ls查看当前文件......
  • Linux设置动态链接库so的默认搜索路径
    众所周知,Linux动态库的默认搜索路径是/lib和/usr/lib,64位系统是/lib64和/usr/lib64。动态库被创建后,一般都复制到这两个目录中。当程序执行时需要某动态库,并且该动态库......
  • Linux Nginx安装以及可能出现错误
    一、编译安装makemakeinstallmake的过程是把各种语言写的源码文件,变成可执行文件和各种库文件;makeinstall是把这些编译出来的可执行文件和库文件复制到合适的地方。......
  • linux服务器运行java项目, 监控查看内存、储存空间和cpu占用率
    服务器部署方式为tomcat中运行war包的方式,有一次重新部署时候发现报异常堆栈溢出了.想要定位到某个war包中通过学习整理出此篇文章以作记录笔记.1.关于内存过高......
  • Linux创建新用户并配置远程连接
    useraddz #创建用户ztail-f/var/log/audit/audit.log#查看登录日志sudovi/etc/sudoers#查看权限,并修改权限  #增加sudo到root权限“#inc......
  • Linux创建文件报错:/bin/bash^M: bad interpreter
    在使用Linux创建好文件shell文件之后,发生了这样一个错误:/bin/bash^M:badinterpreter查找资料后发现:在执行shell脚本时提示这样的错误主要是由于shell脚本文件是dos格式,即......