78 Linux libusb库USB HID应用编程笔记

2024-08-26 23:04

本文主要是介绍78 Linux libusb库USB HID应用编程笔记,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1 前言

        这几天搞另外一个项目,基于Ubuntu开发一个小的应用程序,就是通过USB HID与设备通信。因此需要在Linux环境编写对应USB HID通信应用。

        目前libusb库已经很好的支持USB相关应用的开发,库中提供了丰富的USB接口,用户可以直接调用其提供的API,实现快速开发。

        本文对USB HID应用开发进行了简要记录,方便日后自己查看复习。

2 libusb库准备

(1)获取libusb库源码

下面链接是libusb库的源码下载地址。

libusb库源码:https://github.com/libusb/libusb/releases

(2)编译libusb库

获取到源码之后,需要对库进行编译,下面记录了自己编译的指令。

cd libusb-1.0.26/
#配置
./configure --prefix=/home/libusbinstall --build=x86_64-linux --disable-udev
#编译
make
#安装
make install

(3)安装目录

安装之后可以看到目标目录存在两个文件夹:include/ 和 lib/

3 应用示例

下面直接上代码,无需多言,看代码即可(代码风格及规范进行了简化,实际上没那么丑的)。

头文件:hidusb.h

#include <stdint.h>#include "libusb.h"int hidUSB_write(libusb_device_handle *dev_handle, unsigned char endpoint, unsigned char *pDataIn, int nDataLen, int ntimeout);
int hidUSB_read(libusb_device_handle *dev_handle,unsigned char endpoint, unsigned char *pDataRcv, int nDataLen, int ntimeout);
int hidUSB_open(libusb_device_handle **dev_hdlout, uint16_t vendor_id, uint16_t product_id);
int hidUSB_close(libusb_device_handle *dev_handle);void hidUSB_DeInit(void);
int hidUSB_Init(void);

源文件:hidusb.c

#include <stdio.h>
#include <stdint.h>
#include <string.h>#include "hidusb.h"static libusb_context 			*gUSBCtx = NULL;int hidUSB_write(libusb_device_handle *dev_handle, unsigned char endpoint, unsigned char *pDataIn, int nDataLen, int ntimeout)
{int ret = -1;int transferred = 0;ret = libusb_interrupt_transfer(dev_handle, endpoint, pDataIn, nDataLen, &transferred, ntimeout);if(ret<0){perror("failed to write\n");return 0;}return transferred;}int hidUSB_read(libusb_device_handle *dev_handle,unsigned char endpoint, unsigned char *pDataRcv, int nDataLen, int ntimeout)
{int ret = -1;int transferred = 0;ret = libusb_interrupt_transfer(dev_handle,endpoint, pDataRcv, 64, &transferred, ntimeout);if(ret!=0){perror("failed to read\n");return 0;}return transferred;}int hidUSB_open(libusb_device_handle **dev_hdlout, uint16_t vendor_id, uint16_t product_id)
{int ret = -1;libusb_device_handle *tdev_handle = NULL;//打开指定pid和vid的设备tdev_handle = libusb_open_device_with_vid_pid(NULL, vendor_id, product_id);if(tdev_handle == NULL){perror("Cannot open device\n");return -1;}//内核驱动激活与分离(这一句我没怎么搞懂为啥需要,懂的朋友欢迎留言告诉我)if(libusb_kernel_driver_active(tdev_handle, 0) == 1){ printf("Kernel Driver Active\n");if(libusb_detach_kernel_driver(tdev_handle, 0) == 0){printf("Kernel Driver Detached!\n");}}ret = libusb_claim_interface(tdev_handle, 0);if(ret < 0) {perror("Cannot Claim Interface\n");goto iExit;}*dev_hdlout = tdev_handle;return 0;iExit:	if(tdev_handle)libusb_close(tdev_handle);return -1;
}int hidUSB_close(libusb_device_handle *dev_handle)
{int ret = -1;if(dev_handle){ret = libusb_release_interface(dev_handle, 0);if(ret!=0){	perror("Cannot libusb_release_interface\n");return -1;}libusb_close(dev_handle);}return 0;
}void hidUSB_DeInit(void)
{libusb_exit(gUSBCtx);
}int hidUSB_Init(void)
{int ret = -1;ret = libusb_init(&gUSBCtx); if(ret < 0) {perror("libusb_init failed\n");return -1;	} return 0;
}

主程序:main.c

