Main Site Documentation

Problem Accessing SD Card


#1

I have a GHI Spider. I am using VC# 2012 Express. I connected a button, display T35, and a GHI SD Card version 1.4.
I have used all but the SD Card OK.

I tried to follow the SD Card example NETMF API-> NETMF IO -> PersistentStorage.

When I try to debug the code, I get


...
Using mainboard GHIElectronics-FEZSpider version 1.0
    #### Exception System.InvalidOperationException - CLR_E_INVALID_OPERATION (1) ####
    #### Message: 
    #### GHIElectronics.NETMF.IO.PersistentStorage::.ctor [IP: 0000] ####
    #### SDCard.Program::ProgramStarted [IP: 002f] ####
    #### SDCard.Program::Main [IP: 0015] ####
A first chance exception of type 'System.InvalidOperationException' occurred in GHIElectronics.NETMF.IO.dll
An unhandled exception of type 'System.InvalidOperationException' occurred in GHIElectronics.NETMF.IO.dll
<\code]

The exception happens when attempting
ps = new PersistentStorage("SD");
which I put in the ProgramStarted() function.

#2

hi Tom,

Do you also have VC# 2010 by any chance? I’d suggest trying that, because I think officially the netmf SDK is not yet supported in VS11.

Having said that, an error like that usually means the SDK and firmware versions don’t match - do they? Can you repro this on a new project that only adds the SD card?


#3

please post your code. I think you might be combining non-gadgeteer code with gadgeteer code.


#4

I AM using VC# 2010 ;D In gadgeteer, I added spider, USB DP, display t35,
SDCard. Then used auto connect.

Program.cs


using System;
using System.IO;
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 Microsoft.SPOT.IO;

using Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using Gadgeteer.Modules.GHIElectronics;
using GHIElectronics.NETMF.IO;

namespace SDCard
{
    public partial class Program
    {
        // Hold a static reverence in case GC kicks in...
        static PersistentStorage ps;

        // This method is run when the mainboard is powered up or reset.   
        void ProgramStarted()
        {
            /*******************************************************************************************
            Modules added in the Program.gadgeteer designer view are used by typing 
            their name followed by a period, e.g.  button.  or  camera.
            
            Many modules generate useful events. Type +=<tab><tab> to add a handler to an event, e.g.:
                button.ButtonPressed +=<tab><tab>
            
            If you want to do something periodically, use a GT.Timer and handle its Tick event, e.g.:
                GT.Timer timer = new GT.Timer(1000); // every second (1000ms)
                timer.Tick +=<tab><tab>
                timer.Start();
            *******************************************************************************************/

            button.ButtonPressed += new Button.ButtonEventHandler(button_ButtonPressed);
            RemovableMedia.Insert += new InsertEventHandler(RemovableMedia_Insert);
            RemovableMedia.Eject += new EjectEventHandler(RemovableMedia_Eject);

            // Assume SD card is inserted
            // Create a new storage device
            if (PersistentStorage.DetectSDCard())
            {
                Debug.Print("SD Card detected");
            }
            ps = new PersistentStorage("SD");
            ps.MountFileSystem();

            // Sleep forever
            Thread.Sleep(Timeout.Infinite);
        }

        void RemovableMedia_Insert(object sender, MediaEventArgs e)
        {
            Debug.Print("Storage \"" + e.Volume.RootDirectory + "\" is inserted.");
            Debug.Print("Getting files and folders:");
            if (e.Volume.IsFormatted)
            {
                string[] files = Directory.GetFiles(e.Volume.RootDirectory);
                string[] folders = Directory.GetDirectories(e.Volume.RootDirectory);
                                Debug.Print("Files available on " + e.Volume.RootDirectory + ":");
                for (int i = 0; i < files.Length; i++)
                    Debug.Print(files[i]);

                Debug.Print("Folders available on " + e.Volume.RootDirectory + ":");
                for (int i = 0; i < folders.Length; i++)
                    Debug.Print(folders[i]);
            }
            else
            {
                Debug.Print("Storage is not formatted. Format on PC with FAT32/FAT16 first.");
            }

            // We do not need it any more
            ps.Dispose();
        }

        void RemovableMedia_Eject(object sender, MediaEventArgs e)
        {
            Debug.Print("Storage \"" + e.Volume.RootDirectory + "\" is ejected.");
        }

        void button_ButtonPressed(Button sender, Button.ButtonState state)
        {
            Debug.Print("Button Pressed");
            button.ToggleLED();
        }
    }
}


#5

Mike,
Sorry about the 2012 typo in original post.
I upgraded firmware. Same exception.
I created a new project.
Changed the main board to spider
added an SD card
added USB DP
Auto connect
Added resources to eliminate errors, GHIElectronics.NETMF.[IO,System], System.IO.
Both with and without an SD card inserted, I get the same exception.

I SHOULD HAVE SAID REFERENCES, not resourdes


#6

as Mike said you are mixing gadgeteer and non gadgeteer code:
also never ever use this in gadgeteer…


// Sleep forever
            Thread.Sleep(Timeout.Infinite);

in Gadgeteer all you would need is:


     void ProgramStarted()
        {
            /*******************************************************************************************
            Modules added in the Program.gadgeteer designer view are used by typing 
            their name followed by a period, e.g.  button.  or  camera.
            
            Many modules generate useful events. Type +=<tab><tab> to add a handler to an event, e.g.:
                button.ButtonPressed +=<tab><tab>
            
            If you want to do something periodically, use a GT.Timer and handle its Tick event, e.g.:
                GT.Timer timer = new GT.Timer(1000); // every second (1000ms)
                timer.Tick +=<tab><tab>
                timer.Start();
            *******************************************************************************************/


            sdCard.SDCardMounted += new SDCard.SDCardMountedEventHandler(sdCard_SDCardMounted);
            sdCard.SDCardUnmounted += new SDCard.SDCardUnmountedEventHandler(sdCard_SDCardUnmounted);

 Debug.Print("Program Started");

}
        void sdCard_SDCardUnmounted(SDCard sender)
        {
            Debug.Print("unmounted");
        }

        void sdCard_SDCardMounted(SDCard sender, GT.StorageDevice SDCard)
        {
   
            Debug.Print("SD Mounted");      

            var root = sdcard.ListRootDirectoryFiles();
            foreach (var fileName in root)
            {
               Debug.Print("    " + fileName);
            }
        }

also you may want to do a search in the forum for more info.
Enjoy!.