我试图使用Raspberry#库来执行基本任务,在Raspberry#上使用GPIO引脚(on和off)。以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切换引脚状态,所以设备没有什么问题。
发布于 2014-09-02 04:52:22
以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,或者是根用户。这是无法解决的,因为对内存的完全访问允许的不仅仅是GPIO。它对安全有着巨大的影响。
https://stackoverflow.com/questions/25592564
复制相似问题