- 论坛徽章:
- 0
|
以下这个程序能正常打开。- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <sys/ioctl.h>
- int main(int argc, char **argv)
- {
- int i;
- int ret;
- int fd;
- int press_cnt[4];
-
- fd = open("/dev/button", 0); // 打开设备
- if (fd < 0) {
- printf("Can't open /dev/button\n");
- return -1;
- }
- // 这是个无限循环,进程有可能在read函数中休眠,当有按键被按下时,它才返回
- while (1) {
- // 读出按键被按下的次数
- ret = read(fd, press_cnt, sizeof(press_cnt));
- if (ret < 0) {
- printf("read err!\n");
- continue;
- }
- for (i = 0; i < sizeof(press_cnt)/sizeof(press_cnt[0]); i++) {
- // 如果被按下的次数不为0,打印出来
- if (press_cnt)
- printf("K%d has been pressed %d times!\n", i+1, press_cnt);
- }
- }
- close(fd);
复制代码 下面这个程序一打开就提示:Device or resource busy
其代码如下:- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/select.h>
- #include <fcntl.h>
- #include <errno.h>
- int main(void)
- {
- int fd;
- int key_value;
-
- fd = open("/dev/button",0);
- if(fd < 0)
- {
- perror("Open device key");
- exit(1);
- }
- for(;;)
- {
- fd_set rds;
- int ret;
-
- FD_ZERO(&rds);
- FD_SET(fd,&rds);
-
- ret = select(fd + 1,&rds,NULL,NULL,NULL);
- if(ret < 0)
- {
- printf("select is erron!\n");
- }
- if(ret == 0)
- {
- printf("timeout.\n");
- }else if(FD_ISSET(fd,&rds))
- {
- ret = read(fd,&key_value,sizeof(key_value));
- if(ret != sizeof(key_value))
- {
- printf("read button is errno!\n");
- continue;
- }else
- {
- printf("button_value: %d\n",key_value);
- }
- }
- }
- close(fd);
- }
复制代码 为什么会出现这么奇怪的问题呢??还请各位大虾指点。 |
|