#include <stdio.h>
#include <unistd.h>
#include "hidusb.h"//设备的标识号
#define HT232_USB_VID 			0x5548
#define HT232_USB_PID 			0x6666//这个需要看设备所使用的ep
#define HT232_USB_HID_EPOUT		0x01
#define HT232_USB_HID_EPIN		0x81int main(void)
{int ni = 0;int nRet = -1;uint8_t ucSndBuf[256] = {0};uint8_t ucRcvBuf[256] = {0};uint8_t ucSndLen = 0;uint8_t ucRcvLen = 0;libusb_device_handle *dev_handle = NULL;nRet =  hidUSB_Init();if(nRet != 0){perror("hidUSB_Init failed\r\n");return -1;}nRet = hidUSB_open(&dev_handle, HT232_USB_VID, HT232_USB_PID);if(nRet != 0){perror("hidUSB_open failed\r\n");return -1;}while(1){ucSndBuf[0] = 0x01;ucSndBuf[1] = 0x02;ucSndBuf[2] = 0x03;ucSndBuf[3] = 0x04;ucSndLen = 4;nRet =  hidUSB_write(dev_handle, HT232_USB_HID_EPOUT, ucSndBuf, ucSndLen, 1000);if(nRet == 0){perror("hidUSB_write failed\r\n");goto iSleep;}ucRcvLen =  hidUSB_read(dev_handle, HT232_USB_HID_EPIN, ucRcvBuf, sizeof(ucRcvBuf), 1000);if(ucRcvLen == 0){perror("hidUSB_read failed\r\n");goto iSleep;}for(ni=0; ni<ucRcvLen; ni++){printf("%02X ", ucRcvBuf[ni]);}printf("\r\n");
iSleep:sleep(1);}nRet = hidUSB_close(dev_handle);if(nRet != 0){perror("hidUSB_open failed\r\n");}hidUSB_DeInit();return 0;
}

上述即为Linux环境下USB HID应用编程demo。

4 结束语

知识分享,共同进步。

over!

--------------------------------------------------------------------------------------------------------------

卖个广告:ble、4G、lora、wifi等门禁设备,各种读卡器模块、成品都有,若有需要,可私信。

这篇关于78 Linux libusb库USB HID应用编程笔记的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1109937

相关文章

深入浅出Spring中的@Autowired自动注入的工作原理及实践应用

《深入浅出Spring中的@Autowired自动注入的工作原理及实践应用》在Spring框架的学习旅程中,@Autowired无疑是一个高频出现却又让初学者头疼的注解,它看似简单,却蕴含着Sprin... 目录深入浅出Spring中的@Autowired:自动注入的奥秘什么是依赖注入?@Autowired

linux系统上安装JDK8全过程

《linux系统上安装JDK8全过程》文章介绍安装JDK的必要性及Linux下JDK8的安装步骤,包括卸载旧版本、下载解压、配置环境变量等,强调开发需JDK,运行可选JRE,现JDK已集成JRE... 目录为什么要安装jdk?1.查看linux系统是否有自带的jdk:2.下载jdk压缩包2.解压3.配置环境

Linux搭建ftp服务器的步骤

《Linux搭建ftp服务器的步骤》本文给大家分享Linux搭建ftp服务器的步骤,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录ftp搭建1:下载vsftpd工具2:下载客户端工具3:进入配置文件目录vsftpd.conf配置文件4:

PostgreSQL简介及实战应用

《PostgreSQL简介及实战应用》PostgreSQL是一种功能强大的开源关系型数据库管理系统,以其稳定性、高性能、扩展性和复杂查询能力在众多项目中得到广泛应用,本文将从基础概念讲起,逐步深入到高... 目录前言1. PostgreSQL基础1.1 PostgreSQL简介1.2 基础语法1.3 数据库

Python异步编程之await与asyncio基本用法详解

《Python异步编程之await与asyncio基本用法详解》在Python中,await和asyncio是异步编程的核心工具,用于高效处理I/O密集型任务(如网络请求、文件读写、数据库操作等),接... 目录一、核心概念二、使用场景三、基本用法1. 定义协程2. 运行协程3. 并发执行多个任务四、关键

Linux实现查看某一端口是否开放

《Linux实现查看某一端口是否开放》文章介绍了三种检查端口6379是否开放的方法:通过lsof查看进程占用,用netstat区分TCP/UDP监听状态,以及用telnet测试远程连接可达性... 目录1、使用lsof 命令来查看端口是否开放2、使用netstat 命令来查看端口是否开放3、使用telnet

Linux系统管理与进程任务管理方式

《Linux系统管理与进程任务管理方式》本文系统讲解Linux管理核心技能,涵盖引导流程、服务控制(Systemd与GRUB2)、进程管理(前台/后台运行、工具使用)、计划任务(at/cron)及常用... 目录引言一、linux系统引导过程与服务控制1.1 系统引导的五个关键阶段1.2 GRUB2的进化优

AOP编程的基本概念与idea编辑器的配合体验过程

《AOP编程的基本概念与idea编辑器的配合体验过程》文章简要介绍了AOP基础概念,包括Before/Around通知、PointCut切入点、Advice通知体、JoinPoint连接点等,说明它们... 目录BeforeAroundAdvise — 通知PointCut — 切入点Acpect — 切面

Python中的filter() 函数的工作原理及应用技巧

《Python中的filter()函数的工作原理及应用技巧》Python的filter()函数用于筛选序列元素,返回迭代器,适合函数式编程,相比列表推导式,内存更优,尤其适用于大数据集,结合lamb... 目录前言一、基本概念基本语法二、使用方式1. 使用 lambda 函数2. 使用普通函数3. 使用 N

Python中yield的用法和实际应用示例

《Python中yield的用法和实际应用示例》在Python中,yield关键字主要用于生成器函数(generatorfunctions)中,其目的是使函数能够像迭代器一样工作,即可以被遍历,但不会... 目录python中yield的用法详解一、引言二、yield的基本用法1、yield与生成器2、yi