如何解决树莓派GPIO写入时的内存映射失败异常问题

3

我正在尝试使用 Raspberry Sharp IO 库来向 Pi 上的引脚写入数据。但是在运行时它会抛出一个异常。它之前曾经能够正常工作,但现在却不能。为什么会出现这个错误?

using System;
using Raspberry.IO.GeneralPurpose;
using Raspberry.IO.GeneralPurpose.Behaviors;
using System.Threading;

  namespace blinky
    {
       class MainClass
     {
    public static void Main (string[] args)
    {

    // Here we create a variable to address a specific pin for output
    // There are two different ways of numbering pins--the physical numbering, and the CPU number
    // "P1Pinxx" refers to the physical numbering, and ranges from P1Pin01-P1Pin40
        var led1 = ConnectorPin.P1Pin03.Output();

    // Here we create a connection to the pin we instantiated above
    var connection = new GpioConnection(led1);

    while(true){
        // Toggle() switches the high/low (on/off) status of the pin
        connection.Toggle(led1);
        Thread.Sleep(250);
    }

//  connection.Close();

}
}
 }

返回此错误:

pi@Minion01 ~/blinky1/blinky1/bin/Debug $ mono ./blinky1.exe

Unhandled Exception:
Raspberry.IO.Interop.MemoryMapFailedException: Exception of type 'Raspberry.IO.Interop.MemoryMapFailedException' was thrown.
  at Raspberry.IO.Interop.MemoryMap.ThrowOnError[MemoryMapFailedException] (IntPtr result) [0x00000] in <filename unknown>:0 
  at Raspberry.IO.Interop.MemoryMap.Create (IntPtr address, UInt32 size, MemoryProtection protection, MemoryFlags memoryflags, Int32 fileDescriptor, UInt32 offset) [0x00000] in <filename unknown>:0 
  at Raspberry.IO.GeneralPurpose.GpioConnectionDriver..ctor () [0x00000] in <filename unknown>:0 
  at Raspberry.IO.GeneralPurpose.GpioConnectionSettings.GetBestDriver (GpioConnectionDriverCapabilities capabilities) [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 blinky.MainClass.Main (System.String[] args) [0x00000] in <filename unknown>:0 
[ERROR] FATAL UNHANDLED EXCEPTION: Raspberry.IO.Interop.MemoryMapFailedException: Exception of type 'Raspberry.IO.Interop.MemoryMapFailedException' was thrown.
  at Raspberry.IO.Interop.MemoryMap.ThrowOnError[MemoryMapFailedException] (IntPtr result) [0x00000] in <filename unknown>:0 
  at Raspberry.IO.Interop.MemoryMap.Create (IntPtr address, UInt32 size, MemoryProtection protection, MemoryFlags memoryflags, Int32 fileDescriptor, UInt32 offset) [0x00000] in <filename unknown>:0 
  at Raspberry.IO.GeneralPurpose.GpioConnectionDriver..ctor () [0x00000] in <filename unknown>:0 
  at Raspberry.IO.GeneralPurpose.GpioConnectionSettings.GetBestDriver (GpioConnectionDriverCapabilities capabilities) [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 blinky.MainClass.Main (System.String[] args) [0x00000] in <filename unknown>:0 

我该如何修复它?

1个回答

3

我曾经尝试通过在树莓派上使用monodevelop或者mono filename.exe来运行它,但都没有成功。但是当我使用sudo ./filename.exe时,它就可以正常运行了。


另外,sudo mono filename.exe也可以做同样的事情并且有效(以防有人感到困惑)。 - Tyler Forsythe

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接