我正在尝试让一个aspnet核心get应用程序在我的树莓派上运行。这个应用程序使用Bifrost与树莓的GPIO进行交互,但是每次我执行sudo dotnet run时,它都会失败,并显示以下错误
Using launch settings from /home/pi/Desktop/keypad-alarm/keypad-alarm-server/Properties/launchSettings.json...
Write value High to pin at /sys/class/gpio/gpio17/value
Unhandled Exception: System.UnauthorizedAccessException: Access to the path '/sys/class/gpio/gpio17/value' is denied. ---> System.IO.IOException: Operation not permitted
--- End of inner exception stack trace --- at
System.IO.FileStream.WriteNative(ReadOnlySpan`1 source) at
System.IO.FileStream.FlushWriteBuffer() at
System.IO.FileStream.FlushInternalBuffer() at
System.IO.FileStream.Flush(Boolean flushToDisk) at
System.IO.FileStream.Flush() at
System.IO.StreamWriter.Flush(Boolean flushStream, Boolean flushEncoder) at
System.IO.StreamWriter.Dispose(Boolean disposing) at
System.IO.TextWriter.Dispose()
at System.IO.File.WriteAllText(String path, String contents) at
Bifrost.Devices.Gpio.GpioPin.Write(GpioPinValue pinValue) at
keypad_alarm_server.TweetController.Tweet(Int32 milliseconds) in
/home/pi/Desktop/keypad-alarm/keypad-alarm-server/TweetController.cs:line 25 at
keypad_alarm_server.Program.Main(String[] args) in /home/pi/Desktop/keypad-alarm/keypad-alarm-server/Program.cs:line 15我应该提一下,在我启动Program.cs中的even服务器之前,我立即将GPIO17调到高电平以发出蜂鸣声。
如何确保dotnet有足够的权限访问gpio?
发布于 2018-12-01 21:22:26
好的,像往常一样,在你花时间发布问题后,你会在某个地方找到答案……
pin.SetDriveMode(GpioPinDriveMode.Output);异常具有极大的误导性,因为遗漏了c#中的以下语句:
public class TweetController
{
private static IGpioController _gpio = null;
private const int PIN = 17;
public TweetController()
{
_gpio = GpioController.Instance;
}
public void Tweet(int milliseconds){
var pin = _gpio.OpenPin(PIN);
pin.SetDriveMode(GpioPinDriveMode.Output); // open the gpio pin before writing or unauthorizedexception occurs
pin.Write(GpioPinValue.High);
Thread.Sleep(milliseconds);
pin.Write(GpioPinValue.Low);
}
}https://stackoverflow.com/questions/53570924
复制相似问题