- 浏览: 593409 次
- 性别:
- 来自: 厦门
文章分类
最新评论
-
phil09s:
写的真简洁!就怕搞不定!
SWT,打包发布SWT程序,转EXE一条龙过程记录 -
stona126:
楼主能给出这个Java高级图像处理I/0工具包的下载地址吗?谢 ...
java图像处理 -
chen_jp:
非常好,谢谢!
C++各大有名库的介绍——GUI -
effort_fan:
学习了。
一个Java画图板程序的设计 -
zhongxinhu:
谢谢,刚好在你这里找到答案!
关于java 读取propterties 文件的疑惑 和问题的解决[转载]
Libusb库的使用
转自:http://blog.sina.com.cn/s/blog_4a5b632801009upv.html
使用libusb之前你的linux系统必须装有usb文件系统,这里还介绍了使用hiddev设备文件来访问设备,目的在于不仅可以比较出usb的易用性,还提供了一个转化成libusb驱动的案例。
3.1 find设备
任何驱动第一步首先是寻找到要操作的设备,我们先来看看HID驱动是怎样寻找到设备的。我们假设寻找设备的函数Device_Find(注:代码只是为了方便解说,不保证代码的健全)
int Device_Find()
{
<wbr> </wbr><wbr> </wbr><wbr> char dir_str[100]; </wbr><wbr> </wbr><wbr></wbr>
<wbr> </wbr><wbr> </wbr><wbr> char hiddev[100]; </wbr><wbr> </wbr><wbr> </wbr><wbr></wbr>
DIR dir; <wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr></wbr>
memset (dir_str, 0 , sizeof(dir_str));
memset (hiddev, 0 , sizeof(hiddev));
<wbr> </wbr><wbr> </wbr><wbr></wbr>
dir=opendir("/dev/usb/hid");
<wbr> </wbr><wbr> </wbr><wbr> if(dir){</wbr>
<wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr></wbr>
<wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> sprintf(dir_str,"/dev/usb/hid/");</wbr>
<wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> closedir(dir);</wbr>
<wbr> </wbr><wbr> </wbr><wbr> }else{</wbr>
<wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr></wbr>
<wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> sprintf(dir_str,"/dev/usb/");</wbr>
<wbr> </wbr><wbr> </wbr><wbr> }</wbr>
<wbr> </wbr><wbr> </wbr><wbr></wbr>
for(i = 0; i < DEVICE_MINOR; i++) {
<wbr> </wbr><wbr> </wbr><wbr></wbr>
<wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> sprintf(hiddev, "%shiddev%d", dir_str,i);</wbr>
<wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr></wbr>
<wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> fd = open(hiddev, O_RDWR);</wbr>
<wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> if(fd > 0) {</wbr>
<wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr></wbr>
<wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> ioctl(fd, HIDIOCGDEVINFO, &info);</wbr>
<wbr> </wbr><wbr> </wbr><wbr></wbr>
<wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr></wbr>
<wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> if(info.vendor== VENDOR_ID && info.product== PRODUCT_ID) {</wbr>
<wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr></wbr>
<wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr></wbr>
<wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> device_num++; </wbr><wbr> </wbr><wbr></wbr>
<wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> }</wbr>
<wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> close(fd);</wbr>
<wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> }</wbr>
<wbr> </wbr><wbr> </wbr><wbr> }</wbr>
<wbr> </wbr><wbr> </wbr><wbr> return device_num; </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr></wbr>
}
我们再来看libusb是如何来寻找和初始化设备
int Device_Find()
{
struct usb_bus <wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> *busses;</wbr>
<wbr> </wbr><wbr> </wbr><wbr> int </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> device_num = 0;</wbr>
<wbr> </wbr><wbr> </wbr><wbr> device_num = 0; </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr></wbr>
<wbr> </wbr><wbr> </wbr><wbr></wbr>
<wbr> </wbr><wbr> </wbr><wbr> usb_init(); </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr></wbr>
<wbr> </wbr><wbr> </wbr><wbr> usb_find_busses(); </wbr><wbr> </wbr><wbr></wbr>
<wbr> </wbr><wbr> </wbr><wbr> usb_find_devices();</wbr>
<wbr> </wbr><wbr> </wbr><wbr></wbr>
<wbr> </wbr><wbr> </wbr><wbr></wbr>
busses = usb_get_busses();
<wbr> </wbr><wbr> </wbr><wbr> struct usb_bus </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> *bus;</wbr>
<wbr> </wbr><wbr> </wbr><wbr></wbr>
<wbr> </wbr><wbr> </wbr><wbr> for (bus = busses; bus; bus = bus->next) {</wbr>
<wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> struct usb_device *dev;</wbr>
<wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr></wbr>
<wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> for (dev = bus->devices; dev; dev = dev->next) {</wbr>
<wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr></wbr>
if(dev->descriptor.idVendor==VENDOR_ID&& dev->descriptor.idProduct == PRODUCT_ID) {
<wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr></wbr>
<wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr></wbr>
<wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> device_num++; </wbr><wbr> </wbr><wbr></wbr>
} <wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr></wbr>
<wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> } </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr></wbr>
<wbr> </wbr><wbr> </wbr><wbr> }</wbr>
<wbr> </wbr><wbr> </wbr><wbr> return device_num; </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr></wbr>
}
注:在新版本的libusb中,usb_get_busses就可以不用了,这个函数是返回系统上的usb总线链表句柄
这里我们直接用usb_busses变量,这个变量在usb.h中被定义为外部变量
所以可以直接写成这样:
struct usb_bus <wbr> </wbr><wbr> *bus;</wbr>
<wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> for (bus = usb_busses; bus; bus = bus->next) {</wbr>
<wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> struct usb_device *dev;</wbr>
<wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> for (dev = bus->devices; dev; dev = dev->next) {</wbr>
<wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr></wbr>
<wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> }</wbr>
}
3.2 打开设备
假设我们定义的打开设备的函数名是device_open,
int Device_Open()
{
<wbr> </wbr><wbr> </wbr><wbr> int handle;</wbr>
<wbr> </wbr><wbr> </wbr><wbr></wbr>
handle = open(“hiddev0”, O_RDONLY);
}
int Device_Open()
{
struct usb_device* <wbr> </wbr><wbr> </wbr><wbr> udev;</wbr>
usb_dev_handle* <wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> device_handle;</wbr>
device_handle = usb_open(udev);
}
3.3 读写设备和操作设备
假设我们的设备使用控制传输方式,至于批处理传输和中断传输限于篇幅这里不介绍
我们这里定义三个函数,Device_Write, Device_Read, Device_Report
Device_Report 功能发送接收函数
Device_Write 功能写数据
Device_Read <wbr> </wbr><wbr> 功能读数据</wbr>
Device_Write和Device_Read调用Device_Report发送写的信息和读的信息,开发者根据发送的命令协议来设计,我们这里只简单实现发送数据的函数。
假设我们要给设备发送72字节的数据,头8个字节是报告头,是我们定义的和设备相关的规则,后64位是数据。
HID驱动的实现(这里只是用代码来有助理解,代码是伪代码)
int Device_Report(int fd, unsigned char *buffer72)
{
int <wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> ret;</wbr>
int <wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> index;</wbr>
<wbr> </wbr><wbr> </wbr><wbr> unsigned char send_data[72];</wbr>
unsigned char recv_data[72];
<wbr> </wbr><wbr> </wbr><wbr> struct hiddev_usage_ref uref;</wbr>
<wbr> </wbr><wbr> </wbr><wbr> struct hiddev_report_info rinfo;</wbr>
<wbr> </wbr><wbr> </wbr><wbr> ret = ioctl(fd, HIDIOCINITREPORT, 0);</wbr>
<wbr> </wbr><wbr> </wbr><wbr> if( ret !=0) {</wbr>
<wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> return NOT_OPENED_DEVICE;</wbr>
<wbr> </wbr><wbr> </wbr><wbr> }</wbr>
<wbr> </wbr><wbr> </wbr><wbr></wbr>
<wbr> </wbr><wbr> </wbr><wbr> for(index = 0; index < 72; index++) {</wbr>
<wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr></wbr>
<wbr> </wbr><wbr> </wbr><wbr> uref.report_type = HID_REPORT_TYPE_FEATURE;</wbr>
<wbr> </wbr><wbr> </wbr><wbr> uref.report_id = HID_REPORT_ID_FIRST;</wbr>
<wbr> </wbr><wbr> </wbr><wbr> uref.usage_index = index;</wbr>
<wbr> </wbr><wbr> </wbr><wbr> uref.field_index = 0;</wbr>
<wbr> </wbr><wbr> </wbr><wbr> uref.value = send_data[index];</wbr>
<wbr> </wbr><wbr> </wbr><wbr> ioctl(fd, HIDIOCGUCODE, &uref);</wbr>
<wbr> </wbr><wbr> </wbr><wbr> ret=ioctl(fd, HIDIOCSUSAGE, &uref);</wbr>
<wbr> </wbr><wbr> </wbr><wbr> if(ret != 0 ){</wbr>
<wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> return UNKNOWN_ERROR;</wbr>
<wbr> </wbr><wbr> </wbr><wbr> }</wbr>
}
rinfo.report_type = HID_REPORT_TYPE_FEATURE;
rinfo.report_id = HID_REPORT_ID_FIRST;
rinfo.num_fields = 1;
ret=ioctl(fd, HIDIOCSREPORT, &rinfo); <wbr> </wbr><wbr></wbr>
if(ret != 0) {
<wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> return WRITE_REPORT;</wbr>
}
ret = ioctl(fd, HIDIOCINITREPORT, 0);
for(index = 0; index < 72; index++) {
<wbr> </wbr><wbr> </wbr><wbr> uref.report_type = HID_REPORT_TYPE_FEATURE;</wbr>
<wbr> </wbr><wbr> </wbr><wbr> uref.report_id = HID_REPORT_ID_FIRST;</wbr>
<wbr> </wbr><wbr> </wbr><wbr> uref.usage_index = index;</wbr>
<wbr> </wbr><wbr> </wbr><wbr> uref.field_index = 0;</wbr>
<wbr> </wbr><wbr> </wbr><wbr> ioctl(fd, HIDIOCGUCODE, &uref);</wbr>
<wbr> </wbr><wbr> </wbr><wbr> ret = ioctl(fd, HIDIOCGUSAGE, &uref);</wbr>
<wbr> </wbr><wbr> </wbr><wbr> if(ret != 0 ) {</wbr>
<wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> return UNKNOWN_ERROR;</wbr>
<wbr> </wbr><wbr> </wbr><wbr> }</wbr>
<wbr> </wbr><wbr> </wbr><wbr> recv_data[index] = uref.value;</wbr>
}
memcpy(buffer72, recv_data, 72);
return SUCCESS;
}
libusb驱动的实现
int Device_Report(int fd, unsigned char *buffer72)
{
<wbr> </wbr><wbr> </wbr><wbr></wbr>
<wbr> </wbr><wbr> </wbr><wbr> usb_dev_handle* Device_handle;</wbr>
<wbr> </wbr><wbr> </wbr><wbr></wbr>
<wbr> </wbr><wbr> </wbr><wbr></wbr>
<wbr> </wbr><wbr> </wbr><wbr> unsigned char </wbr><wbr> </wbr><wbr> send_data[72];</wbr>
<wbr> </wbr><wbr> </wbr><wbr> unsigned char </wbr><wbr> </wbr><wbr> recv_data[72];</wbr>
<wbr> </wbr><wbr> </wbr><wbr></wbr>
<wbr> </wbr><wbr> </wbr><wbr> int </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> send_len;</wbr>
<wbr> </wbr><wbr> </wbr><wbr> int </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> recv_len;</wbr>
<wbr> </wbr><wbr> </wbr><wbr></wbr>
<wbr> </wbr><wbr> </wbr><wbr></wbr>
<wbr> </wbr><wbr> </wbr><wbr> memset(send_data, 0 , sizeof(send_data));</wbr>
<wbr> </wbr><wbr> </wbr><wbr> memset(recv_data, 0 , sizeof(recv_data));</wbr>
<wbr> </wbr><wbr> </wbr><wbr></wbr>
<wbr> </wbr><wbr> </wbr><wbr></wbr>
<wbr> </wbr><wbr> </wbr><wbr> Device_handle = (usb_dev_handle*)(g_list[fd].device_handle);</wbr>
<wbr> </wbr><wbr> </wbr><wbr> if (Device_handle == NULL) {</wbr>
<wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> return NOT_OPENED_DEVICE;</wbr>
}
usb_claim_interface(Device_handle, 0);
send_len = usb_control_msg(Device_handle,
USB_ENDPOINT_OUT + USB_TYPE_CLASS + USB_RECIP_INTERFACE,
<wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> HID_REPORT_SET,</wbr>
<wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> 0x300,</wbr>
<wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> 0,</wbr>
<wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> send_data, 72, USB_TIMEOUT);</wbr>
if (send_len < 0) {
<wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> return WRITE_REPORT;</wbr>
}
if (send_len != 72) {
<wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> return send_len;</wbr>
}
recv_len = usb_control_msg(Device_handle,
USB_ENDPOINT_IN + USB_TYPE_CLASS + USB_RECIP_INTERFACE,
<wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> HID_REPORT_GET,</wbr>
<wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> 0x300,</wbr>
<wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> 0,</wbr>
<wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> recv_data, 72, USB_TIMEOUT);</wbr>
<wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr></wbr>
<wbr> </wbr><wbr> </wbr><wbr> if (recv_len < 0) {</wbr>
<wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> printf("failed to retrieve report from USB device!\n");</wbr>
<wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> return READ_REPORT;</wbr>
<wbr> </wbr><wbr> </wbr><wbr> }</wbr>
<wbr> </wbr><wbr> </wbr><wbr></wbr>
<wbr> </wbr><wbr> </wbr><wbr> if (recv_len != 72) {</wbr>
<wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> return recv_len;</wbr>
<wbr> </wbr><wbr> </wbr><wbr> }</wbr>
<wbr> </wbr><wbr> </wbr><wbr></wbr>
<wbr> </wbr><wbr> </wbr><wbr></wbr>
<wbr> </wbr><wbr> </wbr><wbr></wbr>
<wbr> </wbr><wbr> </wbr><wbr> usb_release_interface(RY2_handle, 0);</wbr>
<wbr> </wbr><wbr> </wbr><wbr> memcpy(buffer72, recv_data, 72);</wbr>
return SUCCESS;
}
3.4 关闭设备
假设我们定义的关闭设备的函数名是Device_Close()
int Device_Close()
{
<wbr> </wbr><wbr> </wbr><wbr> int handle;</wbr>
<wbr> </wbr><wbr> </wbr><wbr></wbr>
handle = open(“hiddev0”, O_RDONLY);
close( handle );
}
int Device_Close()
{
struct usb_device* <wbr> </wbr><wbr> </wbr><wbr> udev;</wbr>
usb_dev_handle* <wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> device_handle;</wbr>
device_handle = usb_open(udev);
usb_close(device_handle);
}
libusb的驱动框架
前面我们看了些主要的libusb函数的使用,这里我们把前面的内容归纳下:
一般的驱动应该都包含如下接口:
Device_Find();
Device_Open();
Device_Write();
Device_Read();
Device_Close();
具体代码如下:
#include <usb.h>
typedef struct
{
<wbr> </wbr><wbr> </wbr><wbr> struct usb_device* </wbr><wbr> </wbr><wbr> </wbr><wbr> udev;</wbr>
<wbr> </wbr><wbr> </wbr><wbr> usb_dev_handle* </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> device_handle;</wbr>
<wbr> </wbr><wbr> </wbr><wbr></wbr>
} device_descript;
#define USB_TIMEOUT <wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> 10000</wbr>
#define VENDOR_ID <wbr> </wbr><wbr> </wbr><wbr> 0xffff </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr></wbr>
#define PRODUCT_ID <wbr> </wbr><wbr> 0xffff</wbr>
#define DEVICE_MINOR 16
int <wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> g_num;</wbr>
device_descript g_list[ DEVICE_MINOR ];
int Device_Find()
{
<wbr> </wbr><wbr> </wbr><wbr> struct usb_bus </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> *bus;</wbr>
<wbr> </wbr><wbr> </wbr><wbr> struct usb_device *dev;</wbr>
<wbr> </wbr><wbr> </wbr><wbr> g_num = 0;</wbr>
<wbr> </wbr><wbr> </wbr><wbr> usb_find_busses();</wbr>
<wbr> </wbr><wbr> </wbr><wbr> usb_find_devices();</wbr>
<wbr> </wbr><wbr> </wbr><wbr></wbr>
<wbr> </wbr><wbr> </wbr><wbr></wbr>
<wbr> </wbr><wbr> </wbr><wbr> for (bus = usb_busses; bus; bus = bus->next) {</wbr>
<wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> for (dev = bus->devices; dev; dev = dev->next) {</wbr>
if(dev->descriptor.idVendor==VENDOR_ID&& dev->descriptor.idProduct == PRODUCT_ID) {
<wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr></wbr>
<wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> if (g_num < DEVICE_MINOR) {</wbr>
<wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> g_list[g_num].udev = dev; </wbr><wbr> </wbr><wbr></wbr>
<wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> g_num ++;</wbr>
<wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> } </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr></wbr>
<wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> } </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr></wbr>
<wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> }</wbr>
<wbr> </wbr><wbr> </wbr><wbr> }</wbr>
<wbr> </wbr><wbr> </wbr><wbr></wbr>
<wbr> </wbr><wbr> </wbr><wbr> return g_num;</wbr>
}
int Device_Open()
{
<wbr> </wbr><wbr> </wbr><wbr></wbr>
<wbr> </wbr><wbr> </wbr><wbr> if(g_list[g_num].udev != NULL) {
</wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> g_list[g_num].device_handle = usb_open(g_list[g_num].udev);</wbr>
}
}
int DeviceWite(int handle)
{
<wbr> </wbr><wbr> </wbr><wbr></wbr>
}
int DeviceOpen(int handle)
{
<wbr> </wbr><wbr> </wbr><wbr></wbr>
}
void Device_close(int handle)
{
<wbr> </wbr><wbr> </wbr><wbr></wbr>
}
</wbr>
相关推荐
这篇内容将深入探讨如何在Qt环境中利用libusb库进行USB通信。 首先,理解libusb库的基础是至关重要的。libusb提供了对USB规范的直接访问,支持多种操作系统,包括Linux、Windows和macOS。它提供了设备枚举、配置...
最后,通过`System.loadLibrary("libusb")`加载编译好的libusb库。 8. **测试与调试**:编写Android应用的USB功能,连接USB设备并测试你的libusb接口是否正常工作。如果遇到问题,可以使用NDK的日志输出功能进行...
"examples"目录一般包含使用LIBUSB库的示例代码,这对于初学者理解和学习如何使用库非常有帮助。通过这些例子,开发者可以了解如何打开USB设备、发送和接收数据,以及处理USB相关的错误。 在Ubuntu上进行USB驱动...
在本文中,我们将深入探讨如何使用libusb库文件在VC++6.0环境下实现USB通信。libusb是一个跨平台的库,允许用户访问USB设备,而无需依赖操作系统特定的API。我们将专注于使用libusb与HID(Human Interface Device)...
**libusb库源代码详解** libusb库是一个开源的、跨平台的库,它提供了与USB设备进行底层交互的能力。这个库的主要目的是为了让开发者能够轻松地编写与USB设备通信的应用程序,而无需依赖操作系统特定的API。libusb...
通过上述步骤,我们可以成功地在VC++6.0环境中使用libUsb库实现HID USB设备的通讯功能。这一过程涵盖了环境搭建、代码编写、设备交互等多个方面,为开发者提供了一个完整的参考案例。需要注意的是,在实际开发过程中...
7. **CSLibUsb**: 这可能是C#版的LIBUSB库源代码,实现了与原始LIBUSB库的接口,使.NET开发者能够方便地在C#中使用USB设备。 8. **Test_Info**: 这个目录可能包含了一些用于获取和测试USB设备信息的代码,例如设备...
本文将深入探讨“qt libusb U盘通信”这一主题,讲解如何使用Qt作为用户界面库,结合开源的libusb库来实现与U盘通过USB接口的通信,并在Linux环境下进行操作。 首先,Qt是一个跨平台的应用程序开发框架,广泛应用于...
libusb库是一个开源的库,专门用于在各种操作系统上实现USB设备通信,包括Windows、Linux、macOS等。它提供了一套跨平台的API,使得开发者可以编写与硬件底层细节无关的USB应用程序,大大简化了USB设备驱动的开发...
5. **示例代码**:libusb库通常会包含演示如何使用库功能的示例代码(demo)。这些示例可以帮助开发者快速理解如何初始化USB上下文、枚举设备、打开设备句柄、发送和接收数据等基本操作。 **libusb0的历史与差异** ...
【标题】基于USB2.0的libusb开源库在上位机程序中的应用 USB2.0是一种高速接口标准,广泛应用于数据传输和设备连接...通过深入理解libusb库的使用,开发者可以灵活地构建自己的USB设备应用,提升产品功能和用户体验。
libusb库的使用涵盖了许多关键知识点,以下将详细介绍其核心概念、安装、配置、设备发现、数据传输以及常见问题。 1. **核心概念** - **设备描述符**:每个USB设备都有一个设备描述符,包含了设备的基本信息,如...
libusb-compat的主要目标是为那些设计为使用旧版libusb(如0.1.x系列)的应用程序提供一个向后兼容的接口,同时利用libusb库的新功能。这样,开发者就可以在不修改大量代码的情况下,使他们的应用能够运行在支持...
这样,你的应用程序就能找到并使用libusb1.0.23库。 总结来说,要在Ubuntu中成功安装和使用libusb1.0.23,你需要完成下载、编译、安装的过程,并处理好权限问题,通过udev规则或临时提升权限使QT Creator能够访问...
为了深入理解并实现这个项目,需要熟悉C语言编程,掌握LIBUSB库的使用,了解USB协议的基本原理,以及可能的OpenGL知识。如果项目确实包含了对Kinect的支持,还需要理解其特定的协议和数据格式。开发过程中,调试和...
在Android平台上进行libusb编译是一项关键任务,尤其对于...通过以上步骤,你可以在Android平台上成功编译和使用libusb库。这将为你提供底层USB通信的能力,实现与硬件的直接交互,从而开发出更多定制化的应用程序。
在Windows平台上,libusb库通过安装驱动程序(如libusb-win32或 zadig)来模拟设备驱动,使得用户空间的应用程序可以直接访问USB设备。 首先,你需要下载libusb的源代码或预编译库,并将其集成到Qt项目中。通常,这...
标题“libusb_example_libusb_USB通信_”指的是一个使用libusb库进行USB通信的示例项目,其中可能包含C(cdc_example.c)和Python(cdc_example.py)两种编程语言的实现。libusb是一个跨平台的开源库,允许程序员在...
标题中的“libUSB_RFWRITER用libusb 读写usb设备”意味着libUSB_RFWRITER程序使用libusb库来实现对USB设备的数据读取和写入操作。这通常涉及到识别USB设备的VID(Vendor ID,供应商ID)和PID(Product ID,产品ID)...
libusb-1.0.20是libusb库的一个特定版本,而libusb-1.0.dll则是该库在Windows平台上的动态链接库文件,用于应用程序在运行时加载和调用libusb的功能。libusb-1.0.dll是libusb-1.0.20的一部分,包含了库的主要实现...