The 'linusb' is one of the most used open source library for work with usb devices. It allow to make the basic usb data transfers operation in a little bit easier way compared to standard system calls. Here a short tutorial regarding how to use asynchronous bulk transfer.
Asynchronous transfer allow to launch the transfer command and return immediately, a callback function will be called once the data will be transfer/received completely. Using such transfer mode is quite simple, let's go to see a practical example. At first we have to initialize libusb library and look for the USB device we want to communicate to. Usually device is referred using the two values called Vendor ID and Product ID. The first is a unique number "assigned" to each hardware device producer and the second is a number reporting the device type and model.
libusb_device_handle *dev_handle = NULL;
libusb_device **list;
size_t count, i; // Initialize library
libusb_init(NULL); // Get list of USB devices currently connected
count = libusb_get_device_list(NULL, &list); for(i = 0; i < count; i++)
{
struct libusb_device_descriptor desc; libusb_get_device_descriptor(list[i], &desc); // Is our device?
if(desc.idVendor == 0x1234 && desc.idProduct == 0x5678)
{
// Open USB device and get handle
libusb_open(list[i], &dev_handle);
break;
}
} libusb_free_device_list(list, 1);
Now we have the handle to the USB device we want to communicate. The steps to performs an asynchronous bulk transfer are basically two, first prepare the structure with all the data for "instruct" USB controller to make request operation and then submit the data for start the transfer. Please note that the transfer direction (IN or OUT) is determined by the USB endpoint type we want to communicate to/from. An USB device usually provide some connections called Endpoint for allow data transfer between host and device. An Endpoint is usually marked as IN and OUT where, please note, in/out direction are from host point of view. This mean an endpoint marked as IN, for example, transfer data from device to host (usually the PC where the USB device is connected). Once clarified this point we can move to see the asynchronous transfer procedure as follow:
struct libusb_transfer *xfr;
unsigned char *data;
const unsigned int size = 1024; data = malloc(size);
xfr = libusb_alloc_transfer(0); libusb_fill_bulk_transfer(xfr,
dev_handle,
0x82, // Endpoint ID
data,
size,
callbackUSBTransferComplete,
NULL,
5000
); if(libusb_submit_transfer(xfr) < 0)
{
// Error
libusb_free_transfer(xfr);
free(data);
}
Detailed explanation about each params used can be found at official documentation here. In this example we prepare for a bulk transfer from the device using endpoint 0x82. Data size to transfer are 1024 bytes and timeout is 5000 milliseconds. callbackUSBTransferComplete is the name of the function automatically called when the transfer will be completed. Please note here the function will be called also in case of errors. It's necessary to check params returned  as follow:
void callbackUSBTransferComplete(struct libusb_transfer *xfr)
{
switch(xfr->status)
{
case LIBUSB_TRANSFER_COMPLETED:
// Success here, data transfered are inside
// xfr->buffer
// and the length is
// xfr->actual_length
break;
case LIBUSB_TRANSFER_CANCELLED:
case LIBUSB_TRANSFER_NO_DEVICE:
case LIBUSB_TRANSFER_TIMED_OUT:
case LIBUSB_TRANSFER_ERROR:
case LIBUSB_TRANSFER_STALL:
case LIBUSB_TRANSFER_OVERFLOW:
// Various type of errors here
break;
}
}
Now the transfer code seem complete. However an additional step have to be made. What is not very clear here (at least it wasn't clear for me in the past) is that the "internal USB engine" who make the transfer and automatically call the callback function is not in a separate thread working in parallel but, for have it working as expected, need to be "keep alive" by continuously calling the function libusb_handle_events as follow:
while(1)
{
if(libusb_handle_events(NULL) != LIBUSB_SUCCESS) break;
}
As you can note this is an infinite loop with minimum of error check used just for example purpose. In case you need to manage additional error conditions and provide some way to exit from loop if your software require it. This small snippet is only to explain that if you don't continuously call the libusb_handle_events function your callback function will never be called also if the USB transfer was completed (and this can result in many hours lost insearch for the problem... ^_^).

转-Asynchronous bulk transfer using libusb的更多相关文章

  1. libusb 源码阅读

    libusb_init(NULL), 如果传入一个NULL, 则libusb 内部会有一个 usbi_default_context 变量在内部保存上下文. 这样以后调用 libusb 函数时可以不指 ...

  2. bulkTransfer通讯必须注意的问题:bulk buffer size(16K)

    Android USB host与HID使用bulkTransfer通讯接收和发送的数据长度不会超过16384,这个问题困扰了我很长一段时间,终于发现问题所在,不是出在我的程序设计,也不是硬件的发送/ ...

  3. C#开发usb通知之bulk传输

    usb通信分为4种传输方式,下位机通信协议用的是块传输,也就是bulk传输,C#下实现的usb通信使用的是开源的LibUsbDotNet,主要的就是需要在C#中添加LibUsbDotNet.dll引用 ...

  4. redis.conf配置详细解析

    # redis 配置文件示例 # 当你需要为某个配置项指定内存大小的时候,必须要带上单位, # 通常的格式就是 1k 5gb 4m 等酱紫: # # 1k => 1000 bytes # 1kb ...

  5. redis配置详解

    ##redis配置详解 # Redis configuration file example. # # Note that in order to read the configuration fil ...

  6. redis.conf配置详细翻译解析

    # redis 配置文件示例 # 当你需要为某个配置项指定内存大小的时候,必须要带上单位, # 通常的格式就是 1k 5gb 4m 等酱紫: # # 1k => 1000 bytes # 1kb ...

  7. Redis 配置文件

    # Redis configuration file example. # # Note that in order to read the configuration file, Redis mus ...

  8. Redis 配置文件详解

    # Redis 配置文件 # 当配置中需要配置内存大小时,可以使用 1k, 5GB, 4M 等类似的格式,其转换方式如下(不区分大小写)## 1k => 1000 bytes# 1kb => ...

  9. Redis基本配置

    # redis 配置文件示例 # 当你需要为某个配置项指定内存大小的时候,必须要带上单位, # 通常的格式就是 1k 5gb 4m 等酱紫: # # 1k => 1000 bytes # 1kb ...

随机推荐

  1. 02 Vue之vue对象属性功能&axios数据请求实现

    1.过滤器的声明和使用 过滤器,就是vue允许开发者自定义的文本格式化函数,可以使用在两个地方:输出内容和操作数据中. 定义过滤器的方式有两种. 1 使用Vue.filter()进行全局定义 2 在v ...

  2. Kindergarten CodeForces - 484D (贪心,好题)

    大意: 给定序列, 求划分为若干段, 使得总贡献最大, 每段的贡献为max-min 可以发现最优解一定是连续一段递增或递减, 然后dp即可. #include <iostream> #in ...

  3. Spring配置表友好性优化思路

    Spring配置表需要尽量保证对程序员的友好性,一下提供一种优化思路. 中途未保存,心态炸了,只贴图了,fuuuuuuuuuuuuuck 第一种(最烂,最不友好):以Json的格式保存在配置表中,程序 ...

  4. TCHAR用法

    TCHAR 就是当你的字符设置为什么就是什么例如:程序编译为 ANSI, TCHAR 就是相当于 CHAR当程序编译为 UNICODE, TCHAR 就相当于 WCHAR char :单字节变量类型, ...

  5. 非常不错的地区三级联动,js简单易懂。封装起来了

    首先需要引入area.js,然后配置并初始化插件: 例: <!-- 绑定银行卡开始 --> <script src="js/area.js"></sc ...

  6. Oracle11g温习-第十八章:role管理

    2013年4月27日 星期六 10:52 role 的功能:简化用户的权限管理 建立角色——给角色授权——将角色授予用户/角色 2.查看系统建立的role SYS @ prod >  selec ...

  7. GSON使用之对特殊字符的转换的处理

    很多人是在转换时特殊字符被替换成了unicode编程格式,而我碰到的类似,只不过是后台转换成json字符串到前端,前端解析时 '' 双引号和 / 斜杠被原样转换,冲突了json的关键字符,导致解析时提 ...

  8. 利用sqlldr从MySQL导出一张表数据到Oracle

    根据业务需求,需要从MySQL库中同步一张表tap_application到Oracle中,下面是记录的导入过程. 1. 查看MySQL表结构 desc tap_application; +----- ...

  9. rpc框架实现(持续更新)

    网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,rpc基于长连接的远程过程调用应用而生. 一:A服务调用B服务,整个调用过程,主要经历如下几个步骤:(摘自 ...

  10. [codechef July Challenge 2017] Pishty and tree

    PSHTTR: Pishty 和城堡题目描述Pishty 是生活在胡斯特市的一个小男孩.胡斯特是胡克兰境内的一个古城,以其中世纪风格的古堡和非常聪明的熊闻名全国.胡斯特的镇城之宝是就是这么一座古堡,历 ...