Main Site Documentation

Example code for SD in 4.3


#1

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.


#2

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
        sdPS.Mount();


        // 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 =
                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!");
        }
        // Unmount when done
        sdPS.Unmount();
    }
}

#3

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: .

#4

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();
sdPS.Mount();

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


#5

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.


#6

No effect.


#7

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


#8

Same thing :frowning:


#9

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.


#10

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.


#11

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.


#12

@ 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.


#13

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.


#14

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

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


#15

Hi guys,

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

https://www.ghielectronics.com/downloads/man/Library_Documentation_v4.3/

(look under GHI.IO.Storage.Namespace)

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

LarryScott