免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2439 | 回复: 4
打印 上一主题 下一主题

求高手指点!用两个测试程序打开同一个驱动,一个能正常打开,一个却提示设备繁忙? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-10-09 20:34 |只看该作者 |倒序浏览
以下这个程序能正常打开。
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <sys/ioctl.h>

  5. int main(int argc, char **argv)
  6. {
  7.     int i;
  8.     int ret;
  9.     int fd;
  10.     int press_cnt[4];
  11.    
  12.     fd = open("/dev/button", 0);  // 打开设备
  13.     if (fd < 0) {
  14.         printf("Can't open /dev/button\n");
  15.         return -1;
  16.     }

  17.     // 这是个无限循环,进程有可能在read函数中休眠,当有按键被按下时,它才返回
  18.     while (1) {
  19.         // 读出按键被按下的次数
  20.         ret = read(fd, press_cnt, sizeof(press_cnt));
  21.         if (ret < 0) {
  22.             printf("read err!\n");
  23.             continue;
  24.         }

  25.         for (i = 0; i < sizeof(press_cnt)/sizeof(press_cnt[0]); i++) {
  26.             // 如果被按下的次数不为0,打印出来
  27.             if (press_cnt)
  28.                 printf("K%d has been pressed %d times!\n", i+1, press_cnt);
  29.         }
  30.     }
  31.     close(fd);
复制代码
下面这个程序一打开就提示:Device or resource busy
其代码如下:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/select.h>
  4. #include <fcntl.h>
  5. #include <errno.h>

  6. int main(void)
  7. {
  8.         int fd;
  9.         int key_value;
  10.         
  11.         fd = open("/dev/button",0);
  12.         if(fd < 0)
  13.         {
  14.                 perror("Open device key");
  15.                 exit(1);
  16.         }
  17.         for(;;)
  18.         {
  19.                 fd_set rds;
  20.                 int ret;
  21.                
  22.                 FD_ZERO(&rds);
  23.                 FD_SET(fd,&rds);
  24.                
  25.                 ret = select(fd + 1,&rds,NULL,NULL,NULL);
  26.                 if(ret < 0)
  27.                 {
  28.                         printf("select is erron!\n");
  29.                 }
  30.                 if(ret == 0)
  31.                 {
  32.                         printf("timeout.\n");
  33.                 }else if(FD_ISSET(fd,&rds))
  34.                 {
  35.                         ret = read(fd,&key_value,sizeof(key_value));
  36.                         if(ret != sizeof(key_value))
  37.                         {
  38.                                 printf("read button is errno!\n");
  39.                                 continue;
  40.                         }else
  41.                         {
  42.                                 printf("button_value: %d\n",key_value);
  43.                         }
  44.                 }
  45.         }
  46.         close(fd);
  47. }
复制代码
为什么会出现这么奇怪的问题呢??还请各位大虾指点。

论坛徽章:
3
卯兔
日期:2013-08-26 22:14:57未羊
日期:2013-09-08 19:16:05未羊
日期:2014-10-23 10:34:12
2 [报告]
发表于 2010-10-10 12:59 |只看该作者

论坛徽章:
0
3 [报告]
发表于 2010-10-13 17:46 |只看该作者
估计是驱动程序里面设置了允许打开一次吧,改改就好了

论坛徽章:
0
4 [报告]
发表于 2010-10-14 18:07 |只看该作者
回复 3# star316


    应该不是~~而且即使是用第二个程序作第一次打开也不行...555~~~

论坛徽章:
0
5 [报告]
发表于 2010-10-15 09:17 |只看该作者
回复 4# 308534826

肯定是驱动的问题了,你去驱动的open函数里面找找为什么会返回busy, 看这个意思就是只能打开一个设备
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP