rasp14
Posts: 60
Joined: Sun Jun 22, 2014 2:49 pm

WiringPi C# wrapper unable to quit console normally

Thu Jun 06, 2019 8:58 am

Hi All,

Need some help over here, I'm using WiringPi C# wrapper from this link. My Pi is Pi1B, installed with latest Raspian Stretch.

I wrote a simple C# code to test out the gpio interrupt feature. I execute the program using mono version 5.20.1.19. If pin 17 is detect rising or falling edge, turn on / off pin 23 based on state from pin 17. Same thing for pin 27 and pin 24. Code below:

Code: Select all

	static void Main(string[] args)
        {
            WriteLog("Initialize WiringPi Gpio...");
            WiringPi.Init.WiringPiSetupGpio();
            WriteLog("WiringPi Gpio initialize complete.");

            WriteLog("Setting BCM pin 17 and 27 to input");
            WiringPi.GPIO.pinMode(17, (int)WiringPi.GPIO.GPIOpinmode.Input);
            WiringPi.GPIO.pinMode(27, (int)WiringPi.GPIO.GPIOpinmode.Input);

            WriteLog("Setting BCM pin 23 and 24 to output");
            WiringPi.GPIO.pinMode(23, (int)WiringPi.GPIO.GPIOpinmode.Output);
            WiringPi.GPIO.pinMode(24, (int)WiringPi.GPIO.GPIOpinmode.Output);

            WriteLog("Listening to pin 17 and 27...");

            WiringPi.PiThreadInterrupts.wiringPiISR(17, (int)WiringPi.PiThreadInterrupts.InterruptLevels.INT_EDGE_BOTH,
                new WiringPi.PiThreadInterrupts.ISRCallback(() =>
                {
                    if (WiringPi.GPIO.digitalRead(17) == 0)
                        WiringPi.GPIO.digitalWrite(23, 0);
                    else
                        WiringPi.GPIO.digitalWrite(23, 1);
                }));

            WiringPi.PiThreadInterrupts.wiringPiISR(27, (int)WiringPi.PiThreadInterrupts.InterruptLevels.INT_EDGE_BOTH,
                new WiringPi.PiThreadInterrupts.ISRCallback(() =>
                {
                    if (WiringPi.GPIO.digitalRead(27) == 0)
                        WiringPi.GPIO.digitalWrite(24, 0);
                    else
                        WiringPi.GPIO.digitalWrite(24, 1);
                }));

            Console.ReadLine();

            WriteLog("Killing program...");

            //t.Abort();
            WiringPi.GPIO.digitalWrite(23, 0);
            WiringPi.GPIO.digitalWrite(24, 0);

            Environment.Exit(0);
        }

        static void WriteLog(string message)
        {
            Console.WriteLine(string.Format("{0} - {1}", DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss.fff"), message));
        }

The code work as expected, with one small issue. When i press enter to kill the program, the program won't stop for some reason. Pin 23 and 24 did turn off after i press enter. So I'm thinking i need to do something before kill the program.

Appreciate if someone can help me with this.

Thank you in advance.

Return to “Other programming languages”