【问题标题】:.Net Core 2.1 GPIO filesystemwatcher on Raspberry Pi树莓派上的 .Net Core 2.1 GPIO 文件系统观察器
【发布时间】:2019-08-27 09:31:41
【问题描述】:

我为我的树莓派编写了一个 .Net Core 2.1 应用程序,但是当通过按钮将 GPIO 驱动为高/低时,filesystemwatcher 无法调用事件处理程序时遇到问题。

static void Main(string[] args)
{
    Console.WriteLine("Hello World!");

    GPIO.PinMode(21, GPIO.Direction.Input, GPIO.Edge.Both);


    var fileSystemWatcher = new FileSystemWatcher();

    // Associate event handlers with the events
    fileSystemWatcher.Created += FileSystemWatcher_Created;
    fileSystemWatcher.Changed += FileSystemWatcher_Changed;
    fileSystemWatcher.Deleted += FileSystemWatcher_Deleted;
    fileSystemWatcher.Renamed += FileSystemWatcher_Renamed;

    // tell the watcher where to look
    fileSystemWatcher.Path = "/sys/devices/platform/soc/3f200000.gpio/gpiochip0/gpio/gpio21";

    // You must add this line - this allows events to fire.
    fileSystemWatcher.EnableRaisingEvents = true;


    Console.ReadLine();

}

private static void FileSystemWatcher_Renamed(object sender, RenamedEventArgs e)
{
    Console.WriteLine($"A new file has been renamed from {e.OldName} to {e.Name}");
}

private static void FileSystemWatcher_Deleted(object sender, FileSystemEventArgs e)
{
    Console.WriteLine($"A new file has been deleted - {e.Name}");
}

private static void FileSystemWatcher_Changed(object sender, FileSystemEventArgs e)
{
    Console.WriteLine($"A new file has been changed - {e.Name}");
}

private static void FileSystemWatcher_Created(object sender, FileSystemEventArgs e)
{
    Console.WriteLine($"A new file has been created - {e.Name}");
}

我知道文件系统监视程序此时在符号链接上不起作用,因此使用完整的系统路径。

当 GPIO 21 物理改变状态时,基本上不会调用任何事件。

我已确认该值正在更改使用:

cat /sys/devices/platform/soc/3f200000.gpio/gpiochip0/gpio/gpio21/value

但是,当我编辑该目录中的文件时,确实会调用上述事件,例如当我从另一个 bash 终端发出命令时:

echo out > /sys/devices/platform/soc/3f200000.gpio/gpiochip0/gpio/gpio21/direction

我从正在运行的程序中获得:

A new file has been changed - direction

那么有没有人通过 GPIO 输入来调用文件系统观察器事件?

【问题讨论】:

    标签: c# raspberry-pi .net-core


    【解决方案1】:

    首先是的,.NET Core FileSystemWatcher 似乎不支持检查 Linux sysfs 中的更改,我对“/dev/input”中的条目尝试了相同的操作并得到了相同的结果。

    其次,对于您的问题,GPIO 控制,使用这个 .NET Foundation 项目: https://github.com/dotnet/iot

    dotnet/iot 由 Microsoft 员工和 .NET 基金会社区维护,应成为 .NET Core 中 GPIO、PWM、i2c 控制器的默认 API ...

    开始在 Linux 上使用 libgpiod,dotnet/iot 已经有了 LibGpiodDriver。 /sys/class/gpio 已弃用,控制这些 sysfs 的驱动程序将于 2020 年从 Linux 内核中删除:https://www.kernel.org/doc/Documentation/ABI/obsolete/sysfs-gpio

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多