picandies
Posts: 186
Joined: Wed Nov 26, 2014 5:13 pm

GPIO throws exception ( monodevelop )

Sat Mar 24, 2018 8:36 pm

Hello:
Made a simple control panel with monodevlop (5.10) on the rpi , following some postings on how to install and use. Using the latest Stretch install, on RPI3....Tried adding some I/O using some other examples I saw....however after adding the I/O, the control panel no longer showed . Builds fine, however turns out an exception is being thrown (so program aborts with no panel). Went back to a pure I/O example & found after some investigation this line causes the exception:

var connection = new GpioConnection(led1);

Read somewhere that it might have to do with a privilege level, so tried: sudo mono blinky.exe ...but still got an exception.
I'm using the latest package 2.4 of Raspberry.IO.GeneralPurpose in monodevelop
I'm using the latest package 2.1 of Raspberry.System

Code: Select all

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

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.P1Pin07.Output();

			// Here we create a connection to the pin we instantiated above
			var connection = new GpioConnection(led1);
		
			for (var i = 0; i<100; i++) {
				// Toggle() switches the high/low (on/off) status of the pin
				connection.Toggle(led1);
				System.Threading.Thread.Sleep(50);
			}

			connection.Close();

		}
	}
}
Unhandled Exception:
System.NullReferenceException: Object reference not set to an instance of an object.
at Raspberry.IO.GeneralPurpose.GpioConnection.Allocate (Raspberry.IO.GeneralPurpose.PinConfiguration configuration) [0x00073] in <39616ab275014559a5b975fe859e9ad9>:0
at Raspberry.IO.GeneralPurpose.GpioConnection.Open () [0x00036] in <39616ab275014559a5b975fe859e9ad9>:0
at Raspberry.IO.GeneralPurpose.GpioConnection..ctor (Raspberry.IO.GeneralPurpose.GpioConnectionSettings settings, System.Collections.Generic.IEnumerable`1[T] pins) [0x000f8] in <39616ab275014559a5b975fe859e9ad9>:0
at Raspberry.IO.GeneralPurpose.GpioConnection..ctor (Raspberry.IO.GeneralPurpose.PinConfiguration[] pins) [0x00000] in <39616ab275014559a5b975fe859e9ad9>:0
at blinky.MainClass.Main (System.String[] args) [0x00007] in <d9d28474f28343f98adfff32ee5feb46>:0
[ERROR] FATAL UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object. at Raspberry.IO.GeneralPurpose.GpioConnection.Allocate (Raspberry.IO.GeneralPurpose.PinConfiguration configuration) [0x00073] in <39616ab275014559a5b975fe859e9ad9>:0
at Raspberry.IO.GeneralPur

Any thoughts on what is going wrong (is the program wrong or some other trouble), or are there any other known working samples of blinking an LED or controlling some IO using mono (monodevelop).

picandies
Posts: 186
Joined: Wed Nov 26, 2014 5:13 pm

Re: GPIO throws exception

Sun Mar 25, 2018 5:12 am

After a 2 day journey of madness, found there apparently was something that "broke" the compatibility with the libraries (last updated 2016), causing some chip recognition issues:
As far as I was able to tell it's because newest version of Raspbian report the hardware to be BCM2835 instead of BCM2709.

By chance, I found some newer "nugets" (11/2017) Raspberry.IO.GeneralPurpose3 & Raspberry.System3 & swapped them in, hoping they'd "drop in" in terms of usage syntax.

Now the program runs!! I found you still need to use sudo (ex: sudo mono myprogram.exe) to run the program, or you will get some severe errors:

Unhandled Exception:
Raspberry.IO.Interop.MemoryMapFailedException: Exception of type 'Raspberry.IO.Interop.MemoryMapFailedException' was thrown.
at Raspberry.IO.Interop.MemoryMap.ThrowOnError[TException] (System.IntPtr result) [0x00012] in <71d5e011f1b348499f25c3d042acc2e5>:0
at Raspberry.IO.Interop.MemoryMap.Create (System.IntPtr address, System.UInt32 size, Raspberry.IO.Interop.MemoryProtection protection, Raspberry.IO.Interop.MemoryFlags memoryflags, System.Int32 fileDescriptor, System.UInt32 offset) [0x00018] in

Not sure what is going on or how to work around it (can't run from within the mono IDE).

Return to “Other programming languages”