代码之家  ›  专栏  ›  技术社区  ›  Peter

Dotnet Core WebApp没有GPIO访问权限

  •  0
  • Peter  · 技术社区  · 6 年前

    我正在尝试让一个aspnet核心web应用程序在我的树莓pi上运行。应用程序使用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中的webserver之前,我会立即将gpio 17调高以发出哔哔声。

    如何确保dotnet有足够的权限访问gpio?

    1 回复  |  直到 6 年前
        1
  •  0
  •   Peter    6 年前

    好吧,和往常一样,在你花时间发布问题之后,你会在某个地方找到答案…

    未经授权的异常具有严重的误导性,因为缺少的是C中的以下声明: pin.SetDriveMode(GpioPinDriveMode.Output);

    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);
        }
    }