Something like this:
but please visit the link below for a complete correct way od handling the Dispose on Media removable event
using System.IO;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using GHI.IO;
using GHI.IO.Storage;
namespace MFClassLibrary2
{
public class Class1
{
public static void Main()
{
SDCard sd_card = new SDCard();
//do your thing here
//don't use it like this please refer to the docs below on hw to handle the dispose on Media removable event... :)
sd_card.Dispose();
}
}
}
@ Alex Bilityuk - If you are using the Gadgeteer mainboard driver for the FEZ Cerbuino Bee, you cannot get the internal instance of our GHI.IO.Storage.SDCard class (without using reflection). You need to use the provided Gadgeteer wrappers such as SDCardMounted, SDCardUnmounted, and SDCardStorageDevice which expose a GT.StorageDevice that you can use to access files. The events will be automatically triggered for you when you insert or eject an SD card.
using System;
using System.Threading;
using System.IO;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using Microsoft.SPOT.IO;
using GHI.IO;
using GHI.IO.Storage;
class Program
{
static SDCard sd_card = null;
static InputPort button = new InputPort((Cpu.Pin)xx, false, Port.ResistorMode.PullUp);
public static void Main()
{
while (true)
{
Thread.Sleep(100);
if (button.Read() == false) // starting test....
{
while(button.Read() == false) // wait for releasing the button...
{
Thread.Sleep(10);
}
if (sd_card != null)
{
if (sd_card.Mounted)
{
sd_card.Unmount();
Debug.Print("SD UnMounted");
}
sd_card.Dispose();
sd_card = null;
Thread.Sleep(10);
}
sd_card = new SDCard();
// if exception here:
// + Check wire or connection
// + Try diffirent card.
sd_card.Mount();
// if exception here:
// + reduce SD clock
// + Format FAT32 by PC
Debug.Print("SD Mounted");
}
}
}
}
No John, i already know how to read and write files from/to sd card. I am trying to remount sd card without exception. The first time after the device started i can read and write files to SD, but if i eject and insert sd back to slot, i can’t write and read files any more.
using System;
using System.Threading;
using System.IO;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using Microsoft.SPOT.IO;
using GHI.IO;
using GHI.IO.Storage;
class Program
{
static SDCard sd_card = null;
static InputPort button = new InputPort((Cpu.Pin)xx, false, Port.ResistorMode.PullUp);
public static void Main()
{
while (true)
{
Thread.Sleep(100);
if (button.Read() == false)
{
while(button.Read() == false)
{
Thread.Sleep(10);
}
if (sd_card != null)
{
if (sd_card.Mounted)
{
sd_card.Unmount();
Debug.Print("SD UnMounted");
}
sd_card.Dispose();
sd_card = null;
Thread.Sleep(10);
}
sd_card = new SDCard(); //----- HERE IS System.InvalidOperationException
sd_card.Mount();
Debug.Print("SD Mounted");
}
}
}
}
follow your picture, I can see the exception is thrown in the first time, it means not relate to what we are talking about. As I said, if the exception is raised when creating object, there are 2 reasons as you know.
I also tried a quick test, below is my result. The SD card is completely removed out then reinserted.
And below is my code, just add trycount variable to print out, in case you are interested in.
using System;
using System.Threading;
using System.IO;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using Microsoft.SPOT.IO;
using GHI.IO;
using GHI.IO.Storage;
namespace SD_Reinsert
{
public class Program
{
static SDCard sd_card = null;
static InputPort button = new InputPort((Cpu.Pin)(2 * 16 + 2), false, Port.ResistorMode.PullUp); //// Socket 4 - PC2 - pin 3
public static void Main()
{
int trytime = 0;
while (true)
{
Thread.Sleep(100);
if (button.Read() == false)
{
trytime++;
while (button.Read() == false)
{
Thread.Sleep(10);
}
if (sd_card != null)
{
if (sd_card.Mounted)
{
sd_card.Unmount();
Debug.Print("SD UnMounted " + (trytime));
}
sd_card.Dispose();
sd_card = null;
Thread.Sleep(10);
}
sd_card = new SDCard(); //----- HERE IS System.InvalidOperationException
sd_card.Mount();
Debug.Print("SD Mounted: " + (trytime));
}
}
}
}
}
I am not using your code, I posted your code to show only a logic… i am using a Cerbuino bee mainboard.
Ok here is my code without your button, just want to Mount() and UnMount() SD card:
using System;
using System.Threading;
using System.IO;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using Microsoft.SPOT.IO;
using GHI.IO;
using GHI.IO.Storage;
namespace SD_Reinsert
{
public class Program
{
static SDCard sd_card = null;
ProgramStarted()
{
if (sd_card != null)
{
if (sd_card.Mounted)
{
sd_card.Unmount();
Debug.Print("SD UnMounted");
}
sd_card.Dispose();
sd_card = null;
Thread.Sleep(10);
}
sd_card = new SDCard(); //----- HERE IS System.InvalidOperationException
sd_card.Mount();
Debug.Print("SD Mounted");
}
SD card is completly usable, because i am able to write and read data from it without problem
I tried 3 SD cards, with no sucess
I attached exception
I even can’t step into if (sd_card != null) close, because sd_card is not the current instance of onboard SDCard, it is just a null variable.