【问题标题】:c# mono raspberry pi GPIO with Raspberry# getting Operation is not validc# mono raspberry pi GPIO with Raspberry#getting Operation is not valid
【发布时间】:2014-10-24 21:34:44
【问题描述】:

我正在尝试使用 Raspberry# 库通过 Raspberry PI 上的 GPIO 引脚(打开和关闭)执行基本任务。 根据 github 上的示例:https://github.com/raspberry-sharp/raspberry-sharp-io/wiki/Raspberry.IO.GeneralPurpose

代码:

        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);我得到异常:

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

堆栈跟踪

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 切换引脚状态,因此设备没有任何问题。

【问题讨论】:

    标签: c# mono raspberry-pi gpio


    【解决方案1】:

    以 root 身份执行您的 Mono 程序。普通用户无法访问 /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
                );
            }
        }
    

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

    要打开 /dev/mem,您需要设备文件的常规访问权限和安全功能 CAP_SYS_RAWIO,或者是 root。没有办法解决这个问题,因为对内存的完全访问允许的不仅仅是 GPIO。它具有巨大的安全隐患。

    【讨论】:

      猜你喜欢
      • 2022-07-12
      • 1970-01-01
      • 2022-06-23
      • 1970-01-01
      • 1970-01-01
      • 2015-07-15
      • 2013-04-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多