Linux读取设备信息代码编写

举报
yd_274589494 发表于 2023/07/25 14:16:49 2023/07/25
【摘要】 @TOC 前言本篇文章我们将介绍到如何使用open,ioctl函数读取设备信息。 一、open函数使用man手册查看到open函数的使用方法。 二、ioctl使用man手册查看到ioctl函数的使用方法。 三、总体代码编写#include <linux/input.h>#include <stdio.h>#include <sys/types.h>#include <sys/stat.h>#...

@TOC


前言

本篇文章我们将介绍到如何使用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;
}

总结

大家可以下去实验一下。

【声明】本内容来自华为云开发者社区博主,不代表华为云及华为云开发者社区的观点和立场。转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息,否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。