beaglebone黑色gpioselect不工作

我试图检测一个GPIO引脚从低到高,并遇到麻烦。 从我读过的,我应该能够configuration引脚作为input这种方式:

# echo in > /sys/class/gpio/gpio51/direction # echo rising > /sys/class/gpio/gpio51/edge 

接下来我尝试运行使用select等待上升沿的ac程序。 代码看起来像这样(注意我只是试图读取文件,因为如果你不设置O_NONBLOCK,读取应该被阻塞):

 #include<stdio.h> #include<fcntl.h> #include <sys/select.h> int main(void) { int fd = open("/sys/class/gpio/gpio51/value", O_RDONLY & ~O_NONBLOCK); //int fd = open("/sys/class/gpio/gpio51/value", O_RDONLY | O_NONBLOCK); //unsigned char buf[2]; //int x = read(fd, &buf, 2); //printf("%d %d: %s\n", fd, x, buf); fd_set exceptfds; int res; FD_ZERO(&exceptfds); FD_SET(fd, &exceptfds); //printf("waiting for %d: %s\n", exceptfds); res = select(fd+1, NULL, // readfds - not needed NULL, // writefds - not needed &exceptfds, NULL); // timeout (never) if (res > 0 && FD_ISSET(fd, &exceptfds)) { printf("finished\n"); } return 0; } 

无论引脚的状态如何(高电平或低电平),程序立即退出。 任何人都可以看到我这样做的方式错了吗?

PS。 我有一个python库,使用poll()来做到这一点,和python的预期。 我把引脚拉低,调用python,它阻塞,把引脚拉高,代码继续。 所以我不认为这是与Linux的GPIO驱动程序的问题。

https://bitbucket.org/cswank/gadgets/src/590504d4a30b8a83143e06c44b1c32207339c097/gadgets/io/poller.py?at=master

我想到了。 在select调用返回之前,必须从文件描述符中读取。 这是一个例子:

 #include<stdio.h> #include<fcntl.h> #include <sys/select.h> #define MAX_BUF 64 int main(void) { int len; char *buf[MAX_BUF]; int fd = open("/sys/class/gpio/gpio51/value", O_RDONLY); fd_set exceptfds; int res; FD_ZERO(&exceptfds); FD_SET(fd, &exceptfds); len = read(fd, buf, MAX_BUF); //won't work without this read. res = select(fd+1, NULL, // readfds - not needed NULL, // writefds - not needed &exceptfds, NULL); // timeout (never) if (res > 0 && FD_ISSET(fd, &exceptfds)) { printf("finished\n"); } return 0; }