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();
}
}