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

SD Card Write issue

SD issue

Best Answer Dizel, 03 October 2014 - 12:17 PM

using(FileStream file = new FileStream(fileWrite, FileMode.OpenOrCreate, FileAccess.ReadWrite))
using (StreamWriter writer = new StreamWriter(file))
{
    writer.WriteLine("Hello");
    writer.WriteLine("SD");
}

Thanks sfx

 

At all, this code works fine, but my Windows 8 can't recognize SD after file writing, but i can read data programmatically. Amazing.

 

Solution: format e: /fs:FAT

Read\Write only from Netduino

Go to the full post


  • Please log in to reply
5 replies to this topic

#1 Dizel

Dizel

    New Member

  • Members
  • Pip
  • 5 posts

Posted 02 October 2014 - 12:19 PM

Hello guys,

I have problem with SD card, when i read from it all fine, but when i try write i have Exception in System.IO

DirectoryInfo dir = new DirectoryInfo(@"\SD\");
bool dirExist = dir.Exists;
var files = dir.GetFiles();
string fileName = @"\SD\test.txt.txt";
string fileWrite = @"\SD\testWrite.txt";
bool fileExist = File.Exists(fileName);
OutputPort led = new OutputPort(Pins.ONBOARD_LED, false);
using (FileStream file = 
    new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite))

//using (FileStream file=
    new FileStream(fileWrite, FileMode.OpenOrCreate, FileAccess.ReadWrite))
//using(StreamWriter wr = new StreamWriter(fileWrite))
//{
//    wr.WriteLine("Hello");
//    wr.WriteLine("StreamWriter");
//}

using (StreamReader reader = new StreamReader(file))
{
    string line;
    while ((line = reader.ReadLine()) != null)
    {
        led.Write(true);
        Thread.Sleep(250);
        led.Write(false);
        Thread.Sleep(250);
     }
}

Exception raised in commented code

 

Can some one help me?



#2 Dizel

Dizel

    New Member

  • Members
  • Pip
  • 5 posts

Posted 02 October 2014 - 08:33 PM

I use VS 2013 and last version of MF and Netduino SDK, also firmware on my board 4.3.1



#3 sfx

sfx

    Advanced Member

  • Members
  • PipPipPip
  • 52 posts

Posted 03 October 2014 - 12:08 AM

Hi Dizel,

 

The full .NET Framework will ensure that a FileStream object is disposed of by any wrapper objects that get created (e.g. StreamWriter etc). Because of this, it isn't necessary to call dispose (via a using statement) on a wrapped FileStream since the wrapper will do it for you. Of course, in the interests of readability and consistency it is good practice to always explicitly declare your intentions on a FileStream with either a using statement or a try/finally block. However, I'm not sure if NETMF behaves the same way as its big brother on this.

 

At any rate, it appears that instead of your StreamWriter instance wrapping your FileStream object both are wrapping the shared file path. This indicates to me you have two Stream derived objects competing for the same resource at the same time.

 

Try this:

using (FileStream file = new FileStream(fileWrite, FileMode.OpenOrCreate, FileAccess.ReadWrite))
using (StreamWriter wr = new StreamWriter(file))
{
    wr.WriteLine("Hello");
    wr.WriteLine("StreamWriter");
}

Take care,

 

Nathan



#4 Dizel

Dizel

    New Member

  • Members
  • Pip
  • 5 posts

Posted 03 October 2014 - 11:19 AM

I do not know what is going on, but i hate this "Magic". After several(approximately 100 :blink: (4 hours)) times of debugging application, it can some time work, some time did not work.Deployment Erasing and VS rebooting can some time help, some time no. I wasted a lot of time ... and i cant find any logic dependencies.

 

And again Reader works fine  <_<

 

Default .Net application with SD Card works fine.



#5 Dizel

Dizel

    New Member

  • Members
  • Pip
  • 5 posts

Posted 03 October 2014 - 12:17 PM   Best Answer

using(FileStream file = new FileStream(fileWrite, FileMode.OpenOrCreate, FileAccess.ReadWrite))
using (StreamWriter writer = new StreamWriter(file))
{
    writer.WriteLine("Hello");
    writer.WriteLine("SD");
}

Thanks sfx

 

At all, this code works fine, but my Windows 8 can't recognize SD after file writing, but i can read data programmatically. Amazing.

 

Solution: format e: /fs:FAT

Read\Write only from Netduino



#6 tridy

tridy

    Advanced Member

  • Members
  • PipPipPip
  • 68 posts
  • LocationStockholm, Sweden

Posted 16 June 2015 - 11:25 PM

I do not know what is going on, but i hate this "Magic". After several(approximately 100 :blink: (4 hours)) times of debugging application, it can some time work, some time did not work.Deployment Erasing and VS rebooting can some time help, some time no. I wasted a lot of time ... and i cant find any logic dependencies.

 

And again Reader works fine  <_<

 

Default .Net application with SD Card works fine.

 

Exactly the same story with Netduino 3 WiFi today. Got two 2GB micro-SD cards, could read from both PC and Netduino, could not write to Netduino. Suddenly, after 2 hours of debugging and trying to figure out the problem it started to work.

 

My only guess that I can connect to is that either after the first usage of StreamReader (before that I was using just Directory.GetDirectories() and Directory.GetFiles() to get the list of directories and files) or File.Exists() it triggered the magic of being able to write files.

 

I tried to undo the magic by reformatting the card, erasing Netduino from Deployment Tool, restarting Visual Studio, but the magic remains.

    public class Program
    {
        private const string FileName = @"\SD\FromND.txt";

        public static void Main()
        {
            WriteTestFile();
            ReadDirectoriesAndFiles();
            ReadTestFile();
        }

        private static void WriteTestFile()
        {
            if (File.Exists(FileName))
            {
                File.Delete(FileName);
            }

            using(var streamWriter = new StreamWriter(FileName, false))
            {
                streamWriter.WriteLine("This is the read line from netduino file on microSD card " + FileName);
                streamWriter.Flush();
                streamWriter.Close();
            }
        }

        private static void ReadTestFile()
        {
            using(StreamReader reader = new StreamReader(FileName))
            {
                string allText = reader.ReadToEnd();
                reader.Close();
                Debug.Print(allText);
            }
        }

        public static void ReadDirectoriesAndFiles()
        {
            Debug.Print("Directories and Files: ");

            foreach(var directoryName in Directory.GetDirectories(@"\"))
            {
                Debug.Print(directoryName);
                foreach(var fileName in Directory.GetFiles(directoryName))
                {
                    Debug.Print(fileName);
                }
            }
        }
    }

so, it seems to be working now. It does not feel "stable" but I will post if the magic disappears and the problem comes back.







Also tagged with one or more of these keywords: SD, issue

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.