Hello,
I’m trying to use my SD card on the FEZ Cerbuino and it works fine when the card is plugged in. But i was wondering of there is a way to check of the card is present or not with the OSHW version?
I have found there where some possibilities in the Premium version, but i do think that i over looked the options for the OSHW. Did i?
my current code looks like:
GHI.OSHW.Hardware.StorageDev.MountSD();
if (VolumeInfo.GetVolumes()[0].IsFormatted)
{
_root = VolumeInfo.GetVolumes()[0].RootDirectory;
}
if (File.Exists(_root + @ "\config.json"))
{
var sr = new StreamReader(File.OpenRead(_root + @ "\config.json"));
text = sr.ReadToEnd();
sr.Close();
}
else
{
DeviceID = Guid.NewGuid();
CurrentTime = DateTime.Now;
text = serializer.Serialize(this);
var sw = new StreamWriter(File.Create(_root + @ "\config.json"));
sw.WriteLine(text);
sw.Close();
}
GHI.OSHW.Hardware.StorageDev.UnmountSD();
Someone can give me a shot in the right direction. Also I love to see an option that i can detect of the Card is plugged in or is just removed. I know there are options in the Premium as well.
This is what I use on my Cerbuino:
public sdCardHandler(SDCard sdCard)
{
_sdCard = sdCard;
_sdCard.SDCardMounted += new SDCard.SDCardMountedEventHandler(sdCard_SDCardMounted);
_sdCard.SDCardUnmounted += new SDCard.SDCardUnmountedEventHandler(sdCard_SDCardUnmounted);
Microsoft.SPOT.IO.RemovableMedia.Eject += new Microsoft.SPOT.IO.EjectEventHandler(RemovableMedia_Eject);
Microsoft.SPOT.IO.RemovableMedia.Insert += new Microsoft.SPOT.IO.InsertEventHandler(RemovableMedia_Insert);
}
#region events
void sdCard_SDCardUnmounted(SDCard sender)
{
// The SD card has been unmounted
// DO NOT try to access it without mounting it again first
_isMounted = false;
DebugPrint("Event Unmounted");
}
void sdCard_SDCardMounted(SDCard sender, GT.StorageDevice SDCard)
{
// SD card has been successfully mounted. You can now read/write/create/delete files
// Unmount before removing
_isMounted = true;
// Get the root directory
_rootDirectory = _sdCard.GetStorageDevice().RootDirectory + @ "\"
DebugPrint("Event Mounted. RootDir : " + _rootDirectory);
}
void RemovableMedia_Insert(object sender, Microsoft.SPOT.IO.MediaEventArgs e)
{
_isInserted = true;
DebugPrint("Event Inserted : " + e.Volume.Name);
}
void RemovableMedia_Eject(object sender, Microsoft.SPOT.IO.MediaEventArgs e)
{
_isInserted = false;
_isMounted = false;
DebugPrint("Event Ejected : " + e.Volume.Name);
}
#endregion
Sorry, it looks good and I’'m going to test it soon.
Here is a more complete example that you can run directly on the Cerberus:
using System;
using System.Collections;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Touch;
using System.IO;
using Gadgeteer.Modules.GHIElectronics;
using Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
namespace GadgeteerApp2
{
public partial class Program
{
void ProgramStarted()
{
Debug.Print("Program Started");
sdCard.SDCardMounted += new SDCard.SDCardMountedEventHandler(sdCard_SDCardMounted);
sdCard.SDCardMounted += new SDCard.SDCardMountedEventHandler(sdCard_SDCardMounted);
sdCard.SDCardUnmounted += new SDCard.SDCardUnmountedEventHandler(sdCard_SDCardUnmounted);
Microsoft.SPOT.IO.RemovableMedia.Eject += new Microsoft.SPOT.IO.EjectEventHandler(RemovableMedia_Eject);
Microsoft.SPOT.IO.RemovableMedia.Insert += new Microsoft.SPOT.IO.InsertEventHandler(RemovableMedia_Insert);
sdCard.MountSDCard();
System.Threading.Thread.Sleep(500);
}
void sdCard_SDCardUnmounted(SDCard sender)
{
Debug.Print("Event Unmounted");
}
void sdCard_SDCardMounted(SDCard sender, GT.StorageDevice SDCard)
{
Debug.Print("Mounted");
writeSDcard();
}
void RemovableMedia_Insert(object sender, Microsoft.SPOT.IO.MediaEventArgs e)
{
Debug.Print("Event Inserted : " + e.Volume.Name);
}
void RemovableMedia_Eject(object sender, Microsoft.SPOT.IO.MediaEventArgs e)
{
Debug.Print("Event Ejected : " + e.Volume.Name);
}
void writeSDcard()
{
//Get the root directory
string rootDirectory = sdCard.GetStorageDevice().RootDirectory;
Debug.Print("Writing to : " + rootDirectory);
//Use Streamwriter to write a text file
StreamWriter textFile = new StreamWriter(rootDirectory + @ "\hello.txt");
textFile.WriteLine("Hello SD card");
textFile.Close();
}
}
}