Main Site Documentation

SDcard for Cobra 2


#1

Im using a cobra 2 and a spider. While it was easy to fix SD for the spider.
Im at a complete stand still on trying to get the Cobras onboard SDcard reader to accept the card.
And i cant find any real good information on it either.
Not sure if it matters but im using 4.2.11.2.


#2

Sorry for the bad phrasing, I cant get the SDreader to init.
Don’t know how to explain it.

but with the spider its just

SDCard sdC = new SDcard(socket);

This wont work on the cobra i figured due to the fact its on board.


#3

Yea, found that post to. But for some reason i cant find Mainboard.SDCardMounted

am i missing a namespace. i have included the once i thought had something to do with SD.


#4

If you are using plain NETMF, then you need to include GHI.Hardware as a reference and add ‘using GHI.IO.Storage;’ to your program - that’s where SDCard is defined.


#5

Guess ill have to keep looking. :slight_smile:
The events aint there. even included them, removed and included them again still nothing.
Might just keep working on the spider instead.


#6

I can’t find the original post in the forum at the moment but I lifted this code when I first got the CobraII


namespace <Your Namespace Here >
{
    class sdDetect
    {
        private PersistentStorage sdPS;

        static bool sdEjectRequest = false;

        //private static ManualResetEvent _MediaInsertedEvent = null;


        #region Public SD Interface

        public sdDetect()
        {
            // Start auto mounting thread
            new Thread(SDMountThread).Start();
        }

        public void UnMount()
        {
            if (sdPS != null)
            {
                sdPS.UnmountFileSystem();
            }
        }

        #endregion

        #region SD Card Routines

        /// <summary>
        /// This Thread constantly polls for an SD Card insertion or Eject.
        /// </summary>
        void SDMountThread()
        {
            sdPS = null;
            const int POLL_TIME = 500; // check every 500 millisecond

            bool sdExists;
            while (true)
            {
                try // If SD card was removed while mounting, it may throw exceptions
                {
                    sdExists = PersistentStorage.DetectSDCard();
                    if (sdExists && sdEjectRequest)
                    {
                        sdExists = false;
                    }
                    else if (!sdExists && sdEjectRequest && sdPS == null)
                    {
                        sdEjectRequest = false;
                    }

                    // make sure it is fully inserted and stable
                    if (sdExists && !sdEjectRequest)
                    {
                        Thread.Sleep(250);
                        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(POLL_TIME);
            }
        }
        #endregion
    }
}



namespace <Your Namespace Here >
{
    public partial class Program
    {
        static sdDetect sdCard;
        static VolumeInfo sdCardVolume;

        // This method is run when the mainboard is powered up or reset.  
        void ProgramStarted()
        {
            // Use Debug.Print to show messages in Visual Studio's "Output" window during debugging.
            Debug.Print("Program Started");
           
            RemovableMedia.Insert += RemovableMedia_Insert;
            RemovableMedia.Eject += RemovableMedia_Eject;

            sdCard = new sdDetect();
       }

        private void RemovableMedia_Eject(object sender, MediaEventArgs e)
        {
            sdCardVolume = null;
            Debug.Print("Card Ejected2");
        }

        private void RemovableMedia_Insert(object sender, MediaEventArgs e)
        {
            sdCardVolume = e.Volume;
            Debug.Print("Card Inserted.");
        }
    }
}


Hope this helps.


#7

These events? (located in Microsoft.Spot.IO)

        // Subscribe to RemovableMedia events
         RemovableMedia.Insert += RemovableMedia_Insert;
         RemovableMedia.Eject += RemovableMedia_Eject;

4.3 has changed a few things.


#8

@ Fajdo -
Hi, it’s

Program.Mainboard.MassStorageMounted += Mainboard_MassStorageMounted;

you can get Access to the filesystem with


if (VolumeInfo.GetVolumes()[0].IsFormatted)
                {
                    string rootDirectory =
                        VolumeInfo.GetVolumes()[0].RootDirectory;
                    string[] files = Directory.GetFiles(rootDirectory);
                    string[] folders = Directory.GetDirectories(rootDirectory);

                    Debug.Print("Files available on " + rootDirectory + ":");
                    for (int i = 0; i < files.Length; i++)
                        Debug.Print(files[i]);

                    Debug.Print("Folders available on " + 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!");
                }


in Program.Started.

https://www.ghielectronics.com/community/forum/topic?id=16806