I am looking to do something like this:
http://code.tinyclr....gram-logging-2/
On the netduino, is there such/similar? (It does not just compile...)
Thanks!
![Photo](http://www.gravatar.com/avatar/2022a611decf43d629d3523b9de3fefb?s=100&d=http%3A%2F%2Fforums.netduino.com%2Fpublic%2Fstyle_images%2Fmaster%2Fprofile%2Fdefault_large.png)
SD Logging: Is there a netduino version of this lib?
Started by samjones, Feb 01 2012 05:06 AM
3 replies to this topic
#1
Posted 01 February 2012 - 05:06 AM
#2
Posted 01 February 2012 - 12:14 PM
Hi samjones,
On your Netduino Plus, the SD card is mounted at @"\SD\" automatically when inserted. For a classic Netduino, follow the mounting instructions provided in the firmware download thread for v4.1.1 or v4.2 beta.
Chris
I'd recommend removing the vendor-specific commands and just using the built-in .NET System.IO classes.I am looking to do something like this:
http://code.tinyclr....gram-logging-2/
On the netduino, is there such/similar? (It does not just compile...)
On your Netduino Plus, the SD card is mounted at @"\SD\" automatically when inserted. For a classic Netduino, follow the mounting instructions provided in the firmware download thread for v4.1.1 or v4.2 beta.
Chris
#3
Posted 01 February 2012 - 10:21 PM
Try this. I replaced the mounting code with a new method to check if the SD volume exists. Did a few tests and seems to work great.
using System; using System.IO; using System.Text; using Microsoft.SPOT; using Microsoft.SPOT.IO; namespace SDLogging { public class SDLogger : IDisposable { bool cardLoaded = true; FileStream fileStream; string path; bool FileOpen = false; public bool DebugMirror = true; public SDLogger() { try { // try to access the microSD if (VolumeExist() == false) { throw new Exception("microSD not found"); } // get the path string rootDirectory = VolumeInfo.GetVolumes()[0].RootDirectory; int idx; try { // get the next number for the log, to try to make 'em unique so not overwritten idx = getIndex(rootDirectory); if (DebugMirror) Debug.Print("Try : " + idx.ToString()); } catch { // if can't get number, use 0; idx = 0; if (DebugMirror) Debug.Print("Catch : " + idx.ToString()); } // open file if (DebugMirror) Debug.Print("Open file : " + idx.ToString()); path = rootDirectory + @"\log " + idx + ".csv"; if (DebugMirror) Debug.Print(path.ToString()); //fileStream = new FileStream(rootDirectory + @"\log " + idx + ".csv", FileMode.Create, FileAccess.Write); fileStream = new FileStream(path, FileMode.Create, FileAccess.Write); FileOpen = true; } catch { // if failed, assume no card in slot cardLoaded = false; } } // get the next number from the index.txt file. int getIndex(string rootDirectory) { // open the index file to get the next number FileStream fileHandle = new FileStream(rootDirectory + @"\index.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite); byte[] data = new byte[100]; // read the data int readCount = fileHandle.Read(data, 0, data.Length); int idx; if (readCount != 0) { // convert string data to int var str = new string(Encoding.UTF8.GetChars(data), 0, readCount); try { // increment last log # idx = int.Parse(str) + 1; } catch { idx = 1; } } else idx = 1; // close the file so that it can be overwritten fileHandle.SetLength(0); // store this number byte[] data2 = Encoding.UTF8.GetBytes(idx.ToString()); fileHandle.Write(data2, 0, data2.Length); // close handle fileHandle.Close(); if (DebugMirror) Debug.Print("getIndex = " + idx.ToString()); return idx; } // If you want to do some customized logging, just create a new method similar to the ones below // just write out a string public void LogInfo(string Info) { // if card present, then write if (cardLoaded) { Debug.Print("Going into LogInfo"); byte[] data = Encoding.UTF8.GetBytes(getTicks() + "," + "\"C\"" + ",\"" + Info + "\"\r\n"); // write the data and close the file try { if (FileOpen) // if file is open then write to it { fileStream.Write(data, 0, data.Length); } else // if file is not open, then open it to append and write to the end { fileStream = new FileStream(path, FileMode.Append); fileStream.Write(data, 0, data.Length); } } catch { } finally { Close(); } if (DebugMirror) Debug.Print(getTicks() + "," + "\"C\"" + ",\"" + Info); } } // write out the data from the Sharp IRs public void LogReading(double leftReading, double rightReading) { if (cardLoaded) { Debug.Print("Going into LogReading"); // write CSV data line byte[] data = Encoding.UTF8.GetBytes(getTicks() + "," + "\"R\"" + "," + leftReading.ToString("F2") + "," + rightReading.ToString("F2") + "\r\n"); // write the data and close the file try { if (FileOpen) // if file is open then write to it { fileStream.Write(data, 0, data.Length); } else // if file is not open, then open it to append and write to the end { fileStream = new FileStream(path, FileMode.Append); fileStream.Write(data, 0, data.Length); } } catch { } finally { Close(); } if (DebugMirror) Debug.Print(getTicks() + "," + "\"R\"" + "," + leftReading.ToString("F2") + "," + rightReading.ToString("F2")); } } // write out the data from the Sharp IRs and the corresponding motor speeds public void LogReadingAndSpeed(double leftReading, int leftSpeed, double rightReading, int rightSpeed) { if (cardLoaded) { Debug.Print("Going into LogReadingAndSpeed"); // write CSV data line byte[] data = Encoding.UTF8.GetBytes(getTicks() + "," + "\"S\"" + "," + leftReading.ToString("F2") + "," + leftSpeed + "," + rightReading.ToString("F2") + "," + rightSpeed + "\r\n"); // write the data and close the file try { if (FileOpen) // if file is open then write to it { fileStream.Write(data, 0, data.Length); } else // if file is not open, then open it to append and write to the end { fileStream = new FileStream(path, FileMode.Append); fileStream.Write(data, 0, data.Length); } } catch { } finally { Close(); } if (DebugMirror) Debug.Print(getTicks() + "," + "\"S\"" + "," + leftReading.ToString("F2") + "," + leftSpeed + "," + rightReading.ToString("F2") + "," + rightSpeed); } } public void Close() { fileStream.Close(); fileStream = null; FileOpen = false; } public void Dispose() { // likely never called, as the log will be open as long as system is running. fileStream.Close(); fileStream = null; } long getTicks() { // change this if you want a different timestamp marker return (DateTime.Now.Ticks / 1000) - 128752416000000; } // Check if the SD card is mounted private static bool VolumeExist() { VolumeInfo[] volumes = VolumeInfo.GetVolumes(); foreach (VolumeInfo volumeInfo in volumes) { if (volumeInfo.Name.Equals("SD")) return true; } return false; } } }
#4
Posted 01 February 2012 - 11:02 PM
@ Chris Walker: I tried, but failed... there was more to it than my abilities could figure out.
@ dvanderwekke: Thank you! I will try this over the weekend, or as soon as I can!
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users