【发布时间】: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