I hope that someone can help with this, it’s got me a little stuck.
I’m getting a System.IO exception that I can’t catch when trying to mount the SD card after having previously unmounted it. The RemovableMedia.Insert event fires as expected but then I get the exception when trying to access the file system and I can’t catch it.
This is what I’m using;
public SDcard()
{
try { sd_card = new SDCard(); }
catch { Debug.Print("No SD Card"); }
}
public static void MountSDcard()
{
try
{
RemovableMedia.Insert += new InsertEventHandler(SDMountComplete);
SDmounted = false;
sd_card.Mount();
//while (!SDmounted)
//{
// System.Threading.Thread.Sleep(50);
//}
}
catch (Exception e) { Debug.Print(e.ToString()); }
}
private static void SDMountComplete(object sender, MediaEventArgs e)
{
SDmounted = true;
RemovableMedia.Insert -= new InsertEventHandler(SDMountComplete);
}
public static void UnmountSDcard()
{
try { sd_card.Unmount(); SDmounted = false; }
catch (Exception e) { Debug.Print(e.ToString()); }
}
Firstly I mount the SD card, then read a text file with this;
public string FileReadToEnd(string readToEndFile)
{
string textToEndString;
//rootDirectory = VolumeInfo.GetVolumes()[0].RootDirectory;
textToEndStream = new FileStream(rootDirectory + @ "\" + readToEndFile, FileMode.Open, FileAccess.Read);
if (textToEndStream.CanRead)
{
TextReader textToEnd = new StreamReader(textToEndStream);
textToEndString = textToEnd.ReadToEnd();
textToEnd.Close();
textToEnd.Dispose();
return textToEndString;
}
else
{
throw new Exception("Cannot read from file:" + readToEndFile);
}
}
Unmount the SD card, then if I get a list of files on the card (inside a try/catch) after mounting the second time;
GetSDfiles()
{
rootDirectory = VolumeInfo.GetVolumes()[0].RootDirectory;
if (VolumeInfo.GetVolumes()[0].IsFormatted)
{
return Directory.GetFiles(rootDirectory);
}
else
{
Debug.Print("SD card is not formatted. Format on PC with FAT32/FAT16 first.");
return new string[] { };
}
}
I get an IO exception that is not caught and crashes the board (G120 on CobraIII).
Is it something to do with the filestream from the first file system access? I have to unmount the card because otherwise it is unreadable on a PC.
I noticed that Mount() also takes the card’s clock speed, SDCard() that takes an interface and also a method called ForceInitialization() but I am not sure whether these will help or not, or where to find the right values to pass to them?
Thanks