G30 simple program hangs after running for one hour

Hi,

This is my first project with G30 and NETMF. I am using G30 Dev Board and NETMF SDK 2015 R1.

I need to develop a continuously running module for telemetry use, so I started with a very simple program that prints the number of seconds it is being running (and blinks a led). Unfortunalety it runs for about one hour and then hangs. I haven’t been able to figure out why.

The program always hangs, no matter if it is running stand alone or being debugged with Visual Studio.

This is the code:

using System;
using System.Threading;
using System.Text;
using System.IO;
using System.IO.Ports;
using Microsoft.SPOT;
using Microsoft.SPOT.IO;
using Microsoft.SPOT.Hardware;
using GHI.IO.Storage;
using GHI.Processor;

namespace WISEBOX
{
public class Program
{
static DateTime DT;
static int ciclo; // Contador de ciclos

    public static void Main()
    {
        ciclo = 0;

        Boolean statLed = false;
        string txtCiclo = "";

        DT = new DateTime(2016, 05, 17, 11, 02, 00);
        RealTimeClock.SetDateTime(DT);
        Utility.SetLocalTime(DT);

        // Led 1 
        OutputPort LED1 = new OutputPort(GHI.Pins.G30.Gpio.PC7, true);

        // ==========================================================================
        // Inicia el procesamiento 
        // ==========================================================================
        Debug.Print("Wisebox 4E. Inicializando\n\r");
        Debug.Print("Fecha-Hora RTC  :" + RealTimeClock.GetDateTime().ToString());
        Debug.Print("Fecha-Hora Local:" + DateTime.Now.ToString());

        while (true)
        {

            // Informo el ciclo, enciendo/apago el led y espero 1 segundo
            ciclo++;
            txtCiclo = DateTime.Now.ToString() + " Ciclo " + ciclo.ToString();
            Debug.Print(txtCiclo);
            if (statLed == true)
            {
                statLed = false;
            }
            else
            {
                statLed = true;
            }
            LED1.Write(statLed);
            Thread.Sleep(1000);
        }

    }

}

}

Any help will be highly appreciated.

Juan Carlos Plaza

Are you using the latest pre release and have updated the firmware?

@ jcplaza@ wiseaccess.com - Hi,
with Debug.Print every second you have a dependency outside your board. I would try if it works longer if you use the Debug.Print e.g. only once a minute.

Hi everybody,

First of all, thanks a lot for your recommendations. I apologize for not responding earlier, Since this is my first post, I wasn’t allowed to post replies for 8 hours.

cyberh0me: Below I’m including a commented copy of my code, for better readability.

Gus: I am using the 2015 SDK. I’ll install the latest prerelease, update the firmware and try again tomorrow.

RoSchmi: I modified the program and replaced the Debug.print by a message through COM1, wich I am monitoring using a serial terminal program.

Here os the commented version of my code (the original version, with the Debug.print):

namespace WISEBOX
{
public class Program
{
static DateTime DT; // Define Date and Time variable
static int ciclo; // Define Cicle counter variable

    public static void Main()
    {
        ciclo = 0;		// Set cicle counter to zero

        Boolean statLed = false;	// Define led status variable and initialize it to false
        string txtCiclo = "";	// Define string variable for message

        // Set DateTime to some time
        DT = new DateTime(2016, 05, 17, 11, 02, 00);	
        RealTimeClock.SetDateTime(DT);
        Utility.SetLocalTime(DT);

        // Define my Led (I use the G30 Dev Board led) 
        OutputPort LED1 = new OutputPort(GHI.Pins.G30.Gpio.PC7, true);

        // Print start message 
        Debug.Print("Wisebox 4E. Inicializando\n\r");
        Debug.Print("Fecha-Hora RTC  :" + RealTimeClock.GetDateTime().ToString());
        Debug.Print("Fecha-Hora Local:" + DateTime.Now.ToString());

        // Loop forever
        while (true)
        {
            // Increment cicle counter
            ciclo++;		
            
            // Print Date, Time and cicle counter
            txtCiclo = DateTime.Now.ToString() + " Ciclo " + ciclo.ToString();
            Debug.Print(txtCiclo);
            
            // Change led status. If ON I turn it OFF, and viceversa
            if (statLed == true)
            {
                statLed = false;
            }
            else
            {
                statLed = true;
            }
            LED1.Write(statLed);
            
            // Wait for 1 second
            Thread.Sleep(1000);
        }

    }

}

}

He was requesting that you add code tags not comments. See the “</>” button in the post editor.

Oops!! Thank you ianlee74!!

Here it goes:

namespace WISEBOX
{
    public class Program
    {
        static DateTime DT;	// Define Date and Time variable
        static int ciclo;       // Define Cycle counter variable

