简体   繁体   English

c#mono raspberry pi GPIO with Raspberry#获取操作无效

[英]c# mono raspberry pi GPIO with Raspberry# getting Operation is not valid

I'm trying to use Raspberry# library to perform basic task whith GPIO pin on Raspberry PI (on and off).我正在尝试使用 Raspberry# 库在 Raspberry PI(开和关)上执行带有 GPIO 引脚的基本任务。 As per example on github: https://github.com/raspberry-sharp/raspberry-sharp-io/wiki/Raspberry.IO.GeneralPurpose根据github上的示例: https : //github.com/raspberry-sharp/raspberry-sharp-io/wiki/Raspberry.IO.GeneralPurpose

Code:代码:

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

on line var connection = new GpioConnection(led1);在线var connection = new GpioConnection(led1); I get exception:我得到例外:

"Operation is not valid due to the current state of the object" “由于对象的当前状态,操作无效”

Stack trace堆栈跟踪

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

I am able to switch pin state with Python so nothing is wrong with device.我可以使用 Python 切换引脚状态,因此设备没有任何问题。

Execute your Mono program as root.以 root 身份执行 Mono 程序。 /dev/mem is not accessible to normal users.普通用户无法访问 /dev/mem。

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

Explanation from here: http://www.raspberrypi.org/forums/viewtopic.php?f=29&t=22515从这里解释: http : //www.raspberrypi.org/forums/viewtopic.php? f=29&t= 22515

To open /dev/mem you need both regular access permissions on the device file and the security capability CAP_SYS_RAWIO, or to be root.要打开 /dev/mem,您需要对设备文件的常规访问权限和安全功能 CAP_SYS_RAWIO,或者是 root。 There is no getting around this, because full access to memory allows a lot more than just GPIO.没有办法解决这个问题,因为对内存的完全访问允许的不仅仅是 GPIO。 It has huge security implications.它具有巨大的安全隐患。

you can use Iot.Device.Bindings package it's better你可以使用 Iot.Device.Bindings 包更好

https://docs.microsoft.com/fr-fr/dotnet/iot/tutorials/blink-led https://docs.microsoft.com/fr-fr/dotnet/iot/tutorials/blink-led

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

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