Handling Mount() UnMount() SD events

@ Gus - Then I guess it makes sense to dispose the “media” object if it is removed??

1 Like

I am glad to here from you Dat! Can you post a code snippet please. I don’t know how to get actual SDCard object to dispose it.

No it is not enough. I am using onboard sd reader. How to get the instance of this reader??

I can’t use:


GHI.IO.Storage.SDCard sd = new SDCard(); // It throws exception InvalidOperationexception

I can’t use just:


GHI.IO.Storage.SDCard sd;
sd.Dispose() // Here will be a null reference exception because it's not the instance of current SD onboard reader.

How to get the instance of current SDCard???

Something like this:
but please visit the link below for a complete correct way od handling the Dispose on Media removable event :slight_smile:


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();
        }
  
    }
}

Source:
https://www.ghielectronics.com/docs/51/accessing-folders-and-files

Jay

1 Like

@ 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.

1 Like

The small code below can use to test your issue.

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");
            }
        }

    }
}
1 Like

This is a first code sample i start from. It doesn’t work for me.

Thank you for your code but it doesn’t work… i already tried the same code (picture of exception attached)

John, could you please give me more details of how to use SDCardMounted, SDCardUnmounted, and SDCardStorageDevice? Code snippet would be fantastic…

@ Alex Bilityuk - What exactly are you trying to accomplish using the SD Card? Just reading and writing files?

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.

Try this before the line with exception



if (sd_card != null)
                 {
                     
                     sd_card.Dispose();
                     sd_card = null;
                     Thread.Sleep(10);
                 }
sd_card = new SDCard();

@ Alex Bilityuk - What exception do you get? Can you post a minimal program that causes the exception to happen?

Here is a code:

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");
            }
        }

    }
}

Exception picture attached

@ Alex Bilityuk -

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));
                   
                }
            }

        }

    }
}

and,

If you are using exactly my code, it means non-gadgeteer, how did you get
"Using mainboard GHI Electronic FEZ Cerbuino Bee…"? :think:

The SD gadgeteer driver may still keep or change the object somewhere, I think.

Also,

I tested on Cerberus and Cerbuio bee, work great!

  • You are using Cerbuino Bee board, it mean connection is always good, because no wire needed.
  • You can read/write for first time, it mean the board is happy with that SD.
  • As I tested here, firmware is OK.

So, the rest 99,9% is your code, that is what I am thinking :smiley:

@ Alex Bilityuk - Dat is correct. You cannot use our SDCard class when you are using the Gadgeteer mainboard. We keep an instance of it internally.

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");
        
}

  1. SD card is completly usable, because i am able to write and read data from it without problem
  2. I tried 3 SD cards, with no sucess
  3. I attached exception
  4. 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.

Tell me please wich class should i use and how to instantiate SDCard variable?

What are you thinking?

John, Dat, please give me a details what i have to do to make my code work?

@ Alex Bilityuk -

I read somewhere, someone found that we should not access SD in ProgramStart() if using gadgeteer.

And removing my button cause your program repeat creating SD instance, mount/unmount in short time… I don’t think it good.

Create new project, select “Micro Framework” instead of “Gadgeteer”, paste my code, add some references, and run.

Or you can send directly us email, we will zip example project, just unzip it and run.

Or you can send us your example project, we will try correct for you.

Many options for you :smiley: