【问题标题】:GPIO monitoring with select带选择的 GPIO 监控
【发布时间】:2013-01-12 06:24:02
【问题描述】:

我正在尝试监控 GPIO 引脚,根据 Linux 文档,我应该能够通过使用 select 监控 /sys/class/gpio/gpio##/value 文件来做到这一点:

"value" ... reads as either 0 (low) or 1 (high).  If the GPIO
    is configured as an output, this value may be written;
    any nonzero value is treated as high.

    If the pin can be configured as interrupt-generating interrupt
    and if it has been configured to generate interrupts (see the
    description of "edge"), you can poll(2) on that file and
    poll(2) will return whenever the interrupt was triggered. If
    you use poll(2), set the events POLLPRI and POLLERR. If you
    use select(2), set the file descriptor in exceptfds. After
    poll(2) returns, either lseek(2) to the beginning of the sysfs
    file and read the new value or close the file and re-open it

我正在尝试在 Ruby 中执行此操作,并且根据 IO.Select documentation 它调用 select(2)

因此,有了这些知识,我将以下测试程序组合在一起:

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 块并显示引脚的当前值,然后每 5 秒打印一次timeout

我是否读错了文档? select不应该可以监控吗?

【问题讨论】:

    标签: ruby linux gpio


    【解决方案1】:

    select 正确触发 GPIO 引脚之前,您需要设置引脚的边沿触发。来自the GPIO docs

    "edge" ... reads as either "none", "rising", "falling", or
        "both". Write these strings to select the signal edge(s)
        that will make poll(2) on the "value" file return.
    
        This file exists only if the pin can be configured as an
        interrupt generating input pin.
    

    简单地在 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
    

    【讨论】:

      猜你喜欢
      • 2012-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-20
      • 2014-05-23
      • 1970-01-01
      • 1970-01-01
      • 2011-02-02
      相关资源
      最近更新 更多