// Class to interface with the SD card using System; using System.Threading; using System.Text; using Microsoft.SPOT; using Microsoft.SPOT.IO; using Microsoft.SPOT.Hardware; using System.IO; using GHI.IO.Storage; using GHI.Processor; namespace IMULogger { public class SDMemory { private static SDCard sdCard; //---------------------------------------------------------------------------------------------------- // Method: Create a new file on SD card //---------------------------------------------------------------------------------------------------- public static void SDNewFile(string fileName, string text) { // if necessary, check that SD is present here... sdCard = new SDCard(); sdCard.Mount(); bool fs_ready = false; RemovableMedia.Insert += (a, b) => { fs_ready = true; }; while (!fs_ready) { System.Threading.Thread.Sleep(1); } // Assume only one storage device is available // and that the media is formatted string rootDirectory = VolumeInfo.GetVolumes()[0].RootDirectory; StreamWriter textFile = new StreamWriter(rootDirectory + fileName, false); textFile.WriteLine(text); textFile.Flush(); textFile.Close(); sdCard.Unmount(); sdCard.Dispose(); } //---------------------------------------------------------------------------------------------------- // Method: Write data to a file on SD card //---------------------------------------------------------------------------------------------------- private static StreamWriter textFile = new StreamWriter(rootDirectory + fileName, true); private static string rootDirectory = VolumeInfo.GetVolumes()[0].RootDirectory; public static void SDWrite(string fileName, string text) { // if necessary, check that SD is present here... sdCard = new SDCard(); sdCard.Mount(); bool fs_ready = false; RemovableMedia.Insert += (a, b) => { fs_ready = true; }; while (!fs_ready) { System.Threading.Thread.Sleep(1); } // Assume only one storage device is available // and that the media is formatted textFile.WriteLine(text); textFile.Flush(); textFile.Close(); sdCard.Unmount(); sdCard.Dispose(); } //---------------------------------------------------------------------------------------------------- // Method: Read data from SD card file //---------------------------------------------------------------------------------------------------- public static void SDRead(string fileName) { // ... If desired, check if SD is inserted // SD Card is inserted // Create a new storage device SDCard sdCard = new SDCard(); // Mount the file system sdCard.Mount(); bool fs_ready = false; RemovableMedia.Insert += (a, b) => { fs_ready = true; }; while (!fs_ready) { System.Threading.Thread.Sleep(1); } // Assume one storage device is available, // access it through NETMF string rootDirectory = VolumeInfo.GetVolumes()[0].RootDirectory; FileStream FileHandle = new FileStream(rootDirectory + fileName, FileMode.Open, FileAccess.Read); // Buffer size to read bytes from file byte[] data = new byte[10000]; int read_count = FileHandle.Read(data, 0, data.Length); FileHandle.Close(); Debug.Print("\n\nThe size of data read is: " + read_count.ToString()); Debug.Print("Data from file:"); Debug.Print(new string(Encoding.UTF8.GetChars(data), 0, read_count)); sdCard.Unmount(); sdCard.Dispose(); } } }