Exception on creating instance of PersistentStorage Class

I found that I also needed to a delay between mounting and trying to access the card. It was mentioned earlier that ‘there is a way in the library to detect if the card is present’, any idea on where to look for such>

It is GHIElectronics.NETMF.IO.DetectSDCard()

Thanks! I found a post about this subject on the GHI form that talked about testing to see if the Mode line was pulled HI by the SD card and thought maybe you guys had created an internal function for that.

One small correction though, it is:

GHIElectronics.NETMF.IO.PersistentStorage.DetectSDCard()

I threw this in a program I am using to test reading an XML file from a SD card, and it works great. This is likely not the best way to do things, just setting in a loop looking for a card insert but is does demonstrate that the guys at GHI have made it ‘Freaking Easy’ for us to insure a card is in place before trying to mount it.

           
// Check to see if SD Card is inserted
while (!PersistentStorage.DetectSDCard())
{
      Debug.Print("Please Insert SD card!");
       Thread.Sleep(1000);
}

Glad you find it “freakin’ Easy!” to use :smiley:

On the EMX platform, specifically the EMX development board, what is the difference between PersistentStorage.DetectSDCard and the IO36 pin as referenced in the online wiki?

// Set the SD detect pin
sdDetectPin = new InputPort(EMX.Pin.IO36, false, Port.ResistorMode.PullUp);

Referenced from here:

Does the PersistentStorage.DetectSDCard refer to the IO36 DIO?

I think detect SD method will be dropped.

Gus, is the DetectSDCard() a “soft” detection of the card versus the real IO method?

Do you have a “recommended” SDCARD that “in your experience” works without any errors? We are seeing strange, random, I/O Exception errors.

  • Sometimes PersistentStorage(“SD”) throws an exception. Put the card in a PC and then back in the EMX and it works.
  • Sometimes the card will work for I/O access and then begin failing to read and write when two streams are writing and one stream is reading. (ie binary logging write stream, ascii status logging write stream, ftp upload of a binary file read stream)

Is there a limit to how many streams can be opened to the SD card?

Thanks.

correct

There should be no limits and we do not recommend any specific brand. We have seen SD issues related to power. Adding a 22uF capacitor on power pin fixed the problem. Still, you need to test the card you are planning on using.

I’m not so sure that adding a main thread delay (sleep) in the code is the best approach to doing this. If you’re eventual result will be a large multi-threaded app that just slows down the process.

I would suggest going to a more event driven model where you initialize the SD card, then monitor its state via variables and populate pocos with data once the event fires. This will keep the flow of the app moving along and allow your app to move along initializing other hardware and spawning additional threads, etc.

This is kinda psuedo-code, but you get the drift. I would actually move these into a Singleton wrapper eventually.



        public static PersistentStorage PS;
        public static VolumeInfo Sd;

public static void Main()
{
            RemovableMedia.Insert += RemovableMediaInsert;
            RemovableMedia.Eject += RemovableMediaEject;
            new Thread(SdMountThread).Start();

           //Continue with rest of initializing and enter run state
}

// This thread monitors SD and keeps it clean
public static void SdMountThread()
        {
            PersistentStorage sdPS = null;
            const int pollTime = 500; // check every 500 millisecond

            while (true)
            {
                try // If SD card was removed while mounting, it may throw exceptions
                {
                    var sdExists = PersistentStorage.DetectSDCard();
 
                    // make sure it is fully inserted and stable
                    if (sdExists)
                    {
                        Thread.Sleep(50);
                        sdExists = PersistentStorage.DetectSDCard();
                    }
 
                    if (sdExists && sdPS == null)
                    {
                        sdPS = new PersistentStorage("SD");
                        sdPS.MountFileSystem();
                    }
                    else if (!sdExists && sdPS != null)
                    {
                        sdPS.UnmountFileSystem();
                        sdPS.Dispose();
                        sdPS = null;
                    }
                }
                catch
                {
                    if (sdPS != null)
                    {
                        sdPS.Dispose();
                        sdPS = null;
                    }
                }
 
                Thread.Sleep(pollTime);
            }
        }

private static void RemovableMediaEject(object sender, MediaEventArgs e)
        {
            Debug.Print("Storage \"" + e.Volume.RootDirectory + "\" is ejected.");
            //Clean up any vars holding files/structures/mount state, etc.

        }
        private static void RemovableMediaInsert(object sender, MediaEventArgs e)
        {
            //Set Mount state to positive
            Debug.Print("Storage \"" + e.Volume.RootDirectory + "\" is inserted.");
            Debug.Print("Getting files and folders:");
            if (e.Volume.IsFormatted)
            {
                 //set mount type to formatted, read in all objects and populate some array or List<>
                Sd = e.Volume;
                var files = Directory.GetFiles(e.Volume.RootDirectory);
                Debug.Print("Files available on " + e.Volume.RootDirectory + ":");

                for (var i = 0; i < files.Length; i++)
                    Debug.Print(files[i]);
            }
            else
            {
                //set mount type to unformated
                Debug.Print("Storage is not formatted. Format on PC with FAT32/FAT16 first.");
            }


btw, that method of threading out the SD detection and instantiation has worked well for me for months in a FEZ Cobra with LOTS of different types of SD cards. Once I moved the instatiation of that out to a thread I never had any issues with SD card detection and mounting – it was a really stable methodology.