繁体   English   中英

dotnet core webapp没有gpio访问

[英]dotnet core webapp doesn't have gpio access

我正在尝试使aspnet核心web应用程序在我的树莓派上运行。 该应用程序使用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中启动Web服务器之前,立即将GPIO 17设置为高电平以发出蜂鸣声。

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

好的,像往常一样,在花了一些时间发布问题之后,您可以在某处找到答案...

未经授权的pin.SetDriveMode(GpioPinDriveMode.Output);非常令人误解,因为在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);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM