Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

MicroSD Card File Lost/Corrupted?

SD NP2+ FileSystem

  • Please log in to reply
2 replies to this topic

#1 Bi Qin

Bi Qin

    Member

  • Members
  • PipPip
  • 23 posts

Posted 20 May 2013 - 08:26 AM

Hello everyone.

 

I tried to create&write&read files with micro sd card on NP2+,using a 2gb sandisk micro sd card with FAT format.

 

From the debug output information,I can see that file did created and exist.But when I remove the card from NP2+ and try to read the content from my PC,I found nothing,sometimes file exist but corrupted.

Even not removing the card, just re-deploy the same code again(as the chip will reboot),the file created last time  disappered(I detect whether file exist before I create it).

 

After adding a 1000ms sleep,the situation improves just a little bit,which means if I write it for several times, I may get one time success saved.

 

So,is there anything wrong with my code or any tricks for NP2+ micso sd file write?

================================

using System; using System.Net; using System.Net.Sockets; using System.Threading; using System.IO; using Microsoft.SPOT; using Microsoft.SPOT.Hardware; using SecretLabs.NETMF.Hardware; using SecretLabs.NETMF.Hardware.Netduino; using SecretLabs.NETMF.IO; namespace SDFileLearn {   public class Program   {   public static void Main()   {   DirectoryInfo rootDirectory = new DirectoryInfo(@"SD");   if(!Directory.Exists(@"SDNewDir"))   { DirectoryInfo xDir= Directory.CreateDirectory(@"SDNewDir");   }   if (File.Exists(@"SDTestNew.txt"))   {   Debug.Print("File Exist!");   }   else   {   Debug.Print("Not Exist,Create New!");   }   StreamWriter xWrite = new StreamWriter(System.IO.File.Open(@"SDTestNew.txt", FileMode.Append));   xWrite.Write("Hello New Worldrn");   xWrite.Flush();   xWrite.Close();   Thread.Sleep(1000);   if (File.Exists(@"SDTestNew.txt"))   {   StreamReader xRead = new StreamReader(@"SDTestNew.txt");   Debug.Print(xRead.ReadLine());   xRead.Close();   Thread.Sleep(1000);   }   RecurseFolders(rootDirectory);   if (File.Exists(@"SDTestNew.txt"))   {   Debug.Print("File Exist!");   }   else   {   Debug.Print("Not Exist!");   }   Thread.Sleep(1000);   }   private static void RecurseFolders(DirectoryInfo directory)   {   if (directory.Exists)   {   Debug.Print(directory.FullName);   foreach (FileInfo file in directory.GetFiles())   {   Debug.Print(file.FullName);   }   foreach (DirectoryInfo subDirectory in directory.GetDirectories())   {   RecurseFolders(subDirectory);   }   }   }   } }  

 

Attached Files



#2 Bi Qin

Bi Qin

    Member

  • Members
  • PipPip
  • 23 posts

Posted 21 May 2013 - 01:24 AM

BTW,here below is my firmware information:

 

SolutionReleaseInfo.solutionVendorInfo: Netduino Plus 2 (v4.2.2.2) by Secret Labs LLC SoftwareVersion.BuildDate:   Jan 26 2013 SoftwareVersion.CompilerVersion:   410894



#3 mcfd

mcfd

    Member

  • Members
  • PipPip
  • 11 posts

Posted 18 September 2013 - 06:54 AM

I have similar problem. Sometimes a newly written does not get saved. I am also using firmware v4.2.2.2 on Netduino Plus 2.

 

 

Hello everyone.

 

I tried to create&write&read files with micro sd card on NP2+,using a 2gb sandisk micro sd card with FAT format.

 

From the debug output information,I can see that file did created and exist.But when I remove the card from NP2+ and try to read the content from my PC,I found nothing,sometimes file exist but corrupted.

Even not removing the card, just re-deploy the same code again(as the chip will reboot),the file created last time  disappered(I detect whether file exist before I create it).

 

After adding a 1000ms sleep,the situation improves just a little bit,which means if I write it for several times, I may get one time success saved.

 

So,is there anything wrong with my code or any tricks for NP2+ micso sd file write?

================================

using System; using System.Net; using System.Net.Sockets; using System.Threading; using System.IO; using Microsoft.SPOT; using Microsoft.SPOT.Hardware; using SecretLabs.NETMF.Hardware; using SecretLabs.NETMF.Hardware.Netduino; using SecretLabs.NETMF.IO; namespace SDFileLearn {   public class Program   {   public static void Main()   {   DirectoryInfo rootDirectory = new DirectoryInfo(@"SD");   if(!Directory.Exists(@"SDNewDir"))   { DirectoryInfo xDir= Directory.CreateDirectory(@"SDNewDir");   }   if (File.Exists(@"SDTestNew.txt"))   {   Debug.Print("File Exist!");   }   else   {   Debug.Print("Not Exist,Create New!");   }   StreamWriter xWrite = new StreamWriter(System.IO.File.Open(@"SDTestNew.txt", FileMode.Append));   xWrite.Write("Hello New Worldrn");   xWrite.Flush();   xWrite.Close();   Thread.Sleep(1000);   if (File.Exists(@"SDTestNew.txt"))   {   StreamReader xRead = new StreamReader(@"SDTestNew.txt");   Debug.Print(xRead.ReadLine());   xRead.Close();   Thread.Sleep(1000);   }   RecurseFolders(rootDirectory);   if (File.Exists(@"SDTestNew.txt"))   {   Debug.Print("File Exist!");   }   else   {   Debug.Print("Not Exist!");   }   Thread.Sleep(1000);   }   private static void RecurseFolders(DirectoryInfo directory)   {   if (directory.Exists)   {   Debug.Print(directory.FullName);   foreach (FileInfo file in directory.GetFiles())   {   Debug.Print(file.FullName);   }   foreach (DirectoryInfo subDirectory in directory.GetDirectories())   {   RecurseFolders(subDirectory);   }   }   }   } }  







Also tagged with one or more of these keywords: SD, NP2+, FileSystem

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.