Tuesday, September 27, 2011

Netduino - SD Write, Right?

I've decided to upgrade to the latest firmware 4.2.0; although this does not have 1-wire support it does fix a lot of other issues and when Chris over at secret labs gets round to releasing the 1-wire version I will inlude temperature readings back in my project.

In the meantime....

I have successfully managed to write a file to an sd card and then read it back.

Currently this was achieved using 2 seperate examples:

To write it I used this:


public static void Main()
{
StorageDevice.MountSD("SD", SPI.SPI_module.SPI1, Pins.GPIO_PIN_D8);

using (var filestream = new FileStream(@"SD\dontpanic.txt", FileMode.Create))
{
StreamWriter streamWriter = new StreamWriter(filestream);
streamWriter.WriteLine("This is a test of the SD card support on the netduino...This is only a test...");
streamWriter.Close();
}

using (var filestream = new FileStream(@"SD\dontpanic.txt", FileMode.Open))
{
StreamReader reader = new StreamReader(filestream);
Debug.Print(reader.ReadToEnd());
reader.Close();
}
StorageDevice.Unmount("SD");
}


To read it I used this:


public static void Main()
{
StorageDevice.MountSD("SD1", SPI_Devices.SPI1, Pins.GPIO_PIN_D8);

string[] directories = System.IO.Directory.GetDirectories(@"\");
Debug.Print("directory count: " + directories.Length.ToString());

for (int i = 0; i < directories.Length; i++)
{
Debug.Print("directory: " + directories[i]);
}

string[] files = System.IO.Directory.GetFiles(@"\SD1");
Debug.Print("file count: " + files.Length.ToString());

for (int i = 0; i < files.Length; i++)
{
Debug.Print("filename: " + files[i]);
FileStream fs = new FileStream(files[i], FileMode.Open, FileAccess.Read, FileShare.None, 512);
StreamReader sr = new StreamReader(fs);
Debug.Print("contents: " + sr.ReadToEnd());
}
Thread.Sleep(20000);
}


The next plan is to test I can write, read, delete, edit and also control shift registers on the same bus.

No comments:

Post a Comment