Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 使用select进行GPIO监控_Ruby_Linux_Gpio - Fatal编程技术网

Ruby 使用select进行GPIO监控

Ruby 使用select进行GPIO监控,ruby,linux,gpio,Ruby,Linux,Gpio,我正在尝试监视GPIO pin,根据Linux文档,我应该能够通过使用select监视/sys/class/GPIO/GPIO/value文件来实现这一点: 我正试图在Ruby中实现这一点,根据它调用select2 因此,有了这些知识,我制定了以下测试计划: fd = File.open("/sys/class/gpio/gpio17/value", "r") loop do rs,ws,es = IO.select(nil, nil, [fd], 5) if es r = e

我正在尝试监视GPIO pin,根据Linux文档,我应该能够通过使用select监视/sys/class/GPIO/GPIO/value文件来实现这一点:

我正试图在Ruby中实现这一点,根据它调用select2

因此,有了这些知识,我制定了以下测试计划:

fd = File.open("/sys/class/gpio/gpio17/value", "r")

loop do
  rs,ws,es = IO.select(nil, nil, [fd], 5)
  if es
    r = es[0]
    puts r.read(1)
  else
    puts "timeout"
  end
end
但是,它没有检测到任何管脚变化。当我启动这个应用程序时,它会立即进入if块并显示pin的当前值,然后每5秒打印一次超时

我看错文件了吗?select无法监视此情况吗?

在select将正确触发GPIO引脚之前,您需要设置引脚的边缘触发器。发件人:

在Ruby中,简单地说:

File.open("/sys/class/gpio/gpio17/edge", "w") { |f| f.write("both") }
上面的完整示例如下所示:

fd = File.open("/sys/class/gpio/gpio17/value", "r")
File.open("/sys/class/gpio/gpio17/edge", "w") { |f| f.write("both") }

loop do
  rs,ws,es = IO.select(nil, nil, [fd], 5)
  if es
    r = es[0]
    puts r.read(1)
  else
    puts "timeout"
  end
end
File.open("/sys/class/gpio/gpio17/edge", "w") { |f| f.write("both") }
fd = File.open("/sys/class/gpio/gpio17/value", "r")
File.open("/sys/class/gpio/gpio17/edge", "w") { |f| f.write("both") }

loop do
  rs,ws,es = IO.select(nil, nil, [fd], 5)
  if es
    r = es[0]
    puts r.read(1)
  else
    puts "timeout"
  end
end