I would be happy to understand why I cannot read the value of an InterruptPort from a Thread.
Is this bug or I made an obvious mistake?
To experiment it, comment out one the of the two line in the main().
Context: I have define the an interruptport to intercept events (which is not represented in this code sample.) but I am interested in reading the initial value as well before any event occurs so my system can behave accordingly at the beginning.
My setup is Netduino 1 and MF4.2.
using System; using System.Threading; using Microsoft.SPOT; using Microsoft.SPOT.Hardware; using SecretLabs.NETMF.Hardware; using SecretLabs.NETMF.Hardware.Netduino; namespace ReadInterruptGPIOfromThread { public class Program { InterruptPort floatSwitch = new InterruptPort(Pins.GPIO_PIN_D1, true, Port.ResistorMode.Disabled, Port.InterruptMode.InterruptEdgeBoth); //InterruptEdgeHigh with a hardware pullup resistor set up. The event triggers when the float is low. The float switch is set to be active when up. Thread runT = new Thread(new ThreadStart(p.Run)); //Thread to execute the main program. public static Program p = new Program(); public static void Main() { p.runT.Start(); //This does not work. // p.Run(); //This works when the previous line is commented out! } public void Run() { while (true) { readPort(); Thread.Sleep(1000); } } public void readPort() { Boolean value = floatSwitch.Read(); Debug.Print("Read from port: " + value); } } }