Help needed

Hi, I have a problem with the following code. I’m using a Lemur board and a TinyRTC module on I2C along with a DS18B20 One Wire temp. sensor. Using SDK 4.3 and VS 2015.
The code all works thanks in large part to this forum. The program is a simple data logger for time and temp. which will eventually form part of a larger system. When I say it runs, it does so for just over one hour and them stops working altogether. I had originally set a Thread.Sleep period of 10 minutes, but changed it to the system of 5 secs. in a loop of 120 iterations.
It records to SD card the time and temp every 10 minutes and stops just over one hour. If I change the loop delay to record data every 5 minutes it still stops at just over one hour. I need it to run continuously. Is there something about .NetMF I am missing?


using System;
using System.IO;
using System.Threading;
using System.Text;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using Microsoft.SPOT.IO;
using StablePoint.Hardware.OneWire.Devices;
using StablePoint.Hardware.OneWire;
using GHI.Pins;
using GHI.IO.Storage;

namespace RTC
    public class Program
        public static void Main()
            int i = 0;
            OutputPort LED1 = new OutputPort((Cpu.Pin)FEZLemur.Gpio.Led1, true);
            OutputPort LED2 = new OutputPort((Cpu.Pin)FEZLemur.Gpio.Led2, true);
            InputPort mySwitch = new InputPort((Cpu.Pin)FEZLemur.Gpio.Ldr1, false, Port.ResistorMode.PullUp);
            SDCard sd_card = new SDCard();
            string rootDirectory = VolumeInfo.GetVolumes()[0].RootDirectory;
            FileStream FileHandle = new FileStream(rootDirectory + @ "\data.csv", FileMode.Create); 

            RTC.RTC_1307 RTC_time = new RTC.RTC_1307();

                var oneWirePin = ((Cpu.Pin)FEZLemur.Gpio.D8);
                var oneWireBus = new Bus(oneWirePin);
                var devices = oneWireBus.ScanForDevices(Family.Ds18B20);
                Debug.Print(devices.Length + "  DS18B20 sensor(s) found.");
                //Set the date and time
                //  RTC_time.SetDateTime(new DateTime(2015, 12, 16, 11, 03, 30));
            var ds18B20 = new Ds18B20(devices[0]);

            int myHour = RTC_time.GetDateTime().Hour;
            int myMinute = RTC_time.GetDateTime().Minute;
            int mySecond = RTC_time.GetDateTime().Second;
            int myTimeStart = (myHour * 60 * 60) + (myMinute * 60) + mySecond;
            while (mySwitch.Read())
                i = 0;
                while(i<=120 && mySwitch.Read())
                    Debug.Print(i + "  " + "In short loop");

                myHour = RTC_time.GetDateTime().Hour;
                myMinute = RTC_time.GetDateTime().Minute;
                mySecond = RTC_time.GetDateTime().Second;

                int myTimeFinish = (myHour * 60 * 60) + (myMinute * 60) + mySecond;
                int myTotalTime = myTimeFinish - myTimeStart;

                        if (ds18B20.UpdateValues() == true)
                            string temp = ds18B20.Temperature.ToString() + "°C";                            
                            byte[] data = Encoding.UTF8.GetBytes(RTC_time.GetDateTime().ToString("G") + "," + 1 + "," + myTotalTime.ToString() + "," + temp + "\n");
                            FileHandle.Write(data, 0, data.Length);                           
                            Debug.Print("Recorded" + "   " + myTotalTime.ToString() + "   " + RTC_time.GetDateTime().ToString("G") + "  " + temp);
                            //something's wrong. May happen with longer cables
                            Debug.Print("...failure! Maybe try again?");





RTC Class

using System;
using Microsoft.SPOT.Hardware;

