Need help with SD.Mount()

I hope that someone can help with this, it’s got me a little stuck.

I’m getting a System.IO exception that I can’t catch when trying to mount the SD card after having previously unmounted it. The RemovableMedia.Insert event fires as expected but then I get the exception when trying to access the file system and I can’t catch it.

This is what I’m using;

public SDcard()
        {
            try { sd_card = new SDCard(); }
            catch { Debug.Print("No SD Card"); }
        }

        public static void MountSDcard()
        {
            try 
            {
                RemovableMedia.Insert += new InsertEventHandler(SDMountComplete);
                SDmounted = false;
                sd_card.Mount();
                //while (!SDmounted)
                //{
                //    System.Threading.Thread.Sleep(50);
                //}
            }
            catch (Exception e) { Debug.Print(e.ToString()); }
        }

        private static void SDMountComplete(object sender, MediaEventArgs e)
        {
            SDmounted = true;
            RemovableMedia.Insert -= new InsertEventHandler(SDMountComplete);
        }

        public static void UnmountSDcard()
        {
            try { sd_card.Unmount(); SDmounted = false; }
            catch (Exception e) { Debug.Print(e.ToString()); }
        }

Firstly I mount the SD card, then read a text file with this;

public string FileReadToEnd(string readToEndFile)
        {
            string textToEndString;
            //rootDirectory = VolumeInfo.GetVolumes()[0].RootDirectory;
            textToEndStream = new FileStream(rootDirectory + @ "\" + readToEndFile, FileMode.Open, FileAccess.Read);
            if (textToEndStream.CanRead)
            {
                TextReader textToEnd = new StreamReader(textToEndStream);
                textToEndString = textToEnd.ReadToEnd();
                textToEnd.Close();
                textToEnd.Dispose();
                return textToEndString;
            }
            else
            {
                throw new Exception("Cannot read from file:" + readToEndFile);
            }
        }

Unmount the SD card, then if I get a list of files on the card (inside a try/catch) after mounting the second time;

 GetSDfiles()
        {
            rootDirectory = VolumeInfo.GetVolumes()[0].RootDirectory;
            if (VolumeInfo.GetVolumes()[0].IsFormatted)
            {
                return Directory.GetFiles(rootDirectory);
            }
            else
            {
                Debug.Print("SD card is not formatted. Format on PC with FAT32/FAT16 first.");
                return new string[] { };
            }
        }

I get an IO exception that is not caught and crashes the board (G120 on CobraIII).

Is it something to do with the filestream from the first file system access? I have to unmount the card because otherwise it is unreadable on a PC.

I noticed that Mount() also takes the card’s clock speed, SDCard() that takes an interface and also a method called ForceInitialization() but I am not sure whether these will help or not, or where to find the right values to pass to them?

Thanks

You may want to try.

https://www.ghielectronics.com/docs/51/accessing-folders-and-files

See: Warning

To guarantee that the file buffers and the meta-data are written to the media you need to flush the volume:

•VolumeInfo.GetVolumes()[0].FlushAll(), or
•new Microsoft.SPOT.IO.VolumeInfo(“\SD”).FlushAll().

Just a thought…

Thanks. I actually have no problem with loosing written files or anything like that.

I just tried that, and closing the file stream before unmounting but I still get an exception that I cannot catch after the second mount.

[quote]#### Exception System.IO.IOException - CLR_E_FILE_IO (4) ####
#### Message:
#### Microsoft.SPOT.IO.VolumeInfo::.ctor [IP: 0000] ####
#### Microsoft.SPOT.IO.RemovableMedia::MessageHandler [IP: 0022] ####
A first chance exception of type ‘System.IO.IOException’ occurred in Microsoft.SPOT.IO.dll
An unhandled exception of type ‘System.IO.IOException’ occurred in Microsoft.SPOT.IO.dll[/quote]

If GHI guys are able to test this next week I’d be really grateful.

If you think this is a GHI issue, you’re going to need a good stand-alone repro for this and tell them what platform you’re using.

1 Like

@ Brett - yes exactly

1 Like

At this point I’m not certain where the issue lies and generally consider that my own mistakes are the most likely cause! With that in mind I think I’ve been able to narrow it down to two particular issues that I haven’t been able to solve.

The first is that using SDCard.Mount() when a card is already mounted, or SDCard.Unmount() when the card is not mounted will throw the following exception that I either cannot catch or can be caught yet still crashes the application.

[quote]#### Exception System.InvalidOperationException - 0x00000000 (3) ####

Message: The device is already unmounted.

GHI.IO.Storage.SDCard::Unmount [IP: 0016] ####[/quote]

So I can avoid that exception by always checking if the card is mounted first, except in the other issue…This is that I cannot use SDCard.Unmount() after getting a list of all the files on the card, as in the example posted above, without throwing the following exception that I either cannot catch or can be caught yet still crashes the application.

[quote]#### Exception System.IO.IOException - CLR_E_FILE_IO (4) ####

Message:

Microsoft.SPOT.IO.VolumeInfo::.ctor [IP: 0000]

Microsoft.SPOT.IO.RemovableMedia::MessageHandler [IP: 0022] ####[/quote]

The reason that I don’t know if I’m catching these exceptions is because I’m not seeing the debug messages that are in the catch. I’m assuming at this point that this is because the board is freezing/crashing before the messages can be displayed.

It’s on my list to create some standalone code for this but the priority has so far been to get an application that is running in the field updated and stable. So in the mean time I’m interested to know if anyone else can reproduce any of that on G120 (I’m testing with a Cobra III, NETMF 4.3.8.1, VS2013). Considering there are exceptions that are causing a freeze (I have catches everywhere!) I thought GHI or someone here might be of come across this before.

Thanks

it’s unlikely anybody is going to write code to try to reproduce something for you, because, well, largely its because we agree with you and suspect your code. So if you can provide a stand alone repro code, just how you use it in your app, then people are more likely to go and try it for you.