Main Site Documentation

Learning how to use SD card


#1

I’m trying to figure out how to use the micro-SD card with the Spider board. I’m pretty sure the problem is I’m missing a using statement and/or assembly reference but can’t figure out what I’m missing. I’ve included references to GHI.Premium.IO, Microsoft.SPOT.IO, Microsoft.SPOT.Native and mscorlib. My code is below.

Maybe a more important question is how do I figure out what using and assemblies I need for the hardware I’m using without having to resort to this forum?

using System;
using System.IO;
using System.Text;
using System.Threading;

using Microsoft.SPOT;
using Microsoft.SPOT.IO;

using GHI.Premium.IO;

namespace SDCardTest
{
    public class Program
    {
        public static void Main()
        {
            Debug.Print(" Program Started \r\n");

            PersistentStorage sCard = new PersistentStorage("SD");

            sdCard.MountSDCard();

            //  sdCard.UnmountSDCard();

            //Get the root directory
            string rootDirectory = sdCard.GetStorageDevice().RootDirectory;
            //Use Streamwriter to write a text file
            StreamWriter textFile = new StreamWriter(rootDirectory + @ "\hello.txt");
            textFile.WriteLine("Hello SD card");
            textFile.Close();

            //Use FileStream to read a text file
            FileStream fileStream = new FileStream(rootDirectory + @ "\hello.txt", FileMode.Open);
            //load the data from the stream
            byte[] data = new byte[fileStream.Length];
            fileStream.Read(data, 0, data.Length);
            fileStream.Close();
            string text = new string(Encoding.UTF8.GetChars(data));

            Debug.Print(text);
        }
    }
}

#2

what error message are you getting?


#3

@ Gene -

Nothing to help you about your problem. However you asked a GREAT question…

Maybe a more important question is how do I figure out what using and assemblies I need for the hardware.

I see so much code posted (not necessarily here) that does the same thing… Great code… Ya’ figure it will work… NO SO! … Two hours later you figured out the…

Using whatever stuff!


#4

I’m down to 2 of these errors

Error 1 The type or namespace name ‘FileStream’ could not be found (are you missing a using directive or an assembly reference?) C:\Users\magene\AppData\Local\Temporary Projects\SDCardTest\Program.cs 27 13 SDCardTest

and 1 of these errors

Error 3 The name ‘FileMode’ does not exist in the current context C:\Users\magene\AppData\Local\Temporary Projects\SDCardTest\Program.cs 28 58 SDCardTest

with this code

using System;
using System.IO;
using System.Text;
using System.Threading;

using Microsoft.SPOT;
using Microsoft.SPOT.IO;

using GHI.Premium.IO;
using GHI.Premium.System;

using GHI.Premium.Net;

namespace SDCardTest
{
    public class Program
    {
        public static void Main()
        {
            Debug.Print(" Program Started \r\n");

            PersistentStorage sdCard = new PersistentStorage("SD");

            sdCard.MountFileSystem();

            //  sdCard.UnmountSDCard();

            string rootDirectory = VolumeInfo.GetVolumes()[0].RootDirectory;
            FileStream FileHandle = new FileStream(rootDirectory +
                                          @ "\hello.txt", FileMode.Create);
            byte[] data =
               Encoding.UTF8.GetBytes("This string will go in the file!");
            // write the data and close the file
            FileHandle.Write(data, 0, data.Length);
            FileHandle.Close();

            // if we need to unmount
            sdCard.UnmountFileSystem();
        }
    }
}


#5

The key documentation you need is on this page, http://www.ghielectronics.com/support/.net-micro-framework

You want the 4.2 Microsoft Netmf reference, if you’re using a 4.2 project.

But here’s the thing. If you have created a Gadgeteer app, and dragged the SD card module onto the design surface, all that is added for you and you don’t need to know. This is a really easy way to have the correct items included for you and you can then take that learning over into your other app. To do that, view the DLLs in the Object Browser, and you can navigate through the structure to find the appropriate items you’re looking for.

Filestream is in System.IO, as is FileMode. Seems that you have the USING but do you have the Reference included?


#6

Whoo, hoo! Thanks Brett and tinyCLR, wooops I guess I mean GHI Forum.

In case some future noob looks at this thread, My code is below and I use the following assemblies

GHI.Premium.IO
GHI.Premium.System
Microsoft.SPOT.IO
Microsoft.SPOT.Native
msscorlib
System.IO

using System;
using System.IO;
using System.Text;

using Microsoft.SPOT;
using Microsoft.SPOT.IO;

using GHI.Premium.IO;

namespace SDCardTest
{
    public class Program
    {
        public static void Main()
        {
            Debug.Print(" Program Started \r\n");
   
            PersistentStorage sdCard = new PersistentStorage("SD");
            sdCard.MountFileSystem();

            VolumeInfo sdVol = new VolumeInfo("SD");
            Debug.Print("Volume Name:           " + sdVol.Name.ToString());
            Debug.Print("Volume Total Size:     " + sdVol.TotalSize.ToString());
            Debug.Print("Volume Free Space:     " + sdVol.TotalFreeSpace.ToString());
            Debug.Print("Volume File System:    " + sdVol.FileSystem.ToString());
            Debug.Print("Volume Serial Num:     " + sdVol.SerialNumber.ToString());
            Debug.Print("Volume ID:             " + sdVol.VolumeID.ToString());
            Debug.Print("Volume Label:          " + sdVol.VolumeLabel.ToString());
            Debug.Print("Volume Root Dir:       " + sdVol.RootDirectory.ToString());

            string rootDirectory = sdVol.RootDirectory;
            FileStream FileHandle = new FileStream(rootDirectory + @ "\hello.txt", FileMode.Create);
            byte[] data = Encoding.UTF8.GetBytes("This string will go in the file!");

            // write the data and close the file
            FileHandle.Write(data, 0, data.Length);
            FileHandle.Close();

            String[] files = Directory.GetFiles(@ "\SD");
            for(int j=0; j<files.Length; j++)
               Debug.Print("File number " + j.ToString() + " " + files[j]);
            
            // if we need to unmount
            sdCard.UnmountFileSystem();
         }
    }
}

#7

I still have one question. I have a 16 GB micro SDHC chip inserted in the microSD module. But my output from my code is

Volume Name: SD
Volume Total Size: 3038248960
Volume Free Space: 3038183424
Volume File System: FAT
Volume Serial Num: 0
Volume ID: 0
Volume Label: NO NAME
Volume Root Dir: \SD
File number 0 \SD\hello.txt

The GHI catalog for the SD card (not the microSD but they’re supposed to be functionally equivalent) says

Shouldn’t I have a volume total size something like 16 GB?

Thanks - Gene


#8

Depends if you have it formatted as a single volume, I guess. But also, the comment really is saying you haven’t got a 2Gb maximum size, not that the code that underpins this will report accurate volume space. As a test, can you write a really big file to it and then see if it decreases by the size of the file or if it’s maxxing out on displaying the available space?


#9

OK, I’ll give that a try tomorrow.