前言
本篇文章我们将介绍到如何使用open,ioctl函数读取设备信息。
一、open函数
使用man手册查看到open函数的使用方法。
二、ioctl
使用man手册查看到ioctl函数的使用方法。
三、总体代码编写
#include <linux/input.h> #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <sys/ioctl.h> /* 01_get_input_info /dev/input/event0 */ int main(int argc,char **argv) { int fd; int err; struct input_id id; int len; int byte; int bit; int i; unsigned char evbit[2]; char *ev_names[] = { "EV_SYN ", "EV_KEY ", "EV_REL ", "EV_ABS ", "EV_MSC ", "EV_SW ", "NULL ", "NULL ", "NULL ", "NULL ", "NULL ", "NULL ", "NULL ", "NULL ", "NULL ", "NULL ", "NULL ", "EV_LED ", "EV_SND ", "NULL ", "EV_REP ", "EV_FF ", "EV_PWR ", }; if(argc!=2) { printf("Usage :%s <dev> \n",argv[0]); return -1; } fd=open(argv[1],O_RDWR); if(fd<0) { printf("open %s err\n",argv[1]); return -1; } err=ioctl(fd,EVIOCGID,&id); if(err==0) { printf("bustype = 0x%x\n", id.bustype ); printf("vendor = 0x%x\n", id.vendor ); printf("product = 0x%x\n", id.product ); printf("version = 0x%x\n", id.version ); } len=ioctl(fd,EVIOCGBIT(0, sizeof(evbit)),&evbit); if(len>0&&len<=sizeof(evbit)) { printf("support dev:"); for(i=0;i<len;i++) { byte=evbit[i]; for(bit=0;bit<8;bit++) { if(byte&(1<<bit)) { printf("%s ", ev_names[i*8 + bit]); } } } printf("\n"); } return 0; }
总结
大家可以下去实验一下。