首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >c#单根覆盆子pi与Raspberry#获取操作无效

c#单根覆盆子pi与Raspberry#获取操作无效
EN

Stack Overflow用户
提问于 2014-08-31 13:44:36
回答 1查看 5.6K关注 0票数 3

我试图使用Raspberry#库来执行基本任务,在Raspberry#上使用GPIO引脚(on和off)。以github:https://github.com/raspberry-sharp/raspberry-sharp-io/wiki/Raspberry.IO.GeneralPurpose为例

代码:

代码语言:javascript
复制
        var led1 = ConnectorPin.P1Pin11.Output();
        var connection = new GpioConnection(led1);
        for (var i = 0; i < 100; i++)
        {
            connection.Toggle(led1);
            System.Threading.Thread.Sleep(250);
        }
        connection.Close();

在在线var connection = new GpioConnection(led1);上,我得到了异常:

“由于对象的当前状态,操作无效”

堆栈跟踪

代码语言:javascript
复制
Unhandled Exception:
System.InvalidOperationException: Operation is not valid due to the current state of the object
at Raspberry.IO.GeneralPurpose.GpioConnectionDriver..ctor () [0x00000] in <filename unknown>:0
at Raspberry.IO.GeneralPurpose.GpioConnectionSettings.get_DefaultDriver () [0x00000] in <filename unknown>:0
at Raspberry.IO.GeneralPurpose.GpioConnectionSettings..ctor () [0x00000] in <filename unknown>:0
at Raspberry.IO.GeneralPurpose.GpioConnection..ctor (Raspberry.IO.GeneralPurpose.GpioConnectionSettings settings, IEnumerable`1 pins) [0x00000] in <filename unknown>:0
at Raspberry.IO.GeneralPurpose.GpioConnection..ctor (Raspberry.IO.GeneralPurpose.PinConfiguration[] pins) [0x00000] in <filename unknown>:0
at Hello.Program.Main (System.String[] args) [0x00000] in <filename unknown>:0
[ERROR] FATAL UNHANDLED EXCEPTION: System.InvalidOperationException: Operation is not valid due to the current state of the object
at Raspberry.IO.GeneralPurpose.GpioConnectionDriver..ctor () [0x00000] in <filename unknown>:0
at Raspberry.IO.GeneralPurpose.GpioConnectionSettings.get_DefaultDriver () [0x00000] in <filename unknown>:0
at Raspberry.IO.GeneralPurpose.GpioConnectionSettings..ctor () [0x00000] in <filename unknown>:0
at Raspberry.IO.GeneralPurpose.GpioConnection..ctor (Raspberry.IO.GeneralPurpose.GpioConnectionSettings settings, IEnumerable`1 pins) [0x00000] in <filename unknown>:0
at Raspberry.IO.GeneralPurpose.GpioConnection..ctor (Raspberry.IO.GeneralPurpose.PinConfiguration[] pins) [0x00000] in <filename unknown>:0
at Hello.Program.Main (System.String[] args) [0x00000] in <filename unknown>:0

我可以用Python切换引脚状态,所以设备没有什么问题。

EN

回答 1

Stack Overflow用户

发布于 2014-09-02 04:52:22

以root用户身份执行Mono程序。一般用户无法访问/dev/mem。

代码语言:javascript
复制
public GpioConnectionDriver() {

        using (var memoryFile = UnixFile.Open("/dev/mem", UnixFileMode.ReadWrite | UnixFileMode.Synchronized)) {
            gpioAddress = MemoryMap.Create(
                IntPtr.Zero, 
                Interop.BCM2835_BLOCK_SIZE,
                MemoryProtection.ReadWrite,
                MemoryFlags.Shared, 
                memoryFile.Descriptor,
                Interop.BCM2835_GPIO_BASE
            );
        }
    }

来自这里的解释:http://www.raspberrypi.org/forums/viewtopic.php?f=29&t=22515

要打开/dev/mem,您既需要对设备文件的常规访问权限,也需要安全功能CAP_SYS_RAWIO,或者是根用户。这是无法解决的,因为对内存的完全访问允许的不仅仅是GPIO。它对安全有着巨大的影响。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25592564

复制
相关文章

相似问题

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