namespace RTC
    class RTC_1307 : IDisposable
        private I2CDevice I2C;
        I2CDevice.I2CTransaction[] xaction;
        ushort DS1307_Address = 0x68;

        public void Dispose()
            xaction = null;

        public RTC_1307()
            //Create I2C object
            I2CDevice.Configuration conf = new I2CDevice.Configuration(DS1307_Address, 100);
            I2C = new I2CDevice(conf);

        public DateTime GetDateTime()
            xaction = new I2CDevice.I2CTransaction[2];
            xaction[0] = I2CDevice.CreateWriteTransaction(new byte[] { 0x00 });
            byte[] ReturnedDateTime = new byte[7];
            xaction[1] = I2CDevice.CreateReadTransaction(ReturnedDateTime);
            if (I2C.Execute(xaction, 1000) == 0)
                new Exception("Failed to send I2C data");

            int sec = bcdToDec(ReturnedDateTime[0]) & 0x7f;
            int min = bcdToDec(ReturnedDateTime[1]);
            int hour = bcdToDec(ReturnedDateTime[2]) & 0x3f;
            int dayofweek = bcdToDec(ReturnedDateTime[3]);
            int dayofmonth = bcdToDec(ReturnedDateTime[4]);
            int month = bcdToDec(ReturnedDateTime[5]);
            int year = bcdToDec(ReturnedDateTime[6]) + 2000;

            DateTime dt = new DateTime(year, month, dayofmonth, hour, min, sec);
            return dt;

        public void SetDateTime(DateTime datetime)
            xaction = new I2CDevice.I2CWriteTransaction[1];
            byte[] sb = new byte[8] { 0x00,
                                  decToBcd(datetime.Year - 2000)

            xaction[0] = I2CDevice.CreateWriteTransaction(sb);
            if (I2C.Execute(xaction, 1000) == 0)
                new Exception("Failed to send I2C data");

        private byte decToBcd(int val)
            return (byte)((val / 10 * 16) + (val % 10));

        private byte bcdToDec(byte val)
            return (byte)((val / 16 * 10) + (val % 16));

Yes, I had a number of Debug.Print messages throughout the code and everything is fine until it passes an hour, actually it usually get to about 1 hour 10 minutes. and then hangs. I’ve been working at it on and off for days and can’t see what the problem is.

When you say it stops working, what do you mean?

  • Does the code exit?
  • Is it stuck in one of your loops?
  • Does the processor seem to hang?
  • Does it just stop recording data and we don’t know where it is in the code?

My first thoughts would be:

  • Run under the debugger, when it stops hit the break button and see where it is executing.
  • Add some exception handling and see if an exception is being thrown
  • Since it stops after a certain amount of time and the code deals with time, be suspect of your time calculations throwing an error

I look forward to seeing what is happening. Welcome to the forum.

Thanks I’ll give what you say a try this evening.
I currently have a 5 second loop which outputs a debug message with every iteration, the messages stop so it would seem to be in this loop that things are currently hanging. I added the loop because I was using Thread.Sleep(600000) to get the 10 minute delay, and I thought this was causing the problem.

VS2015 doesn’t work correctly with 4.3 today. Use vs2013 please

Also another threads was posted today with a similar issue. We are looking into it.

Try using MFDEPLOY to debug instead of VS.
Open mfdeploy, click file>>connect it to your board and keep watch.
At least this will help eliminate the VS issue if it is the cause.

Also enable GC output and keep track of memory usage.
Btw I believe @ Gus is referring to this thread:


Thanks Gus and Jay,
I’ve installed VS2013 and the problem persists. I put in try and catch statements, but it just seems to stop working without any message. I’ve looked at MFDeploy but don’t know how to use it. I can ping the board, but presumably I should have a hex file to transfer to it.

Under \bin\Debug\ there is an exe file and a pdb file, but no hex file.

Sorry I should have added I used GC and there is 38K memory free.

Just reading the other thread regarding this issue. Its interesting that my program is stopping at 426x seconds most times. Occasionally it will stop sooner. If I have the main sensor reading loop set to either check the sensor every 5 minutes or 10 minutes it almost always stops at 4200 (plus a few) seconds.

It is the 1 hour, 11 minutes, 34 seconds. (The S4.294K bug.)

Just read Gus’s reply to DarrenUtd. I’ll wait for the man in red suit to bring the next release.