Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always getting an exception GpioConnection: Object reference not set to an instance of an object #86

Open
Asshen opened this issue May 6, 2017 · 11 comments

Comments

Projects
None yet
8 participants
@Asshen
Copy link

commented May 6, 2017

Hey.

I have a Raspberry Pi 3 B. I did some GPIO development using Python, but I would love to use C#.
I set up a small project (Raspberry.IO references are OK):

using System;
using Raspberry.IO.GeneralPurpose;

namespace Lichtje1
{
    class Program
    {
        public static void Main( string[] args )
        {
            var led1 = ConnectorPin.P1Pin15.Output();

            var connection = new GpioConnection( led1 );

            for( var i = 0; i < 100; i++ )
            {
                connection.Toggle( led1 );
                System.Threading.Thread.Sleep( 250 );
            }

            connection.Close();
        }
    }
}

I keep getting the following error no matter if I compile on Visual Studio (on Windows) or MonoDevelop (on Raspberry Pi):

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 Lichtje1.Program.Main (System.String[] args) [0x00011] in <fb46ca8bbdcc45f0952b44480e70cfba>: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.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 Lichtje1.Program.Main (System.String[] args) [0x00011] in <fb46ca8bbdcc45f0952b44480e70cfba>:0

Thank you for any help.
Kind regards.
Kris.

@Arakis

This comment has been minimized.

Copy link

commented May 7, 2017

i have a similar problem:

System.NullReferenceException: Object reference not set to an instance of an object
at Raspberry.IO.GeneralPurpose.GpioOutputBinaryPin..ctor

Its since the upgrade from kernel ARCH Linux 4.4 to 4.9

@Bulinlinbu

This comment has been minimized.

Copy link

commented May 30, 2017

So do I

It seems that "Driver" is null GeneralPurpose > GpioConnection.cs line 488 :
Driver.Allocate(configuration.Pin, configuration.Direction);

@Bulinlinbu

This comment has been minimized.

Copy link

commented May 30, 2017

OK, il think the problem is from Raspberry.System API, it doesn't reconize the CPU of this version of PI.

If I do this :
Raspberry.Board.Current.IsRaspberryPi; // False

See this request :
raspberry-sharp/raspberry-sharp-system#6

@Bulinlinbu

This comment has been minimized.

Copy link

commented May 30, 2017

Right, it works for me.
First : in Raspberry.System librairy, add the BCM2835 CPU
see this post : raspberry-sharp/raspberry-sharp-system#6

Next : In Raspberry.IO.GeneralPurpose, edit the function :
In GpioConnectionDriver.cs line 291
and in MemoryGpioConnectionDriver line 246

private static uint GetProcessorBaseAddress(Processor processor)
{
	switch (processor)
	{
		case Processor.Bcm2708:
			return Interop.BCM2835_GPIO_BASE;
		case Processor.Bcm2709:
		case Processor.Bcm2835: // Add this
			return Interop.BCM2836_GPIO_BASE;
		default:
			throw new ArgumentOutOfRangeException("processor");
	}
}

IT is magic !

@NeededAnUnusedName

This comment has been minimized.

Copy link

commented Jul 2, 2017

Hi, can anyone please repeat the steps (as simple as possible ^^) to solve this problem? There is a dead link. I found the function above but there are so many build errors... Is there a simpler solution, or can I maybe update to a fixed version with nuget?

@Bulinlinbu

This comment has been minimized.

Copy link

commented Jul 2, 2017

Link not dead anymore

@NeededAnUnusedName

This comment has been minimized.

Copy link

commented Jul 2, 2017

Thanks, maybe fix the other link too, I still cant reach it.
But it helped a lot, thank you very much! =)

@JohnRusk

This comment has been minimized.

Copy link

commented Oct 22, 2017

A note for others who find this page: it's recommended to use the revision number now, for identifying the processor. See raspberrypi/linux#2008 and raspberrypi/firmware#705

Also, I wonder if even older Pi's report Bcm2835 as the Hardware value in /proc/cpuinfonow. (The change, after all, was in the OS kernel, not the actual hardware). If that's the case, the both old and new Pi's may be reporting the same value, in which case the fix suggested by @Bulinlinbu above will not be safe on older Pi's. I have no way of testing this tho, since I only have a new one.

However, if you know you're only running on a RPi 3, then @Bulinlinbu's change looks safe enough.

Finally, test on my RPi3 indicates that this quick and dirty workaround also works, if you know you are running on a RPi 3:

Dictionary<string, string> boardSettings = 
                   (Dictionary<string, string>) Board.Current.GetType()
                  .GetField("settings", BindingFlags.Instance | BindingFlags.NonPublic)
                 .GetValue(Board.Current);
boardSettings["Hardware"] = "BCM2709";

I wouldn't advocate using that for any long-term purpose, but as a quick workaround until R#.IO, for users who know they are on RPi3, it seems to work. It amounts to forcing in the value that the library expects to see on an RPi 2 - which results in the correct GPIO memory addresses being selected because the addresses are the same on the Pi 3.

@Dip11

This comment has been minimized.

Copy link

commented Aug 7, 2018

install Raspberry.IO.GeneralPurpose3 -Version 3.1.1 instead of Raspberry.IO.GeneralPurpose as nuget package. Hope ur problem will be solved.

@JTrotta

This comment has been minimized.

Copy link

commented Aug 7, 2018

Raspberry.IO.GeneralPurpose3 has been disocontinued.
Try https://github.com/JTrotta/RaspberrySharp

@piibet

This comment has been minimized.

Copy link

commented Dec 16, 2018

Please make sure you have enabled all Interfaces in Rasperry Pi Configuration.
I forgot to do that, and it gave me same error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.