首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >dotnet核心webapp没有gpio访问权限

dotnet核心webapp没有gpio访问权限
EN

Stack Overflow用户
提问于 2018-12-01 20:48:33
回答 1查看 305关注 0票数 1

我正在尝试让一个aspnet核心get应用程序在我的树莓派上运行。这个应用程序使用Bifrost与树莓的GPIO进行交互,但是每次我执行sudo dotnet run时,它都会失败,并显示以下错误

代码语言:javascript
复制
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?

EN

回答 1

Stack Overflow用户

发布于 2018-12-01 21:22:26

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

pin.SetDriveMode(GpioPinDriveMode.Output);异常具有极大的误导性,因为遗漏了c#中的以下语句:

代码语言:javascript
复制
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);
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53570924

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档