Cerberus and sdcard

Hi all,

I’m a new programmer on netmf but not new on .net. I want t thanks GHI innovation. The gadgeteer’s and ghi mainboard with their modules are a good issue for my developpement time.
on a project, I want to log informations on µSD. And this doesn’t work. I searched since days for a solution, in the forum and codeshare but no one samples work.
My sdcard is a kingston 4GB SDHC. Format in FAT32 / 4k per blocks.

The code :

using System;
using System.IO;

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

using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using Gadgeteer.Modules.GHIElectronics;


namespace GadgeteerApp1
{
    public partial class Program
    {
        private static string szTextContent = "If I told you, I'd have to kill you...\r\n";

        void ProgramStarted()
        {
            DebugPrint("Program Started");

            sdCard.DebugPrintEnabled = true;
            sdCard.SDCardMounted += new GTM.GHIElectronics.SDCard.SDCardMountedEventHandler(sdCard_SDCardMounted);
            sdCard.SDCardUnmounted += new GTM.GHIElectronics.SDCard.SDCardUnmountedEventHandler(sdCard_SDCardUnmounted);

            Microsoft.SPOT.IO.RemovableMedia.Eject += new Microsoft.SPOT.IO.EjectEventHandler(RemovableMedia_Eject);
            Microsoft.SPOT.IO.RemovableMedia.Insert += new Microsoft.SPOT.IO.InsertEventHandler(RemovableMedia_Insert);
        }

        void RemovableMedia_Insert(object sender, Microsoft.SPOT.IO.MediaEventArgs e)
        {
            DebugPrint("Insert - Mounted : " + e.Volume.Name);
        }

        void RemovableMedia_Eject(object sender, Microsoft.SPOT.IO.MediaEventArgs e)
        {
            DebugPrint("Eject - UnMounted : " + e.Volume.Name);
        }

        void sdCard_SDCardUnmounted(GTM.GHIElectronics.SDCard sender)
        {
            DebugPrint("SDCardUnmounted - Mounted : " + sender.IsCardMounted + ", Inserted : " + sender.IsCardInserted);
        }

        void sdCard_SDCardMounted(GTM.GHIElectronics.SDCard sender, GT.StorageDevice SDCard)
        {
            DebugPrint("SDCardMounted - Mounted : " + sender.IsCardMounted + ", Inserted : " + sender.IsCardInserted);
            ShowDevInfo(SDCard);
            Boolean Tests = RunBasicTests(SDCard.Volume);
            DebugPrint("Basic storage tests " + ((Tests) ? "passed" : "failed"));
        }

        void ShowDevInfo(GT.StorageDevice StrgDev)
        {
            VolumeInfo VolInfo = StrgDev.Volume;
            string DevInfo = VolInfo.Name + " format details:\r\n" +
                            "Device Flags    = " + VolInfo.DeviceFlags + "\r\n" +
                            "FileSystem      = " + VolInfo.FileSystem + "\r\n" +
                            "FileSystemFlags = " + VolInfo.FileSystemFlags + "\r\n" +
                            "SerialNumber    = " + VolInfo.SerialNumber + "\r\n" +
                            "TotalFreeSpace  = " + VolInfo.TotalFreeSpace + "\r\n" +
                            "TotalSize       = " + VolInfo.TotalSize + "\r\n" +
                            "VolumeID        = " + VolInfo.VolumeID + "\r\n" +
                            "VolumeLabel     = " + VolInfo.VolumeLabel + "\r\n\r\n";

            string DirInfo = "Dir of " + VolInfo.Name + "\r\n" +
                              StrgDev.RootDirectory + "\r\n";

            string[] RootFiles = StrgDev.ListRootDirectoryFiles();
            string Files = string.Empty;
            foreach (string File in RootFiles)
            {
                FileInfo FileNfo = new FileInfo(StrgDev.RootDirectory + "\\" + File);
                Files += (new string(' ', StrgDev.RootDirectory.Length) + File + " size: " + FileNfo.Length + " bytes\r\n");
            }

            DebugPrint(DevInfo + DirInfo + Files);
        }

