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

Script stops running after board is disconnected from computer


  • Please log in to reply
10 replies to this topic

#1 haxburgen

haxburgen

    Member

  • Members
  • PipPip
  • 21 posts

Posted 23 October 2011 - 02:31 PM

Hi All,

My script runs fine when I have my Netduino Plus plugged in and being debugged. But when I removed it from my computer and try to run the script independent of debugging, it stops running. I am using the board as a Web Client running on a continuous loop. I don't believe its code related but here is my code.

using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.NetduinoPlus;

namespace myProgram
{
    public class Program
    {
        public static void Main()
        {
            WebCall webCall = new WebCall();

            TimerCallback timerDelegate = new TimerCallback(webCall.GetResponse);

            AutoResetEvent autoEvent = new AutoResetEvent(false);

            Timer programLoop = new Timer(timerDelegate, autoEvent, 1000, 60000);

            while (programLoop != null)
            {
            }
        }
    }


    public class WebCall
    {
        OutputPort controlPort = new OutputPort(Pins.GPIO_PIN_D8, false);

        // produce request
        String requestUri = "http://www.mysite.com/somecall.php?ID=1";

        public void GetResponse(Object stateInfo)
        {
            try
            {
                using (var request = (HttpWebRequest)WebRequest.Create(requestUri))
                {
                    request.Method = "GET";
                    // headers
                    request.ContentType = "application/x-www-form-urlencoded";
                    try
                    {
                        using (var response = (HttpWebResponse)request.GetResponse())
                        {
                            StreamReader reader = new StreamReader(response.GetResponseStream());

                            String returnString = reader.ReadToEnd();

                            if (returnString.Length > 0)
                            {
                                String[] array1 = returnString.Split(new Char[] { ',' });

                                for (int l = 0; l < array1.Length - 1; l++)
                                {
                                    String[] array2 = array1[l].Split(new Char[] { '-' });

                                    if (array2[1].Equals("0"))
                                    {
                                        controlPort.Write(false);
                                    }
                                    else
                                    {
                                        controlPort.Write(true);
                                    }
                                }
                            }
                        }
                    }
                    catch (WebException ex)
                    {
                        Debug.Print(ex.Message);
                    }
                }
            }
            catch (IOException ex)
            {
                Debug.Print(ex.Message);
            }
        }
    }
}


#2 Stefan W.

Stefan W.

    Advanced Member

  • Members
  • PipPipPip
  • 153 posts

Posted 23 October 2011 - 04:34 PM

With "stops running", you mean it stops sending requests, or it stops execution completely? Did you try pulsing the onboard LED in another thread to see if execution completely stops, or just the web part? Also, to check if it's connected to debugging, you could just "deploy" it from VS and then reset the board, without starting the debugger ...
I believe that no discovery of fact, however trivial, can be wholly useless to the race, and that no trumpeting of falsehood, however virtuous in intent, can be anything but vicious.
-- H.L. Mencken, "What I Believe"

#3 haxburgen

haxburgen

    Member

  • Members
  • PipPip
  • 21 posts

Posted 23 October 2011 - 06:11 PM

Thanks for the reply Stefan. I have never worked with threading before, other than the main thread. How would I go about blinking the led on another thread? Thanks!

#4 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 23 October 2011 - 06:57 PM

My script runs fine when I have my Netduino Plus plugged in and being debugged. But when I removed it from my computer and try to run the script independent of debugging, it stops running. I am using the board as a Web Client running on a continuous loop. I don't believe its code related but here is my code.

It is only a wild guess, but could you try making programLoop a class member:

public class Program
{
  private static Timer programLoop;
  ...
  public static void Main()
  {
    ...
    programLoop = new Timer(timerDelegate, autoEvent, 1000, 60000);

    Thread.Sleep(Timeout.Infinite);
  }
}


#5 haxburgen

haxburgen

    Member

  • Members
  • PipPip
  • 21 posts

Posted 24 October 2011 - 01:33 AM

Thanks CW2. That worked. I don't know why that made a difference though.

#6 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 24 October 2011 - 01:46 AM

Thanks CW2.

That worked. I don't know why that made a difference though.

You may want to try one of those changes (make the variable static) and then the other (just go to sleep forever) to identify which issue tweak fixed the issue. My guess is that the loop was exiting (also, the loop was just taking up a whole bunch of CPU cycles...going to sleep in the main thread is a better solution overall).

Chris

#7 haxburgen

haxburgen

    Member

  • Members
  • PipPip
  • 21 posts

Posted 24 October 2011 - 01:59 AM

I actually want to take that back my previous post. The web response seems to happen the first time after I pull the USB out of my computer. (I guess because its in the middle of the timer call?) After that its no good.

#8 Stefan W.

Stefan W.

    Advanced Member

  • Members
  • PipPipPip
  • 153 posts

Posted 24 October 2011 - 04:34 PM

Regarding the LED (stolen from http://nerduino.wordpress.com/)

public class Program
{
  private static Timer programLoop;
  private static Thread flasher;
  ...
  public static void Main()
  {
    ...
    programLoop = new Timer(timerDelegate, autoEvent, 1000, 60000);
    flasher = new Thread(delegate(){ doFlash(); });
    Thread.Sleep(Timeout.Infinite);
  }
  public static void doFlash()
 {
 OutputPort led = new OutputPort(Pins.GPIO_PIN_D13, false);

 while (true)
 {
 led.Write(true);
 Thread.Sleep(250);
 led.Write(false);
 Thread.Sleep(250);
 }
 }

}


I believe that no discovery of fact, however trivial, can be wholly useless to the race, and that no trumpeting of falsehood, however virtuous in intent, can be anything but vicious.
-- H.L. Mencken, "What I Believe"

#9 Nevyn

Nevyn

    Advanced Member

  • Members
  • PipPipPip
  • 1072 posts
  • LocationNorth Yorkshire, UK

Posted 24 October 2011 - 08:17 PM

public class Program
{
  private static Timer programLoop;
  private static Thread flasher;
  ...
  public static void Main()
  {
    ...
    programLoop = new Timer(timerDelegate, autoEvent, 1000, 60000);
    flasher = new Thread(delegate(){ doFlash(); });
    Thread.Sleep(Timeout.Infinite);
  }
  public static void doFlash()
 {
 OutputPort led = new OutputPort(Pins.GPIO_PIN_D13, false);

 while (true)
 {
 led.Write(true);
 Thread.Sleep(250);
 led.Write(false);
 Thread.Sleep(250);
 }
 }

}

I've taken to flashing the LED "on demand" as I find the constantly flashing LED can be distracting. I've done this by hooking into the on board button event handler and flash the LED when it's pushed. It has several advantages:

- It does not consume CPU time from the application when it's not needed
- It still allows to to detect if the board and app are still alive even if only in some limited form
- No flashing LED when it's not needed
- Can leave in production code if necessary as it does not consume any power when not used

Regards,
Mark

To be or not to be = 0xFF

 

Blogging about Netduino, .NET, STM8S and STM32 and generally waffling on about life

Follow @nevynuk on Twitter


#10 haxburgen

haxburgen

    Member

  • Members
  • PipPip
  • 21 posts

Posted 25 October 2011 - 06:22 AM

I figured out that execution stopss at the point at which I get the http response.

#11 haxburgen

haxburgen

    Member

  • Members
  • PipPip
  • 21 posts

Posted 01 November 2011 - 05:08 PM

UPDATE: After removing the 9V barrel connector I had to the netduino and connecting the Micro USB to an iPhone outlet, I can now successfully run my program independent from the computer. Thanks Chris!




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.