Click here to Skip to main content
15,884,836 members
Please Sign up or sign in to vote.
1.25/5 (4 votes)
See more:
Whit this part of my code I use to control the GPIO of a raspberry.
When I click on the start button and then the stop button the relay is working properly. When I do this for the second time i get an error. What am I doing wrong...

C#
{
   private const int relay1 = 4;
   private const int relay2 = 25;
   private const int relay3 = 22;
   private const int relay4 = 5;
   private GpioPin pin;
   private GpioPinValue pinValue;

   public MainPage()
   {
      //Scherm laden
      InitializeComponent();
   }

   //Relay1 START
   private void StartR1_Click(object sender, RoutedEventArgs e)
   {
      var gpio = GpioController.GetDefault();
      pin = gpio.OpenPin(relay1);
      pinValue = GpioPinValue.High;
      pin.Write(pinValue);
      pin.SetDriveMode(GpioPinDriveMode.Output);

      //Button Control Aan/Uit
      StartR1.IsEnabled = false;
      StopR1.IsEnabled = true;

   }

   //Relay1 STOP
   private void StopR1_Click(object sender, RoutedEventArgs e)
   {
      var gpio = GpioController.GetDefault();
      pinValue = GpioPinValue.Low;
      pin.Write(pinValue);
      pin.SetDriveMode(GpioPinDriveMode.Input);

      //Button Control Aan/Uit
      StartR1.IsEnabled = true;
      StopR1.IsEnabled = false;

   }
Posted
Updated 27-Oct-15 8:09am
v2
Comments
Patrice T 27-Oct-15 14:12pm    
What error and where ?
Dave Kreskowiak 27-Oct-15 14:22pm    
Why is it every newb thinks the error message is not an important piece of information when troubleshooting a problem?
ZurdoDev 27-Oct-15 14:25pm    
The error is?
Zoltán Zörgő 27-Oct-15 14:26pm    
As I see you are suing some GPIO class. Where is it from? Could you post a link to it?
On the other hand, you create those controllers without freeing them, I don't think it is wise to do so whatever class you are using.

Are using Windows 10? http://blogs.msdn.com/b/cdndevs/archive/2015/05/08/windows-10-how-to-use-iot-extension-for-raspberry-pi-2-part-1.aspx

Wild guess: the second time, the call to the openPin method fails because the pin is already open in exclusive mode (see the documentation[^]).
If that is the case then you should make such call just one time (possibly at your application startup).
 
Share this answer
 
Comments
Zoltán Zörgő 27-Oct-15 14:38pm    
No wild guess, I have found the same.
Look here: https://ms-iot.github.io/content/en-US/win10/samples/PinMappingsRPi2.htm[^]

The pin opened is disposed[^] (wrapped in using clause).
You can't simply reopen it. Use the IDisposable pattern properly.
 
Share this answer
 
Comments
CPallini 27-Oct-15 16:35pm    
5.
CPallini's and Zoltan's solutions are correct. More specifically, you need to do the following:

C#
var gpio = GpioController.GetDefault();
if (pin == null) // add this line.
    pin = gpio.OpenPin(relay1);


Good luck!
 
Share this answer
 
Comments
CPallini 27-Oct-15 16:35pm    
5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900