        private Boolean RunBasicTests(VolumeInfo VolInfo)
        {
            if (!CreateDir(VolInfo.RootDirectory + "\\TestFolder"))
            {
                return false;
            }
            DebugPrint("CreateDir test passed");
            if (!WriteTextFile(VolInfo.RootDirectory + "\\TestFolder\\TestFile.txt"))
            {
                return false;
            }
            DebugPrint("WriteTextFile test passed");
            ShowTestResults(VolInfo);
            if (!DeleteDir(VolInfo.RootDirectory + "\\TestFolder"))
            {
                return false;
            }
            DebugPrint("DeleteDir test passed");
            VolInfo.FlushAll();
            return true;
        }

        private Boolean CreateDir(string szFolderName)
        {
            DirectoryInfo Folder = new DirectoryInfo(szFolderName);
            try
            {
                Folder.Create();
            }
            catch (Exception err)
            {
                DebugPrint("Failed to create " + szFolderName + "; " + err.Message);
                return false;
            }
            DebugPrint("Created " + szFolderName);
            return Folder.Exists;
        }

        private Boolean WriteTextFile(string szFileName)
        {
            StreamWriter Stream = GetFileStream(szFileName);
            if (null == Stream)
            {
                return false;
            }
            try
            {
                Stream.Write(szTextContent);
            }
            catch (Exception err)
            {
                Stream.Close();
                DebugPrint("Failed to write " + szTextContent + " to " + szFileName + "; " + err.Message);
                return false;
            }
            Stream.Close();
            DebugPrint("Wrote " + szTextContent + " to " + szFileName);
            return true;
        }

        private Boolean DeleteDir(string szFolderName)
        {
            DirectoryInfo Folder = new DirectoryInfo(szFolderName);
            try
            {
                Folder.Delete(true);
            }
            catch (Exception err)
            {
                DebugPrint("Failed to delete " + szFolderName + "; " + err.Message);
                return false;
            }
            DebugPrint("Deleted " + szFolderName);
            return !Folder.Exists;
        }

        private StreamWriter GetFileStream(string szFileName)
        {
            StreamWriter Stream = null;
            try
            {
                Stream = new StreamWriter(szFileName);
            }
            catch (Exception err)
            {
                DebugPrint("Failed to access " + szFileName + "; " + err.Message);
            }
            return Stream;
        }

        void ShowTestResults(VolumeInfo VolInfo)
        {
            DirectoryInfo DirInfo = new DirectoryInfo(VolInfo.RootDirectory + "\\TestFolder");
            FileInfo[] Files = DirInfo.GetFiles();
            string FileData = string.Empty;
            foreach (FileInfo File in Files)
            {
                FileData += (new string(' ', VolInfo.RootDirectory.Length) + File.Name + " size: " + File.Length + " bytes\r\n");
            }
            DebugPrint("** Contents of " + VolInfo.RootDirectory + "\\" + DirInfo.Name + "\r\n" + FileData);
        }

        void DebugPrint(string Msg)
        {
            Debug.Print("[" + DateTime.Now.ToString("hh:mm:ss.fff") + "] " + Msg);
        }

    }
}


and result in debug output

Create TS.

 Loading start at 8059bec, end 8082120

Assembly: mscorlib (4.2.0.0)Assembly: Microsoft.SPOT.Native (4.2.0.0)Assembly: Microsoft.SPOT.Hardware (4.2.0.0)
Assembly: Microsoft.SPOT.Graphics (4.2.0.0)Assembly: Microsoft.SPOT.TinyCore (4.2.0.0)
Assembly: Microsoft.SPOT.Hardware.SerialPort (4.2.0.0)Assembly: Microsoft.SPOT.IO (4.2.0.0)
Assembly: System.IO (4.2.0.0)Assembly: Microsoft.SPOT.Hardware.OneWire (4.2.0.0)Assembly: Microsoft.SPOT.Hardware.Usb (4.2.0.0)
Assembly: Microsoft.SPOT.Hardware.PWM (4.2.0.1)Assembly: Microsoft.SPOT.Net (4.2.0.0)
Assembly: System (4.2.0.0)Loading Deployment Assemblies.

Attaching deployed file.

Assembly: GHI.OSHW.Hardware (4.2.2.0)Attaching deployed file.

Assembly: Gadgeteer (2.42.0.0)Attaching deployed file.

Assembly: System.Http (4.2.0.0)Attaching deployed file.

Assembly: GTM.GHIElectronics.SDCard (1.0.0.0)Attaching deployed file.

Assembly: Microsoft.SPOT.Net.Security (4.2.0.0)Attaching deployed file.

Assembly: GHIElectronics.Gadgeteer.FEZCerberus (1.0.5.0)Attaching deployed file.

Assembly: Microsoft.SPOT.Touch (4.2.0.0)Attaching deployed file.

Assembly: GadgeteerApp1 (1.0.0.0)Attaching deployed file.

Assembly: System.Net.Security (4.2.0.0)Resolving.

The debugging target runtime is loading the application assemblies and starting execution.
Ready.

'Microsoft.SPOT.Debugger.CorDebug.dll' (Managé) : 'C:\Program Files (x86)\Microsoft .NET Micro Framework\v4.2\Assemblies\le\mscorlib.dll' chargé, symboles chargés.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managé) : 'C:\Program Files (x86)\Microsoft .NET Micro Framework\v4.2\Assemblies\le\Microsoft.SPOT.Native.dll' chargé, symboles chargés.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managé) : 'C:\Program Files (x86)\Microsoft .NET Micro Framework\v4.2\Assemblies\le\Microsoft.SPOT.Hardware.dll' chargé, symboles chargés.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managé) : 'C:\Program Files (x86)\Microsoft .NET Micro Framework\v4.2\Assemblies\le\Microsoft.SPOT.Graphics.dll' chargé, symboles chargés.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managé) : 'C:\Program Files (x86)\Microsoft .NET Micro Framework\v4.2\Assemblies\le\Microsoft.SPOT.TinyCore.dll' chargé, symboles chargés.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managé) : 'C:\Program Files (x86)\Microsoft .NET Micro Framework\v4.2\Assemblies\le\Microsoft.SPOT.Hardware.SerialPort.dll' chargé, symboles chargés.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managé) : 'C:\Program Files (x86)\Microsoft .NET Micro Framework\v4.2\Assemblies\le\Microsoft.SPOT.IO.dll' chargé, symboles chargés.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managé) : 'C:\Program Files (x86)\Microsoft .NET Micro Framework\v4.2\Assemblies\le\System.IO.dll' chargé, symboles chargés.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managé) : 'C:\Program Files (x86)\Microsoft .NET Micro Framework\v4.2\Assemblies\le\Microsoft.SPOT.Hardware.OneWire.dll' chargé, symboles chargés.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managé) : 'C:\Program Files (x86)\Microsoft .NET Micro Framework\v4.2\Assemblies\le\Microsoft.SPOT.Hardware.Usb.dll' chargé, symboles chargés.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managé) : 'C:\Program Files (x86)\Microsoft .NET Micro Framework\v4.2\Assemblies\le\Microsoft.SPOT.Hardware.PWM.dll' chargé, symboles chargés.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managé) : 'C:\Program Files (x86)\Microsoft .NET Micro Framework\v4.2\Assemblies\le\Microsoft.SPOT.Net.dll' chargé, symboles chargés.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managé) : 'C:\Program Files (x86)\Microsoft .NET Micro Framework\v4.2\Assemblies\le\System.dll' chargé, symboles chargés.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managé) : 'C:\Program Files (x86)\GHI Electronics\GHI OSHW NETMF v4.2 SDK\Assemblies\le\GHI.OSHW.Hardware.dll' chargé
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managé) : 'C:\Program Files (x86)\Microsoft .NET Gadgeteer\Core\Assemblies\.NET Micro Framework 4.2\le\Gadgeteer.dll' chargé, symboles chargés.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managé) : 'C:\Program Files (x86)\Microsoft .NET Micro Framework\v4.2\Assemblies\le\Microsoft.SPOT.Net.Security.dll' chargé, symboles chargés.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managé) : 'C:\Program Files (x86)\Microsoft .NET Micro Framework\v4.2\Assemblies\le\System.Net.Security.dll' chargé, symboles chargés.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managé) : 'C:\Program Files (x86)\Microsoft .NET Micro Framework\v4.2\Assemblies\le\System.Http.dll' chargé, symboles chargés.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managé) : 'C:\Program Files (x86)\GHI Electronics\GHI .NET Gadgeteer SDK\Modules\SDCard\NETMF 4.2\le\GTM.GHIElectronics.SDCard.dll' chargé, symboles chargés.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managé) : 'C:\Program Files (x86)\GHI Electronics\GHI .NET Gadgeteer SDK\Mainboards\FEZCerberus\NETMF 4.2\le\GHIElectronics.Gadgeteer.FEZCerberus.dll' chargé, symboles chargés.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managé) : 'C:\Program Files (x86)\Microsoft .NET Micro Framework\v4.2\Assemblies\le\Microsoft.SPOT.Touch.dll' chargé, symboles chargés.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managé) : 'C:\DocumentAllUsers\Programmation\Poubelle\GadgeteerApp1\GadgeteerApp1\bin\Debug\le\GadgeteerApp1.exe' chargé, symboles chargés.
Le thread '<Sans nom>' (0x2) s'est arrêté avec le code 0 (0x0).
Using mainboard GHI Electronics FEZCerberus version 1.1
SDCard ERROR : Unable to mount SD card. Is card formatted as FAT32?
[12:00:29.974] Program Started
[12:00:30.165] Eject - UnMounted : SD
[12:00:30.489] SDCardUnmounted - Mounted : True, Inserted : True
[12:00:31.170] Insert - Mounted : SD
[12:00:32.174] SDCardMounted - Mounted : True, Inserted : True
Le thread '<Sans nom>' (0x3) s'est arrêté avec le code 0 (0x0).
[12:00:41.501] SD format details:
Device Flags    = 0
FileSystem      = FAT
FileSystemFlags = 0
SerialNumber    = 0
TotalFreeSpace  = 3892305920
TotalSize       = 3892314112
VolumeID        = 0
VolumeLabel     = NO NAME    

