Question about MountFileSystem() for SD Card

I have used the example codes from http://www.ghielectronics.com/community/codeshare/entry/184 for my SD Card. Everything is fine, but only one disadvantage that I must plug-out and plug-in SD card again, before I write something to SD card after loading my project

My programm ist as follow:

C# codes:



        public static void Main()
        {
                  ......................................
                  .....................................
                //Sd Card detection
                card.Insert += new InsertEventHandler(card_Insert);
                card.Eject += new EjectEventHandler(card_Eject);

                //checking SD card, when start
                if (card.Available)
                {
                    Debug.Print("Card already inserted and ready to use");
                    Debug.Print("Root directory: " + card.Volume.RootDirectory);

                }
                else
                    Debug.Print("No SD card inserted");

                  ..................................................................
                  ..................................................................

           using (HttpServer server = new HttpServer())
           {
               server.AddEvent("/", new HttpServer.EventCallback(index));
              ........................................................
              ...................................................
            }

        }


After building project and loading, I can see the SD status from “Output Window”, but before I write something to SD card, I must plug out and plug in SD card again for the first time SD card to write.

I don’t want to plug out and in at the first time SD card to write after loading my project, how can I solve the problem?

I would just check if the card is there during startup ?


        bool sdExists;
        sdExists = PersistentStorage.DetectSDCard();

        if (sdExists)
        {
            MySD = new PersistentStorage("SD");
            MySD.MountFileSystem();

        etc...


@ David@ Emrol -

thank you for your help. If I define like your said, and it works fine at the beginning. but I want to use the 2 handlers as follow and your codes together:


            card.Insert += new InsertEventHandler(card_Insert);
            card.Eject += new EjectEventHandler(card_Eject);

the 2 Handler is very useful for my project.

I have tested many times, your codes can not work with the above 2 handler, there is always the conflick about “new PersistentStorage(“SD”)”, how can I resolve the problem?

My test codes is as follow:



static PersistentStorage SDCard;
main()
{
            if (PersistentStorage.DetectSDCard())
            {
                SDCard = new PersistentStorage("SD");
                SDCard.MountFileSystem();
                Debug.Print("Card already inserted and ready to use");
            }
            else
            {
                Debug.Print("No SD card inserted");
            }

         
            //object declare for class SdCard
            SdCard card = new SdCard(); // SdCard() is from the example 184 of WouterH

            //Sd Card detection
            card.Insert += new InsertEventHandler(card_Insert);
            card.Eject += new EjectEventHandler(card_Eject);

           using (HttpServer server = new HttpServer())
           {
                     .................................................................
                      .................................................................
}
}

You need to move the scope of your object setup back into the IF block. Something like:

            if (PersistentStorage.DetectSDCard())
            {
             //object declare for class SdCard
            SdCard card = new SdCard(); // SdCard() is from the example 184 of WouterH
 
                 SDCard.MountFileSystem();
                Debug.Print("Card already inserted and ready to use");
 
          //Sd Card detection
            card.Insert += new InsertEventHandler(card_Insert);
            card.Eject += new EjectEventHandler(card_Eject);
            }
            else
            {
                Debug.Print("No SD card inserted");
            }
 
 
 

I have tried many times, but not successful… it happens always error at “SDCard.MountFileSystem();”, see picture 1 and 2.

have you looked into Woute’s code? You’ll find that i’s already doing some of this. Your task will be to understand what his class is doing and either extend it to register your events or to remove the duplicated code