linuxHID设备读写
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);
}
//
port_type=HID_REPORT_TYPE_OUTPUT;
port_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++)
{
port_port_type;
port_port_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++)
//{
port_port_type;
port_port_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",
port_type, port_id, uref.field_index, uref.usage_index, uref.usage_code);
if(ret != 0)
{
printf("ioctl four failed\n");
close(fd);
return -1;
}
printf("\n");
//}
port_type=HID_REPORT_TYPE_OUTPUT;
port_port_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", port_id,port_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;
}
}
port_type=HID_REPORT_TYPE_INPUT;
port_id=HID_REPORT_ID_FIRST;
for(i=0;i<rinfo.num_fields;i++)
{
printf("num_field=%d\n",rinfo.num_fields);
port_port_type;
port_port_id;
finfo.field_index=i;
printf("port_id:%d\n",port_id);
ret =ioctl(fd,HIDIOCGFIELDINFO,&finfo);
if(ret !=0)
{
printf("ioctl five faile!\n");
close(fd);
return -1;
}
else
{
printf("port_id:%d\n",port_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++)
printf("%3x",urefs.values[i]);
ret=ioctl(fd,HIDIOCGUSAGES,&urefs);//接收数据,程序运行正确
printf("ret = %d\n",ret);linux怎么读光盘
for(i=0;i<21;i++)//打印接收的值,运行后结果没有变,还是bufw的值
{
printf("%3x",urefs.values[i]);
}
printf("\n");
close(fd);
return 0;
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论