I just acquired a Panda II and like to use it as a CAN datalogger in automotive application.
Before deploying it to the vehicle, I first tested it logging date and time data to SD card with the codes below (most are cut and paste from the FEZ beginners’ guide and from this forum). A new file is created with date and time as filename in every loop executed. It worked ok but stopped logging once the SD card is full.
Like to seek advise on how to make it detect the SD card is full and overwrite the oldest file. Ideally, I like to leave the device running in the vehicle and only retrieve data when something abnormal happens.
I am a novice in programming, any help is appreciated. Thanks in advance for any advise. Thanks!
using System.Threading;
using System.IO;
using Microsoft.SPOT;
using Microsoft.SPOT.IO;
using Microsoft.SPOT.Hardware;
using System;
using GHIElectronics.NETMF.IO;
using GHIElectronics.NETMF.Hardware;
using GHIElectronics.NETMF.System;
namespace Time_datalog
{
public class Program
{
public static void Main()
{
// To keep track of time, set it at the beginning of your application from the RTC.
// If the time was never set before, set it to a certain date and time.
/*bool timeValid = false;
if (timeValid == false)
RealTimeClock.SetTime(DateTime.Today);
timeValid = true;
*/
// Set the system time. CAN messages will have a time stamp
Utility.SetLocalTime(RealTimeClock.GetTime());
bool CardIn = false;
RemovableMedia.Insert += new InsertEventHandler(RemovableMedia_Insert);
RemovableMedia.Eject += new EjectEventHandler(RemovableMedia_Eject);
try
{
// try to access the microSD
PersistentStorage sdPS = new PersistentStorage("SD");
sdPS.MountFileSystem();
CardIn = true;
}
catch
{
// if failed, assume no card in slot
Debug.Print("No SD card!");
CardIn = false;
}
while (CardIn)
{
using (var w = new StreamWriter(@ "\SD\DataLogFile_" + DateTime.Now.ToString("MMddyyyy") + "_" + DateTime.Now.ToString("hhmmss") + ".txt", true))
{// Write data
DateTime now = DateTime.Now;
string iniLine = "date " + now.ToString("ddd MMM dd h:mm:ss ") + now.ToString("tt ").ToLower() + now.ToString("yyyy");
Debug.Print(iniLine);
w.WriteLine(iniLine);
for (int i = 0; i < 2000; i++)
{
string Nextline = DateTime.Now.ToString();
//Debug.Print(Nextline);
w.WriteLine(Nextline);
Thread.Sleep(50);
}
}
}
}
static void RemovableMedia_Eject(object sender, MediaEventArgs e)
{
Debug.Print("SD card ejected");
}
static void RemovableMedia_Insert(object sender, MediaEventArgs e)
{
Debug.Print("SD card inserted");
if (e.Volume.IsFormatted)
{
Debug.Print("Available folders:");
string[] strs = Directory.GetDirectories(e.Volume.RootDirectory);
for (int i = 0; i < strs.Length; i++)
Debug.Print(strs[i]);
Debug.Print("Available files:");
strs = Directory.GetFiles(e.Volume.RootDirectory);
for (int i = 0; i < strs.Length; i++)
Debug.Print(strs[i]);
}
else
{
Debug.Print("SD card is not formatted");
}
}
}
}