Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

Reading an InterruptPort from a thread does not work!

InterruptPort Thread

  • Please log in to reply
2 replies to this topic

#1 shimoda

shimoda

    Member

  • Members
  • PipPip
  • 23 posts
  • LocationQuebec, Canada

Posted 09 September 2015 - 06:32 PM

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);
        }
    }
}


#2 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 10 September 2015 - 12:00 AM

Hi shimoda,

I'm assuming that the problem here is that you're opening the same interruptport on _two_ different threads. Ports can only be instantiated once--and then they must be disposed of before they can be opened on another thread.

Of course this could be another issue. The first thing I'd try is moving the InterruptPort constructor from the variable definition to the Run function...so that you know when the port is being instantiated, and so that VS helpfully alerts you if you try opening the port twice.

Does that help get you on the right path?

Chris

#3 shimoda

shimoda

    Member

  • Members
  • PipPip
  • 23 posts
  • LocationQuebec, Canada

Posted 11 September 2015 - 03:08 AM

Hi Chris, 

 

You got it right! Moving the interruptPort into the Run function works. This code sample is just a part of a bigger project I have and I am more confuse on how to deal with threads.

 

Is there a way to make Visual Studio debugger showing more relevant information to nail down thread and event issues? 

 

The big picture of my project is the following: 

- The program will consist of a sequence of steps. 

- An interrupt can Suspend and Resume the program not matter which step is processing. 
- Each step will have few different interrupts that will live through it. 

 

I would appreciate any information sources on how I can architecture threads in my program to reach my goal.







Also tagged with one or more of these keywords: InterruptPort, Thread

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.