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

Posted

技术标签:

【中文标题】c# mono raspberry pi GPIO with Raspberry#getting Operation is not valid【英文标题】:c# 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 切换引脚状态,因此设备没有任何问题。

【问题讨论】:

【参考方案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。它具有巨大的安全隐患。

【讨论】:

以上是关于c# mono raspberry pi GPIO with Raspberry#getting Operation is not valid的主要内容,如果未能解决你的问题,请参考以下文章

Raspberry Pi - Python 中的 GPIO 事件

使用 GPIO 后 Raspberry Pi 上的 SPI 重置

Tkinter 滑块和 Raspberry Pi 4 GPIO 接口

如何通过 Apache Web 服务器控制 Raspberry Pi 的串行 GPIO 引脚

Raspberry Pi 4B 循迹模块

ValueError:发送的通道在 Raspberry Pi 上无效 - 使用 Python 控制 GPIO Pin 2 (BOARD) 会导致错误