Dir of SD
\SD

Une exception de première chance de type 'System.IO.IOException' s'est produite dans Microsoft.SPOT.IO.dll
[12:00:44.913] Failed to create \SD\TestFolder; Exception was thrown: System.IO.IOException
[12:00:45.603] Basic storage tests failed

Please help :frowning:

UP !!
Thanks

Did you ever get this working? I was having a different error and tried your code to see if my SD card was okay and it works fine for me.

Output:


Using mainboard GHI Electronics FEZCerberus version 1.1
[12:10:24.597] Program Started
[12:10:26.480] SDCardMounted - Mounted : True, Inserted : True
[12:10:26.510] SD format details:
Device Flags    = 0
FileSystem      = FAT
FileSystemFlags = 0
SerialNumber    = 0
TotalFreeSpace  = 4006379520
TotalSize       = 4006608896
VolumeID        = 0
VolumeLabel     = NO NAME    

Dir of SD
\SD
   hello.txt size: 0 bytes
   _time.txt size: 0 bytes
   status.txt size: 21 bytes

[12:10:26.740] Created \SD\TestFolder
[12:10:26.745] CreateDir test passed
[12:10:26.794] Wrote If I told you, I'd have to kill you...
 to \SD\TestFolder\TestFile.txt
[12:10:26.797] WriteTextFile test passed
[12:10:26.817] ** Contents of \SD\TestFolder
   TestFile.txt size: 40 bytes

[12:10:26.828] Deleted \SD\TestFolder
[12:10:26.833] DeleteDir test passed
[12:10:26.905] Basic storage tests passed
The thread '<No Name>' (0x3) has exited with code 0 (0x0).