日期:2014-05-16  浏览次数:20879 次

linux下hid设备读写的问题
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <linux/hid.h>
#include <linux/hiddev.h>
int main(int argc,char **argv)
{
int fd;
int rtn,ret=0;
int i,j=0;
struct hiddev_report_info rinfo;
struct hiddev_usage_ref uref;
struct hiddev_field_info finfo;
struct hiddev_usage_ref_multi urefs;
struct hiddev_devinfo dinfo;
struct hiddev_collection_info cinfo;
unsigned char bufw[21]={0x02,0x08,0xce,0,0,0,0,0,0,0};
unsigned char recv[21]={0};
//printf("argc = %d\n",argc);
//打开设备文件
fd=open("/dev/usb/hiddev0",O_RDWR);
if(fd > 0)
{
printf("open hid success!,fd = %d\n",fd);
}
//
rinfo.report_type=HID_REPORT_TYPE_OUTPUT;
rinfo.report_id=HID_REPORT_ID_FIRST;
ret=ioctl(fd,HIDIOCGREPORTINFO,&rinfo);
if(ret != 0)
{
printf("ioctl one failed!\n");
close(fd);
return -1;
}
for(i=0;i<rinfo.num_fields;i++)
{
finfo.report_type=rinfo.report_type;
finfo.report_id=rinfo.report_id;
finfo.field_index=i;
ret = ioctl(fd,HIDIOCGFIELDINFO,&finfo);
printf("finfo.maxusage %d\n",finfo.maxusage);
if(ret != 0)
{
printf("ioctl two failed\n");
close(fd);
return -1;
}
//for(j=0;j<finfo.maxusage;j++)
//{
uref.report_type=finfo.report_type;
uref.report_id=finfo.report_id;
uref.field_index=i;
uref.usage_index=j;//ioctl(fd,HIDIOCGUCODE,&uref);
ret = ioctl(fd,HIDIOCGUSAGE,&uref);
if(ret != 0)
{
printf("ioctl three faile!\n");
close(fd);
return -1;
}
ret = ioctl(fd,HIDIOCGUCODE,&uref);
printf("usage.value:%x\n",uref.value);
printf("usage report_type %d report_id %d field_index %d usage_index %d usage_code %d\n",  
uref.report_type, uref.report_id, uref.field_index, uref.usage_index, uref.usage_code);
if(ret != 0)
{
printf("ioctl four failed\n");
close(fd);
return -1;
}
printf("\n");
//}
urefs.uref.report_type=HID_REPORT_TYPE_OUTPUT;
urefs.uref.report_id=finfo.report_id;
urefs.num_values=8;
urefs.uref.field_index=i;
urefs.uref.usage_index=0;
for(j=0;j<21;j++)
{
urefs.uref.usage_code+=4;
urefs.values[j]=bufw[j];
printf("usage report_type:%d,report_id:%d,field_index:%d,usage_index:%d,usage_code:%d\n", urefs.uref.report_id,urefs.uref.report_id,urefs.uref.field_index,urefs.uref.usage_index, urefs.uref.usage_code);
}
ret=ioctl(fd,HIDIOCSUSAGES,&urefs);//写入数据1
if(ret !=0)
{
printf("failed !\n");
close(fd);
return -1;
}
}
rinfo.report_type=HID_REPORT_TYPE_INPUT;
rinfo.report_id=HID_REPORT_ID_FIRST;
for(i=0;i<rinfo.num_fields;i++)
{
printf("num_field=%d\n",rinfo.num_fields);
finfo.report_type=rinfo.report_type;
finfo.report_id=rinfo.report_id;
finfo.field_index=i;
printf("finfo.report_id:%d\n",finfo.report_id);
ret =ioctl(fd,HIDIOCGFIELDINFO,&finfo);
if(ret !=0)
{
printf("ioctl five faile!\n");
close(fd);
return -1;
}
else
{
printf("finfo.report_id:%d\n",finfo.report_id);
}
}
for(i=0;i<21;i++)//打印结构体中是否有值。运行之后显示正确
{
printf("%3x",urefs.values[i]);
}
printf("\n");
ret=ioctl(fd,HIDIOCINITREPORT,0);
printf("ret = %d\n",ret);
memset(urefs.values,0,sizeof(urefs.values));//将结构体中用来存储数据的urefs.values置空
for(i=0;i<21;i++)