Example code for SD in 4.3

Can anyone provide me an example code how to work with SD cards using 4.3? I am having problems with GHI.IO.Storage.SD.

Your wish is my command :slight_smile:

using System;
using System.IO;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.IO;
using GHI.IO.Storage;

class Program
    public static void Main()
        // ...
        // SD Card is inserted
        // Create a new storage device

        SD sdPS = new SD();

        // Mount the file system

        // Assume one storage device is available, access it through 
        // NETMF and display the available files and folders:
        Debug.Print("Getting files and folders:");
        if (VolumeInfo.GetVolumes()[0].IsFormatted)
            string 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("Folders available on " + rootDirectory + ":");
            for (int i = 0; i < folders.Length; i++)
            Debug.Print("Storage is not formatted. " +
                "Format on PC with FAT32/FAT16 first!");
        // Unmount when done

Thank you.
I am using SD card that was used for testing with 4.2. It was working OK there. I have few files stored on it.
When I try your code, I get [em]System.IndexOutOfRangeException[/em] in this line:

That's because VolumeInfo.GetVolumes().Length = 0.

Any ideas what is going on?

P.S. [em]Your wish is my command :)[/em]
If that were the case more often... Uhh, I would be very happy :think: .

One more thing. Let’s say I want to use two SD cards. Will these two lines of code take care of mounting them both?

SD sdPS = new SD();

And how should I use IsSDCardPresent? How to specify which card to check?

We do not support multiple cards.

Add thread sleep 5000 before getting the volumes, will it work?

I am using a phone so I can’t test anything now.

No effect.

call GetVolumes()
wait a while
call GetValuems again.
This is needed for USB devices for example

Same thing :frowning:

Update: I have purchased a new card and it seems to work now. Has anyone at GHI done any testing why some SD cards are being rejected?

This try-and-see-which-works method is not particularly frustration-free.

Generally that’s true, but so far all the Cards I used were working.
There is even a Speed comparission table in the Forums, you could use a a “which one to buy” guide.
But in fact any list of Cards would be out of date very soon nowadays.

1 Like

We have several cards that we test all the time and we continue to get new cards and test regularly. We are also open to testing any cards anyone would provide to us.

@ Gus - that’s very nice! I will send you few of mine.

@ Reinhard Ostermeier - thanks for the link. I wasn’t aware of that SD card speed table.

One more SD card related question. Why can I set P1.8 (on Cobra II) as InputPort, but cannot set it as InterruptPort (I get System.ArgumentException). InterruptPort would be very handy.

@ iamin - only pins P0_x and P2_x support interrupt. See g120 brochure.

@ GHI this should be mentioned in the developer guide also.

1 Like

Hi guys,

According to this document, the ‘SD’ class (referenced by Gus) is now ‘SDCard’.


(look under GHI.IO.Storage.Namespace)

Must have been an earlier version of 4.3 where the class name was SD?