Linux 下使用libusb 与USB-HID 设备通讯之控制传输

10 篇文章 0 订阅
8 篇文章 1 订阅
一、前言

        上一篇文章记录到如何在ubuntu 安装开源项目libusb,这篇将记录,如下使用libusb 提供的api 方便的与USB-HID 设备通讯,通讯方式为控制传输。


二、关于libusb 如何查找HID 设备,可以看观看一下我的Linux 下使用libusb 与USB-HID 设备通讯之中断传输

这篇文章,里面有详细记载如何查找HID 设备。


三、libusb_control_transfer() 函数

int LIBUSB_CALL libusb_control_transfer(libusb_device_handle *dev_handle,
	uint8_t request_type, uint8_t bRequest, uint16_t wValue, uint16_t wIndex,
	unsigned char *data, uint16_t wLength, unsigned int timeout);

在libusb中控制传输使用的就是上面的函数,对比下面这个Android 中控制传输的方法

 public int controlTransfer(int requestType, int request, int value, int index,
            byte[] buffer, int offset, int length, int timeout) {
        checkBounds(buffer, offset, length);
        return native_control_request(requestType, request, value, index,
                buffer, offset, length, timeout);
    }

比较上面两个方法,可以知道是相似的。因此,对于libusb_control_tranfer() 这个函数的参数详细的讲解以及如何传参,

请跳转到我的另一篇文章,Android USB HID bulkTransfer()参数解析,这篇文章有详细说明。

这里,我直接贴出代码,如下:

3.1 打开设备

int OpenUsbControlTransferDevice()
{
  	int error = 0;

	error = libusb_init(&ctxForControlTransfer);
	if (error < 0)
	{
		if(gUsbDebugEnable)
			PrintError("libusb init failed", __FILE__, __FUNCTION__, __LINE__, error);

		return DEIVCE_ERROR_OPEN_DEVICE;
	}
/*
	if(gUsbDebugEnable)
		libusb_set_debug(ctxForControlTransfer, LIBUSB_LOG_LEVEL_INFO); //set verbosity level to 3, as suggested in the documentation
*/
	mHandleDevForControlTransfer = libusb_open_device_with_vid_pid(ctxForControlTransfer, DEVICE_VID_FOR_CONTRALTRANSFER, DEVICE_PID_FOR_CONTRALTRANSFER);
	if(mHandleDevForControlTransfer == NULL)
	{
		if(gUsbDebugEnable)
			PrintError("libusb open device failed", __FILE__, __FUNCTION__, __LINE__, error);

		return DEIVCE_ERROR_OPEN_DEVICE;
	}
		
	if(libusb_kernel_driver_active(mHandleDevForControlTransfer, 0) == 1)
	{
		libusb_detach_kernel_driver(mHandleDevForControlTransfer, 0);
	}
		
	error = libusb_claim_interface(mHandleDevForControlTransfer, 0);
	if (error < 0) 
	{
		if(gUsbDebugEnable)
			PrintError("libusb claim interface failed ", __FILE__, __FUNCTION__, __LINE__, error);

		return DEIVCE_ERROR_OPEN_DEVICE;
	}
		
	return DEVICE_ERROR_OK;
}

上面,其中有一点需要注意的是,在USB-HID 协议中规定,控制传输默认使用interface 0,因此,在申请接口时,需要注意。

3.2 关闭设备

int CloseUsbControlTransferDevice()
{
	if(mHandleDevForControlTransfer != NULL)
	{
		libusb_release_interface(mHandleDevForControlTransfer, 0);
		libusb_close(mHandleDevForControlTransfer);	
	}

	if(ctxForControlTransfer != NULL)
		libusb_exit(ctxForControlTransfer);

	return DEVICE_ERROR_OK;
}

3.3 Host 主机 --》 HID 设备

error = libusb_control_transfer(mHandleDevForControlTransfer, 0x21, 0x09, 0x301, 0, dataStream,  0x100, timeout);
	if(error < 0)
		return DEVICE_ERROR_USB_SEND_ERROR;
	else
		return DEVICE_ERROR_OK;

3.4 HID 设备 --》 Host 主机

error = libusb_control_transfer(mHandleDevForControlTransfer, 0xA1, 0x01, 0x302, 0, dataStream,  0x100, timeout);	
	if(error < 0)
	{
		return DEVICE_ERROR_USB_REC_ERROR;
	}

关于libusb的控制传输就是记录这么多了,方便下次可以回顾。微笑

  • 3
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
Linux使用libusb,你需要进行以下几个步骤: 1. 安装libusb库:使用包管理器安装libusb库,例如在Ubuntu上可以使用以下命令进行安装: ``` sudo apt-get install libusb-1.0-0-dev ``` 2. 编译链接使用libusb的程序:在编译你的程序时,需要将libusb库链接进去。可以使用以下命令编译一个简单的示例程序: ``` gcc -o your_program your_program.c -lusb-1.0 ``` 3. 编写代码:在你的代码中包含libusb的头文件,并使用libusb提供的函数进行设备的检测、打开、读写等操作。以下是一个简单的示例代码: ```c #include <stdio.h> #include <stdlib.h> #include <libusb-1.0/libusb.h> int main() { libusb_context *ctx = NULL; libusb_device_handle *dev_handle = NULL; // 初始化libusb库 if (libusb_init(&ctx) < 0) { printf("Failed to initialize libusb\n"); return -1; } // 打开指定的USB设备 dev_handle = libusb_open_device_with_vid_pid(ctx, vendor_id, product_id); if (dev_handle == NULL) { printf("Failed to open USB device\n"); libusb_exit(ctx); return -1; } // 在这里可以进行设备的读写操作 // 关闭设备和释放资源 libusb_close(dev_handle); libusb_exit(ctx); return 0; } ``` 请注意,上述示例代码中的`vendor_id`和`product_id`需要根据实际情况进行定义,以指定要打开的USB设备。 4. 运行程序:在终端中运行编译生成的可执行文件,即可使用libusb操作USB设备。 以上是在Linux使用libusb的基本步骤,你可以根据自己的需求进行进一步的开发和调试。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值