The first time this happened I thought it was a fluke, so I set my project to logging again. When I recovered the datafile, once again logging stopped in the same timespan (I've left the project to sit for 24+ hours in both cases). I'm going to try it again tonight just to see what happens, but I'd appreciate some insight.
Am I running out of memory? The first time I ran the project I had a lot of variable creation inside the while(true) loop portion of the code. My first instinct was that somehow the garbage collector wasn't doing its thing and so memory from a previous loop wasn't being released, or something. I'm not sure how TinyCLR does garbage collection. So, I moved a lot of variable creation to before the while(true) loop, but the same error repeated itself.
Here's the code as of right now:
public class Program { public static SerialPort talkback = new SerialPort("COM2", 14400, Parity.None, 8, StopBits.One); public static void Main() { RemovableMedia.Insert += new InsertEventHandler(SD_Insert); RemovableMedia.Eject += new EjectEventHandler(SD_Eject); int rejected = 0; int FilterNotch = 60; bool writeSuccessful = false; bool d1 = false; bool d2 = false; bool d3 = false; bool d4 = false; bool d5 = false; AD7714.Reset(); AD7714.ChannelSetup(1, d1, false, 24, false, true, FilterNotch); AD7714.ChannelSetup(2, d2, false, 24, false, true, FilterNotch); AD7714.ChannelSetup(3, d3, false, 24, false, true, FilterNotch); AD7714.ChannelSetup(4, d4, false, 24, false, true, FilterNotch); AD7714.ChannelSetup(5, d5, false, 24, false, true, FilterNotch); int backupCounts = 0; int backupFileCount = 0; if (Directory.Exists(@"SD")) { try { using (var filestream = new FileStream(@"SDdata.txt", FileMode.Append, FileAccess.Write)) { StreamWriter writer = new StreamWriter(filestream); writer.WriteLine("Machine_Time,Duration,Ch1_Counts,Ch1_Volts,Ch2_Counts,Ch2_Volts,Ch3_Counts,Ch3_Volts,Ch4_Counts,Ch4_Volts,Ch5_Counts,Ch5_Volts,Errors"); writer.Close(); } if (File.Exists(@"SDdata.txt")) { RGB.Blue(); LCD.Open(); LCD.Clear(); LCD.Print("Files Created."); LCD.Close(); Thread.Sleep(500); RGB.Off(); } else { RGB.Red(); LCD.Open(); LCD.Clear(); LCD.Print("Couldn't create!"); LCD.Close(); Thread.Sleep(Timeout.Infinite); } } catch (Exception ex) { Debug.Print("You dun goofed: " + ex.Message); } } long start = 0; long end = 0; long duration; int[] Channel = new int[5]; while (true) { start = Utility.GetMachineTime().Ticks; for (int i = 0; i < 5; i++) { Channel[i] = AD7714.ReadChannel(i, 1, false, 40); if (Channel[i] == 8388608) { rejected++; } } end = Utility.GetMachineTime().Ticks; duration = (end - start) / 10000; if (duration < 800) { string timeData = Utility.GetMachineTime().ToString() + "," + duration.ToString() + " "; string countsData = string.Empty; for (int i = 0; i < 5; i++) { countsData += Channel[i].ToString() + ","; } countsData.TrimEnd(countsData[countsData.Length - 1]); countsData += " "; double[] volts = new double[5]; string voltsData = String.Empty; for (int i = 0; i < 5; i++) { volts[i] = (double)Channel[i] * 3.0 / 16777215.0; voltsData += volts[i] + ","; } voltsData.TrimEnd(voltsData[voltsData.Length - 1]); voltsData += " "; string errorsData = rejected.ToString() + "END"; string xmitData = timeData + countsData + voltsData + errorsData; string fileData = timeData.Trim() + ","; for (int i = 0; i < 5; i++) { fileData += Channel[i].ToString() + "," + volts[i] + ","; } fileData += rejected.ToString(); if (File.Exists(@"SDdata.txt")) { try { using (var filestream = new FileStream(@"SDdata.txt", FileMode.Append, FileAccess.Write)) { StreamWriter writer = new StreamWriter(filestream); writer.WriteLine(fileData); writer.Close(); writeSuccessful = true; } } catch (Exception ex) { Debug.Print("I found an " + ex.Message); } } else { writeSuccessful = false; } if (writeSuccessful) { RGB.Yellow(); Thread.Sleep(500); RGB.Green(); Thread.Sleep(500); RGB.Off(); } #region Serial Communications byte[] xmit = Encoding.UTF8.GetBytes(xmitData); talkback.Open(); talkback.Write(xmit, 0, xmit.Length); talkback.Close(); #endregion } else { rejected++; AD7714.Reset(); Thread.Sleep(1000); } backupCounts++; if (backupCounts == 14400) // 4-hour backups { if (File.Exists(@"SDdata.txt")) { File.Copy(@"SDdata.txt", @"SDdata" + backupFileCount.ToString() + ".bak"); backupFileCount++; } backupCounts = 0; } } } static void SD_Insert(object sender, MediaEventArgs args) { if (Directory.Exists(@"SD")) { } } static void SD_Eject(object sender, MediaEventArgs args) { if (!Directory.Exists(@"SD")) { } } }
AD7714 is class I wrote to control an external ADC, RGB is a class that controls an RGB LED, and LCD is a class that controls a serial LCD screen. Nothing important there. The variables created on lines 85-88 are the ones I have moved out of the loop to see it that helps.
The only thing I can think to do is run the project with the debugger attached for 14 hours and see what exceptions start getting thrown. Otherwise, I'm stumped.