Handling Mount() UnMount() SD events

@ Alex Bilityuk - I ask what your end goal is when using an SD card so I can write an example for you that does not use SDCard. You cannot use SDCard in a Gadgeteer program with the FEZ Cerbuino Bee, FEZ Cerbuino Net, or FEZ Cobra II Eco because they work with SDCard internally and provide Gadgeteer wrappers for you.

Dat posted a code that doesn’t work for my board… I attached the exception picture it doesn’t work for my board…

Dat, i need to make it work in Gadgeteer project, i don’t need it in microframework project

John, my full project is on Gadgeteer. I control the robo using WiFly module, and send commands on it. You mean if i need to control SdCard Mount() and UnMount() methods i should use a Microframework project instead of Gadgeteer?

@ Alex Bilityuk - No, you can use SD cards through Gadgeteer. You just cannot use the SDCard class on the Cerbuino mainboard. The SDCardMounted event on the Mainboard object gives you a Gadgeteer.StorageDevice object that you can use to read and write files. That object is valid until you receive an SDCardUnmounted event.

John, can you do me a favour? post a code snippet please… how to use SDCardMounted event on the mainboard object to mount and unmount SD card in Gadgeteer project… thank you.

@ Alex Bilityuk - You can also search the forum. There are some examples:

https://www.ghielectronics.com/community/forum/search?q=SDCardUnmounted

@ Alex Bilityuk - This will write a file to the SD card as soon as it is inserted.


using Microsoft.SPOT;
using System.Text;
using GT = Gadgeteer;

namespace GadgeteerApp1
{
    public partial class Program
    {
        private void ProgramStarted()
        {
            Mainboard.SDCardMounted += this.Mainboard_SDCardMounted;
            Mainboard.SDCardUnmounted += this.Mainboard_SDCardUnmounted;
        }

        private void Mainboard_SDCardUnmounted(GHIElectronics.Gadgeteer.FEZCerbuinoNet sender, EventArgs e)
        {
            Debug.Print("SD card removed.");
        }

        private void Mainboard_SDCardMounted(GHIElectronics.Gadgeteer.FEZCerbuinoNet sender, GT.StorageDevice device)
        {
            Debug.Print("SD card inserted.");

            device.WriteFile("Test.txt", Encoding.UTF8.GetBytes("Hello, world!"));
        }
    }
}

John, tried your code as is.

Unmount event is fired well

Mount event throws an exception: A first chance exception of type ‘System.Exception’ occurred in GHI.Hardware.dll (picture attached)

How to fire Mainboard_SDCardMounted event it never fires when the SDCard inserted…


using System;
using System.IO;
using System.Collections;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.IO;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Presentation.Shapes;
using Microsoft.SPOT.Touch;
using GHI.IO;
using GHI.IO.Storage;

using Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using System.Text;

namespace SDTest
{
    public partial class Program
    {
        
        void ProgramStarted()
        {
            Mainboard.SDCardMounted += Mainboard_SDCardMounted;
            Mainboard.SDCardUnmounted += Mainboard_SDCardUnmounted;
            while (Mainboard.IsSDCardInserted)
            {
                while (Mainboard.IsSDCardMounted)
                {
                    Mainboard.MountStorageDevice(Mainboard.SDCardStorageDevice.Volume.Name.ToString());

  Thread.Sleep(1000); 
                }
                
                Thread.Sleep(1000);
            }
            
        }

        void Mainboard_SDCardUnmounted(GHIElectronics.Gadgeteer.FEZCerbuinoBee sender, EventArgs e)
        {
            Debug.Print("SD is Unmounted");
        }

        void Mainboard_SDCardMounted(GHIElectronics.Gadgeteer.FEZCerbuinoBee sender, GT.StorageDevice device)
        {
            
            Debug.Print("SD is mounted");
            //device.WriteFile("Test.txt", Encoding.UTF8.GetBytes("Hello, world!"));
        }

Alex, do NOT use the while (true) pattern in Gadgeteer ProgramStarted(). Let ProgramStarted finish. Unless it finishes, the handlers will be in an inconsistent state. http://blogs.msdn.com/b/net_gadgeteer/archive/2011/12/19/why-not-while-true.aspx

Brett, how to automatically fire SDCardMounted event?

its an event - if it is triggered, it will fire. If you think the way you’re using the device is not triggering that event then explain what you did to let others know - Dat or John @ GHI will be able to validate what they see versus what you see. But you have to get rid of that while() loop in your program started and go back to John’s original code

Alex - Just tested this.

Ok, i just want to trigger SDCardMounted event automatically. How to do that? When i insert SD to slot this event is not triggerd.



using System;
using System.IO;
using System.Collections;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.IO;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Presentation.Shapes;
using Microsoft.SPOT.Touch;
using GHI.IO;
using GHI.IO.Storage;

using Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using System.Text;

namespace SDTest
{
    public partial class Program
    {
        
        void ProgramStarted()
        {
            Mainboard.SDCardMounted += Mainboard_SDCardMounted;
            Mainboard.SDCardUnmounted += Mainboard_SDCardUnmounted;           
            
        }

        void Mainboard_SDCardUnmounted(GHIElectronics.Gadgeteer.FEZCerbuinoBee sender, EventArgs e)
        {
            Debug.Print("SD is Unmounted");
        }

        void Mainboard_SDCardMounted(GHIElectronics.Gadgeteer.FEZCerbuinoBee sender, GT.StorageDevice device)
        {
            
            Debug.Print("SD is mounted");
            //device.WriteFile("Test.txt", Encoding.UTF8.GetBytes("Hello, world!"));
        }

The event should be triggered automatically when the card is inserted. It will not trigger if the card is already inserted when you power up the board.

Here is mine. Just tested :frowning:

In my case it doesn’t trigger not after nor before the power is up…i tried to insert the card before power up and after power up, insert/eject during power is up but with no sucess. Instead of Sd is mounted in output window i see this exception

@ Alex Bilityuk -

I was able reproduce your issues.
Try to format your cards by PC.

It doesn’t help me. I formatted my card in FAT32, the same thing… the mounted event is not fired automatically

:slight_smile: