SD card problem

Hi,
I’ve recently started using FEZ domino, and it’s a wonderful card.

I’ve tried running the first sd card example from the beginners e-book, but it seems that “VolumeInfo” class doesn’t exist in any assembly or reference of NETF 4.0 SDK…

for example:

 if (VolumeInfo.GetVolumes()[0].IsFormatted)

Thanks for help

Did you try Microsoft.SPOT.IO?

I did a search in MS MF documentation for the VolumeInfo class. At the top of the help page it told me the namespace and assembly to use.

Sorry, my bad.

I’ve mixed it up with the System.IO

Thank you

Not bad… Learning…

Hi everyone

I was testing the SD card features and I can’t read the phrases that I wrote in a text File by Debug.Print. Could anyone tell me what is wrong with my code?, this is only an easy test because I’m gonna use this for other things (only if it works):

public void ReadWordeSD()
        {
            string chainelu;
            string rootDirectory = VolumeInfo.GetVolumes()[0].RootDirectory;
            FileStream FileHandle = new FileStream(rootDirectory + @ "\hello.txt", FileMode.Open, FileAccess.Read);
            byte[] data = new byte[100];
            // Read Bytes
            int read_count = FileHandle.Read(data, 0, data.Length);
            FileHandle.Close();
            Debug.Print("Taille des donn���©es lues : " + read_count.ToString());
            Debug.Print("Donn���©es extraites du fichier :");
            Debug.Print(data.ToString());
            chainelu = data.ToString();
            Debug.Print(chainelu);
            
            // This doesn't work cause it prints the number en ASCII code so, I think i'm missing something to convert the byte in String 
            /*for (int i = 0; i < data.Length; i++)
            {
                Debug.Print(data[i].ToString());
            }*/
            
        }

You say you can’t read… What exactly is happening?

Are you sure the hello.txt contains something?

Are you getting any output from the program?

What happens when you single step through the program?

Debug.Print(data.ToString()); is not going to give you what you expect. Need to change to a string or cast each byte to a char.

Try this…

for (int i = 0; i < read_count; i++)
{
       Debug.Print((char)data[i]);
}

hello.txt has something because I can read it in my computer.

That’s what I saw in the execution of the program

Taille des donn���©es lues : 91
Donn���©es extraites du fichier :
System.Byte[]
System.Byte[]

the last two lines are the anwser of Debug.Print

I tried another thing like your suggestion because there is a mistake in that, you can’t cast a char like that in a method that is waiting for a String


 for (int i = 0; i < read_count; i++)
            {
                Debug.Print(((char)data[i]).ToString());
            }

But now i’m sure that I received the correct data so thanks a lot

There is an example of reading text from a text file in either the GHI SDK docs or the free e-book. It was the first program I used to start testing reading from the SD. I would suggest starting with that example to see how it should work.

You could have some data corruption on the media but windows managed to handle the corruption.
Do this exactly and let me know if you have problems

  1. format your media on the PC…on the PC no FEZ

  2. create a a text file on the PC…on PC not on FEZ

  3. save and close the file and eject the media properly on your PC

  4. put the media back in the PC and make sure the file is still there

  5. Now try to read the file on FEZ. Does it work?

His problem was he printing out what he read incorrectly…

[quote]But now i’m sure that I received the correct data so thanks a lot
[/quote]

Yeah, my problem was that I couldn’t see what was happening after the read method So I wanted to be sure of the data I received.

I can’t resolve the “System.Byte[]” answer, I hope that other people can print the text, because I can’t .What I have to do is to cast into char an then into String, it’s a little rare.

What i did: I took the example of the ebook, after that I create an object called SDMemory where I create all the differents methods to read, write, etc from a SDCard

Then I created a class with the main program to create my object and then to use my methods and I can open the card, i can verify if it was inserted. I can read the folders and I can create and write some files. But I can’t read phrases from a file directly, I saw that the \n variable can’t be read by Debug, i don’t no why.

Then I try all the other things I told

I need to write and read to many files in the SD Memory that’s why I’m trying this test


class Program
    {
        public static void Main()
        {
            //PersistentStorage sdPS = new PersistentStorage("SD");
            SDMemory sdM = new SDMemory();

            // create a thread handler
            Thread MyThreadHandler;
            // create a new thread object
            // and assing to my handler
            MyThreadHandler = new Thread(MyThread);


            try
            {
                // start my new thread
                MyThreadHandler.Start();
                sdM.MountFileSys();
                //sdM.EcritureSDTest();
                sdM.LectureSD();
                sdM.LectureChaineSD();

                sdM.UnmountFileSys();
            }
            catch
            {
                Debug.Print("###Exception");
            }
        }

        public static void MyThread()
        {
            OutputPort LED;
            LED = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.LED, true);

            while (true)
            {

                LED.Write(!LED.Read());
                // sleep this thread for 1 seond
                Thread.Sleep(500);
            }
        }

    }

And this is my object SDMemory


class SDMemory
    {

        PersistentStorage sdPS;

        public SDMemory()
        {
            // On suppose que la carte SD est ins�©r�©e
            // Cr�©e un p�©riph�©rique de stockage
            try
            {
                this.sdPS = new PersistentStorage("SD");
            }
            catch
            {
                throw new Exception("SD card not detected");
            }
        }


        // Cette m�©thode permet de d�©tecter lorsqu'une m�©moire SD a �©t�© ins�©rt�©
        public static bool DetecterSD()
        {

            Debug.Print("D�©tection d'une carte SD");
            if (GHIElectronics.NETMF.IO.PersistentStorage.DetectSDCard())
            {
                Debug.Print("Une carte a �©t�© inser�©");
                return true;
            }
            else
            {
                Debug.Print("Pas de carte ins�©r�©");
                return false;
            }
        }



        public void MountFileSys()
        {
            this.sdPS.MountFileSystem();
        }


        public void UnmountFileSys()
        {

            // D�©monte le syst�¨me de fichiers
            this.sdPS.UnmountFileSystem();
        }



        public void DeleteFile(String filename)
        {
            /*
             *  Pour supprimer un fichier c'est pas si simple, il faut utiliser les m�©those VolumeInfo.Flush() ou 
             *  VolumeInfo.FlushAll()
             */
        }


        // Consid�¨re qu'un p�©riph�©rique est bien disponible et y acc�¨de via le Micro Framework
        // pour afficher la liste des fichiers et r�©pertoires
        public void LectureSD()
        {

            Debug.Print("Lecture des fichiers et r�©pertoires:");
            if (VolumeInfo.GetVolumes()[0].IsFormatted)
            {
                string rootDirectory = VolumeInfo.GetVolumes()[0].RootDirectory;
                string[] files = Directory.GetFiles(rootDirectory);
                string[] folders = Directory.GetDirectories(rootDirectory);
                Debug.Print("Fichiers sur " + rootDirectory + ":");
                for (int i = 0; i < files.Length; i++)
                    Debug.Print(files[i]);
                Debug.Print("R�©pertoires sur " + rootDirectory + ":");
                for (int i = 0; i < folders.Length; i++)
                    Debug.Print(folders[i]);
            }
            else
            {
                Debug.Print("La carte n'est pas format�©e. Formatez-l�  sur un PC en FAT32/FAT16.");
            }
        }

        //Pour v�©rifier avec un PC et un lecteur de cartes que un0 fichier est pr�©sent.
        //on va ouvrir un fichier et lire une chaine �©crite pr�©c�©demment.
        public void LectureChaineSD()
        {
            string chainelu;
            char[] phrase = new char[100];

            string rootDirectory = VolumeInfo.GetVolumes()[0].RootDirectory;
            FileStream FileHandle = new FileStream(rootDirectory + @ "\hello.txt", FileMode.Open, FileAccess.Read);
            byte[] data = new byte[100];
            // Lit les donn�©es et ferme le fichier
            int read_count = FileHandle.Read(data, 0, data.Length);
            //FileHandle.Close();
            Debug.Print("Taille des donn�©es lues : " + read_count.ToString());
            Debug.Print("Donn�©es extraites du fichier :");
            Debug.Print(data.ToString());
            chainelu = data.ToString();
            Debug.Print(chainelu);
            for (int i = 0; i < data.Length; i++)
            {
                phrase[i] = (char)data[i];
                Debug.Print(phrase[i].ToString());
            }

            for (int i = 0; i < read_count; i++)
            {
                Debug.Print(((char)data[i]).ToString());
            }

                FileHandle.Close();
        }

       

        // On  ouvrira un fichier pour �©crire une chaine �  l'int�©rieur
        public void EcritureSDTest()
        {

            // Consid�¨re qu'un p�©riph�©rique de stockage est disponible et y acc�¨de �  travers NETMF
            string rootDirectory = VolumeInfo.GetVolumes()[0].RootDirectory;
            FileStream FileHandle = new FileStream(rootDirectory + @ "\hello.txt", FileMode.OpenOrCreate);
            byte[] data = Encoding.UTF8.GetBytes("Cette chaine ira dans le fichier !\n");
            // Ecrit les donn�©es et ferme le fichier
            FileHandle.Write(data, 0, data.Length);
            data = Encoding.UTF8.GetBytes("Cette chaine 2 ira dans le fichier par deuxieme fois!");
            // Ecrit les donn�©es et ferme le fichier
            FileHandle.Write(data, 0, data.Length);
            FileHandle.Close();

        }
    }