Raspberry Pi上的.Net Core 2.1 GPIO filesystemwatcher

Law*_*Lee 5 c# raspberry-pi .net-core

我已经为我的树莓派编写了.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}");
}
Run Code Online (Sandbox Code Playgroud)

我知道filesystemwatcher目前在符号链接上不起作用,因此使用了完整的系统路径。

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

我已经确认该值正在使用以下方法进行更改:

cat /sys/devices/platform/soc/3f200000.gpio/gpiochip0/gpio/gpio21/value
Run Code Online (Sandbox Code Playgroud)

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

echo out > /sys/devices/platform/soc/3f200000.gpio/gpiochip0/gpio/gpio21/direction
Run Code Online (Sandbox Code Playgroud)

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

A new file has been changed - direction
Run Code Online (Sandbox Code Playgroud)

那么,有没有人得到GPIO输入来调用filesystemwatcher事件?

Mat*_*llo 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