        public static void Main()
        {
            ciclo = 0;		// Set cycle counter to zero

            Boolean statLed = false;	// Define led status variable and initialize it to false
            string txtCiclo = "";	// Define string variable for message

            // Set DateTime to some time
            DT = new DateTime(2016, 05, 17, 11, 02, 00);	
            RealTimeClock.SetDateTime(DT);
            Utility.SetLocalTime(DT);

            // Define my Led (I use the G30 Dev Board led) 
            OutputPort LED1 = new OutputPort(GHI.Pins.G30.Gpio.PC7, true);

            // ======================================================================================================================
            // Print start message 
            // ======================================================================================================================
            Debug.Print("Wisebox 4E. Inicializando\n\r");
            Debug.Print("Fecha-Hora RTC  :" + RealTimeClock.GetDateTime().ToString());
            Debug.Print("Fecha-Hora Local:" + DateTime.Now.ToString());

            // Loop forever
            while (true)
            {
                // Increment cycle counter
                ciclo++;		
                
                // Print Date, Time and cycle counter
                txtCiclo = DateTime.Now.ToString() + " Ciclo " + ciclo.ToString();
                Debug.Print(txtCiclo);
                
                // Change led status. If ON I turn it OFF, and viceversa
                if (statLed == true)
                {
                    statLed = false;
                }
                else
                {
                    statLed = true;
                }
                LED1.Write(statLed);
                
                // Wait for 1 second
                Thread.Sleep(1000);
            }

        }

    }

}

@ jcplaza@ wiseaccess.com - On a side note, the int variable ciclo will eventually throw and overflow exception, if you really are looping forever.

Try adding Debug.GC(true) just before the Sleep method. I want to know if you’re running out of memory.

@ Mr. John Smith - You are right! with a 32 bit integer, and the posted program, it will only run for 68 years. :smiley:

3 Likes

I’ve found that the Debug.Print causes issues on these small memory processors. I’ve seen it on the older Cerbuino so I only use this when doing initial tests but prefer to breakpoint and read values with the debugger.

Try commenting them out and leaving it running. If it does crash it will output to the debug window the exception.

There is nothing really in your code that would be using up memory so try without the debug and see if this will still run for longer.

My bet is with @ Dave’s idea.

A general coding tip for you - this…

can be shortened to just this…


// Change led status. If ON I turn it OFF, and viceversa
statLed = !statLed;

Or drop the status all together…

using System.Threading;
using Microsoft.SPOT.Hardware;

namespace MFConsoleApplication2
{
    public class Program
    {
        private static OutputPort _led;
        public static void Main()
        {
            _led = new OutputPort(Cpu.Pin.GPIO_Pin0, false);
            for (;;)
            {
                _led.Write(!_led.Read());
                Thread.Sleep(250);
            }
        }
    }
}

Thanks a lot to everybody for the recommendations. I’ll include all the improvements into my code.

Yesterday I took out the Debug.print and replaced it by a message every 60 seconds through COM1. The program run for about 50 minutes and then hung.

Today I’ll be out of my office, but this evening I’ll performs the following tests:
[ul]Include Debug.GC(true).ToString() into the message.
I have a spare G30 Dev Board, so I’ll install the lateste prerelease of the SDK in another computer, upgrade the firmware in the spare G30 Dev Board and run the program to see if that produces a different result.
[/ul]
I’ll post the results tonight.

Replace all you string concatenations with string.Concat() e.g.


With 

```cs]txtCiclo = string.Concat(DateTime.Now, " Ciclo ", ciclo);[/code


And add exception handling inside your loop, e.g.

```cs
while (true)
{
    try
    {
        // your code goes here
    }
    catch (Exception ex)
    {
        Debug.Print(ex.Message);
    }
}

@ jcplaza@ wiseaccess.com - You need the latest firmware, from the pre release. If I remember correctly, there was a bug that causes the g30 to hang after some time. This was fixed in the prerelease.

1 Like

LOL. And all of us here throwing out suggestions, to an issue that’s already been fixed #facepalm.

My suggestion about exception handling should still apply. ;D

1 Like

Gus: Great!. Installing the firmware from the prerelease is the first thing I’ll do this evening. I’ll post the results asap.

By the way, all the comments and recommendations will help me improve my coding, so the side effects of this post have been beneficial too. I’ll definitely apply all of them. :clap:

Upgrading the firmware did the magic. The program has been working for more than 10 hours.

Thanks a lot for the great support, suggestions and comments. This is my very first project with a GHI product and now I am sure it will be the first of many. Great product, great support, and great community!!!

3 Likes