是否可以在/sys/class/gpio/gpioX设备树中的文件上设置JAVA NIO WatchService 来监督那里的更改?
最好直接从Java中检测GPIO文件(即GPIO输入)的变化,但我担心这是不受支持的.
确认它不受支持(也许是为什么)就足够了.
我搜索了与inotify相关的问题,这个问题有点不同......
我使用以下代码来监视一个文件(而不是目录)的更改.在测试中,当我保存目标文件时,read()会返回,这意味着它可以工作.但是event-> mask是32768,它不是IN_MODIFY,名称是空的.另一个问题:它无法持续监控.当我第二次更改文件时,它没有响应.感谢您的帮助!
#include <sys/inotify.h>
#include <unistd.h>
#include <stdio.h>
#define EVENT_SIZE (sizeof (struct inotify_event))
#define BUF_LEN (16 * (EVENT_SIZE + 16))
int main()
{
int fd;
fd = inotify_init();
if (fd < 0)
perror("inotify_init()");
int wd;
wd = inotify_add_watch(fd, "target.txt", IN_MODIFY);
if (wd < 0)
perror("inotify_add_watch");
char buf[BUF_LEN];
int len;
start:
len = read(fd, buf, BUF_LEN);
if (len > 0)
{
int i = 0;
while (i < len)
{
struct inotify_event *event;
event = (struct inotify_event *) &buf[i];
printf("wd=%d …Run Code Online (Sandbox Code Playgroud)