当前位置:网站首页>韦东山设备信息查询例程学习
韦东山设备信息查询例程学习
2022-06-23 10:20:00 【stone_322】
1.查询 阻塞机制
#include <linux/input.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <stdio.h>
int main(int argc, char **argv)
{
int fd;
int ret;
struct input_id id;
int len;
unsigned int evbit[2];
unsigned char byte;
int i;
int bit;
char read_buff[100] = {
'\0'};
struct input_event event;
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>\ne.g., %s /dev/input/event0\n", argv[0], argv[0]);
return -1;
}
fd = open(argv[1], O_RDWR ); /*| O_NONBLOCK*/
if (fd < 0)
{
return -1;
}
ret = ioctl(fd, EVIOCGID, &id);
if (ret == 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)
{
printf("support ev type:");
for (i = 0; i < len; i++)
{
byte = ((unsigned char *)evbit)[i];
for (bit = 0; bit < 8; bit++)
{
if (byte & (1 << bit))
{
printf("%s ", ev_names[i * 8 + bit]);
}
}
}
printf("\n");
}
while (1)
{
len = read(fd, &event, sizeof(event));
if (len == sizeof(event))
{
printf("get event: type = 0x%x, code = 0x%x, value = 0x%x\n", event.type, event.code, event.value);
}
else
{
printf("read err.\n");
}
}
return 0;
}
2.poll机制
#include <linux/input.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <poll.h>
int main(int argc, char **argv)
{
int fd;
int ret;
struct input_event event;
struct pollfd fds[1];
nfds_t nfds = 1;
if (argc != 2)
{
printf("Usage: %s <dev>\ne.g., %s /dev/input/event0\n", argv[0], argv[0]);
return -1;
}
fd = open(argv[1], O_RDWR ); /*| O_NONBLOCK*/
if (fd < 0)
{
return -1;
}
fds[0].fd = fd;
fds[0].events = POLLIN;
while (1)
{
fds[0].revents = 0;
ret = poll(fds, nfds, 3000);
if (ret > 0)
{
if (fds[0].revents == POLLIN)
{
while (read(fd, &event, sizeof(event)) == sizeof(event))
{
printf("get event: type = 0x%x, code = 0x%x, value = 0x%x\n", event.type, event.code, event.value);
}
}
else
{
printf("read error.\n");
}
}
else if(ret == 0)
{
printf("time out\n");
}
else
{
printf("poll err.\n");
}
}
return 0;
}
3.select机制
#include <linux/input.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/select.h>
/* According to earlier standards */
#include <sys/time.h>
int main(int argc, char **argv)
{
int fd;
int ret;
struct input_event event;
fd_set readfd;
unsigned int nfds = 1;
struct timeval time_val;
if (argc != 2)
{
printf("Usage: %s <dev>\ne.g., %s /dev/input/event0\n", argv[0], argv[0]);
return -1;
}
fd = open(argv[1], O_RDWR | O_NONBLOCK);
if (fd < 0)
{
return -1;
}
nfds = fd +1;
FD_ZERO(&readfd);
while (1)
{
time_val.tv_sec = 3;
time_val.tv_usec = 0;
FD_SET(fd, &readfd);
ret = select(nfds, &readfd, NULL, NULL, &time_val);
if (ret > 0)
{
if (FD_ISSET(fd, &readfd))
{
while (read(fd, &event, sizeof(event)) == sizeof(event))
{
printf("get event: type = 0x%x, code = 0x%x, value = 0x%x\n", event.type, event.code, event.value);
}
}
else
{
printf("read error.\n");
}
}
else if(ret == 0)
{
printf("time out\n");
}
else
{
printf("select err.\n");
}
}
return 0;
}
poll和select在open的时候,要使用NONBLOCK, 即非阻塞。原因可以查看这里。
4.异步机制
#include <linux/input.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <fcntl.h>
int fd;
void my_sig_process(int sig)
{
struct input_event event;
while (read(fd, &event, sizeof(event)) == sizeof(event))
{
printf("get event: type = 0x%x, code = 0x%x, value = 0x%x\n", event.type, event.code, event.value);
}
}
int main(int argc, char **argv)
{
int ret = 0;
int cnt = 0;
if (argc != 2)
{
printf("Usage: %s <dev>\ne.g., %s /dev/input/event0\n", argv[0], argv[0]);
return -1;
}
fd = open(argv[1], O_RDWR | O_NONBLOCK);
if (fd < 0)
{
return -1;
}
signal(SIGIO, my_sig_process);
fcntl(fd, F_SETOWN, getpid());
ret = fcntl(fd, F_GETFL);
fcntl(fd, F_SETFL, ret | FASYNC);
while (1)
{
printf("main loop count = %d\n", cnt++);
sleep(1);
}
return 0;
}
边栏推荐
- Lying trough, the most amazing paper artifact!
- NOI OJ 1.3 15:苹果和虫子 C语言
- Build a security video monitoring platform using Huawei cloud ECS server
- 炫酷相册代码,祝对象生日快乐!
- Shengshihaotong enables high-quality development with industrial Digitalization
- NOI OJ 1.3 17:计算三角形面积 C语言
- 天龙八部TLBB系列 - 网单服务端各目录文件说明【超详细】
- Noi OJ 1.2 06: round floating point numbers to zero
- 2021-04-16数组
- Golang 快速上手 (1)
猜你喜欢

Build a security video monitoring platform using Huawei cloud ECS server

马斯克 18 岁儿子请愿改名,欲断绝父子关系

安装typescript环境并开启VSCode自动监视编译ts文件为js文件

Personal blog system graduation project opening report

RT thread add MSH command
![[software and system security] heap overflow](/img/ca/1b98bcdf006f90cabf3e90e416f7f2.png)
[software and system security] heap overflow

JVM easy start-02

STM32F1与STM32CubeIDE编程实例-红外寻迹传感器驱动

Mysql 的Innodb引擎和Myisam数据结构和区别

SQL create a new field based on the comparison date
随机推荐
Unity技术手册 - 生命周期内速度限制(Limit Velocity Over Lifetime)子模块和速度继承(Inherit Velocity)子模块
Step by step introduction to sqlsugar based development framework (9) -- Realizing field permission control with WinForm control
Pycharm installation tutorial, super detailed
On shore experience of Chang'an University majoring in transportation in 2023
一个优秀速开发框架是什么样的?
Five SQL functions for operation date that must be known in SQL tutorial
SQL教程之 5 个必须知道的用于操作日期的 SQL 函数
Golang 快速上手 (1)
NOI OJ 1.3 05:计算分数的浮点数值 C语言
MySQL-02. Understanding of indexes at work
Tencent tangdaosheng: practice "science and technology for the good" and promote sustainable social value innovation
搭建一个QQ机器人叫女友起床
How to pass values to onclick events in thymeleaf
文献综述怎么写 ,一直没头绪写不出来怎么办?
NOI OJ 1.3 20:计算2的幂 C语言
NOI OJ 1.3 15:苹果和虫子 C语言
2021-04-16 method overload parameter transfer
NOI OJ 1.3 16:计算线段长度 C语言
Musk's 18-year-old son petitioned to change his name to sever the father son relationship
NOI OJ 1.2 06:浮点数向零舍入