Netduino home hardware projects downloads community

Jump to content


Photo

[SD Card] How to write/read files


  • Please log in to reply
13 replies to this topic

#1 LordOfTheFlakes

LordOfTheFlakes

    Member

  • Members
  • PipPip
  • 25 posts

Posted 20 June 2012 - 05:43 PM

Hey everybody, I have a problem: I'm trying to write data on my sd-card, but I can't add a "using" statement for System.IO or Microsoft.SPOT.IO (not found) I have installed .net MF 4.2. Is that normal? What shall I do? Thanks LordOfTheFlakes
Please correct my wrong English! ;)

#2 Stefan

Stefan

    Moderator

  • Members
  • PipPipPip
  • 1965 posts
  • LocationBreda, the Netherlands

Posted 20 June 2012 - 06:46 PM

Hi LordOfTheFlakes, Have you also included the System.IO reference to your project? You have to add it as a reference before the using statement will work.
"Fact that I'm a moderator doesn't make me an expert in things." Stefan, the eternal newb!
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs

#3 LordOfTheFlakes

LordOfTheFlakes

    Member

  • Members
  • PipPip
  • 25 posts

Posted 20 June 2012 - 06:57 PM

Thank you for that hint!

I can't find SecretLabs.NETMF.IO.dll on my Computer. Where can I download it?
It is not part of the C:\Program Files\Secret Labs\Netduino SDK\Assemblies\v4.1\ folder ...

Sorry for all these beginner questions :unsure:

LordOfTheFlakes

EDIT: OOPPPS System.IO :huh: Ok I added the reference and the errors are gone. Thanks!
Please correct my wrong English! ;)

#4 LordOfTheFlakes

LordOfTheFlakes

    Member

  • Members
  • PipPip
  • 25 posts

Posted 20 June 2012 - 07:14 PM

Sorry for that double-post, but when I try to read the directories, I get this error:

Eine Ausnahme (erste Chance) des Typs "System.IO.IOException" ist in Microsoft.SPOT.IO.dll aufgetreten.
#### Exception System.IO.IOException - CLR_E_INVALID_DRIVER (1) ####
#### Message:
#### Microsoft.SPOT.IO.NativeIO::GetAttributes [IP: 0000] ####
#### System.IO.Directory::Exists [IP: 0015] ####
#### System.IO.DirectoryInfo::get_Exists [IP: 0007] ####
#### netduinoIO.Program::RecurseFolders [IP: 0005] ####
#### netduinoIO.Program::Main [IP: 000c] ####

Code:

public static void Main()
        {
            DirectoryInfo rootDirectory = new DirectoryInfo(@"\SD\");
            RecurseFolders(rootDirectory);
            Thread.Sleep(Timeout.Infinite);
        }

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

Might the memory capacity of my SD card be the problem? (32 GB)

LordOfTheFlakes
Please correct my wrong English! ;)

#5 Stefan

Stefan

    Moderator

  • Members
  • PipPipPip
  • 1965 posts
  • LocationBreda, the Netherlands

Posted 20 June 2012 - 07:25 PM

Might the memory capacity of my SD card be the problem? (32 GB)

SDHC, that's only supported with a beta version of the firmware:
http://forums.netdui...re-v411-beta-1/
"Fact that I'm a moderator doesn't make me an expert in things." Stefan, the eternal newb!
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs

#6 LordOfTheFlakes

LordOfTheFlakes

    Member

  • Members
  • PipPip
  • 25 posts

Posted 21 June 2012 - 12:42 PM

Great man, it works now properly on my 32 GB sd-card!

LordOfTheFlakes

EDIT:

I can use the code above to list the directories. BUT I tried to write an read a file:

1. Writing was sucessfull, I can view the files on my computer.
2. Reading causes this error:

#### Exception System.IO.IOException - CLR_E_INVALID_DRIVER (1) ####
#### Message:
#### Microsoft.SPOT.IO.NativeIO::GetAttributes [IP: 0000] ####
#### System.IO.File::Exists [IP: 001a] ####
#### netduinoIO.Program::ReadFile [IP: 0009] ####
#### netduinoIO.Program::Main [IP: 0008] ####


I have seen the error in this forum but there was no useful soloution ...

Codes:

private static String rootDir = @"\SD\";

Write:

public static void WriteFile(String sFilename, String sLines)
        {
            StreamWriter myFile = new StreamWriter(rootDir + sFilename);
            myFile.Write(sLines);
            myFile.Close();
        }

Read:

 public static string ReadFile(String sFilename)
        {
            string sContent = "";

            if (File.Exists(sFilename))
            {
                StreamReader myFile = new StreamReader(new FileStream(rootDir + sFilename, FileMode.Open, FileAccess.Read));
                sContent = myFile.ReadToEnd();
                myFile.Close();
            }
            return sContent;
        }

Please correct my wrong English! ;)

#7 Stefan

Stefan

    Moderator

  • Members
  • PipPipPip
  • 1965 posts
  • LocationBreda, the Netherlands

Posted 21 June 2012 - 01:02 PM

See my change:

 public static string ReadFile(String sFilename)
        {
            string sContent = "";

            // Don't use this:
            //if (File.Exists(sFilename))
            // Use this instead:
            if (File.Exists(rootDir + sFilename))
            {
                StreamReader myFile = new StreamReader(new FileStream(rootDir + sFilename, FileMode.Open, FileAccess.Read));
                sContent = myFile.ReadToEnd();
                myFile.Close();
            }
            return sContent;
        }

Did that help?
"Fact that I'm a moderator doesn't make me an expert in things." Stefan, the eternal newb!
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs

#8 LordOfTheFlakes

LordOfTheFlakes

    Member

  • Members
  • PipPip
  • 25 posts

Posted 21 June 2012 - 01:10 PM

Oh my god! Yes. That was the problem :blink: Thats soooo embarrassing .... I want to thank you very much for this great and fast support in this forum! It's not like I was a coding beginnner but I normally use vb.net so I just copied code from the internet in my project like you do it when you want to add a new feature (but don't know anything about the topic) I hate this kind of mistakes. They are soooo needless ... :mellow: The code doesn't work and the fist thing I think is: The code is from the internet -> it is right -> the problem must be the hardware ^^ and it's not. Thx ;) LordOfTheFlakes
Please correct my wrong English! ;)

#9 nakchak

nakchak

    Advanced Member

  • Members
  • PipPipPip
  • 404 posts
  • LocationBristol, UK

Posted 21 June 2012 - 02:16 PM

I hate this kind of mistakes. They are soooo needless ... :mellow:


I dunno, silly mistakes are the ones you learn from :)

Been using .net since it was known as VB7/ASP Plus and still I forget to add references and wonder why my code fails to build...

For me personally unless the code comes from Jon Skeet or a few other select .net devs i always assume its wrong until proven right, at the very least do a code review and if coming from a VB perspective check the MSDN code examples you often find subtle differences, when i made the jump to C# from VB string escapement always got me i.e.

"C:\Directory\file.txt"

needs to be either
@"C:\Directory\file.txt" //@ prefix means string literal

//or

"C:\\Directory\\file.txt" //note double \\ to escape the \ which is the C# string control char i.e. \r\n for a line break.

But having made the jump you would have a hard time convincing me to go back :)

Nak.

#10 Stefan

Stefan

    Moderator

  • Members
  • PipPipPip
  • 1965 posts
  • LocationBreda, the Netherlands

Posted 21 June 2012 - 02:33 PM

For me personally unless the code comes from Jon Skeet or a few other select .net devs i always assume its wrong until proven right

Should I feel offended? :ph34r: :lol:

With Netduino, I find working with the SD always a bit strange, due to the SD mountpoint. A few interesting things I found out:
- All IO in the root folder will fail, except for a GetDirectories and GetFiles call. File.Exists should return false, instead it gives an exception.
- The SD mount point is case sensitive. With my linux knowledge I understand that, but for .NET, I find it odd.

I'm not sure if these things are bugs or not, but it's interesting anyways.
"Fact that I'm a moderator doesn't make me an expert in things." Stefan, the eternal newb!
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs

#11 nakchak

nakchak

    Advanced Member

  • Members
  • PipPipPip
  • 404 posts
  • LocationBristol, UK

Posted 21 June 2012 - 03:00 PM

Should I feel offended? :ph34r: :lol:

With Netduino, I find working with the SD always a bit strange, due to the SD mountpoint. A few interesting things I found out:
- All IO in the root folder will fail, except for a GetDirectories and GetFiles call. File.Exists should return false, instead it gives an exception.
- The SD mount point is case sensitive. With my linux knowledge I understand that, but for .NET, I find it odd.

I'm not sure if these things are bugs or not, but it's interesting anyways.


Note "few other select .net devs" ;)

I have spent quite lot of time since monday picking apart the SD card firmware and all of that makes a lot of sense, as far as my understanding goes the netmf file system has more in common with *nix than dos based file systems, they have just wrapped it to make it more familiar to .net devs i.e. there is code in there to limit folder path depth to match the desktop experience. SD is the mount point, as you pointed out, so in windows terms trying to call File.Exists("C:") would also yield false or an IO error, yet File.Exists("C:\file.txt") would return true. I Suppose one of the reasons for not hard coding a SD\ prefix to all file path requests is the use case of multiple SD cards/storage devices and would require quite a bit of hacking at core netmf runtimes to provide that functionality and would ultimately break code portability to the wider netmf eco system.

As for case sensitivity I am sure that has more to do with platform optimisation, why do a string comparison or case conversion if you can just type the mount point in correctly? ;) Besides file IO in mono is case sensitive (or it was last time i looked a few years ago)...

Nak.

#12 Grant

Grant

    Advanced Member

  • Members
  • PipPipPip
  • 130 posts
  • LocationAustralia

Posted 12 August 2012 - 05:06 AM

Hi Everyone.

I want to put part of my code on an SD card and have it behave like it was on an EEPROM. Basically, I have this table at the start of my code that defines a whole heap of variables as int's. I use a webserver to change them, but they always need changing, and my Netduino is not always powered so they return to defaults all the time. I'm really new to programming and have been reading a all about these stream writers and such. But I can't work out from the material how I could read and write resources to the SD Card. The ultimate solution to the problem would be to put a .cs file on the card. And then when I changed my ints they might stay the same. But I bet it's going to be harder than this. Here is the part of the file I want to "remember its state"

Thanks in advance.

    public class Program
    {
        //Variable Table
        public static int FWAM = 0;
            public static int FWAM1C = 180; public static int FWAM2C = 300; public static int FWAM3C = 180; 
            public static int FWAM1T = 35;  public static int FWAM2T = 60;  public static int FWAM3T = 35;
            public static int FWAM1A = 0;   public static int FWAM2A = 1;   public static int FWAM3A = 0; 
            public static int FWAM1AA = 0;  public static int FWAM2AA = 45; public static int FWAM3AA = 0; 
            public static int FWAM1E = 0;   public static int FWAM2E = 0;   public static int FWAM3E = 0;


#13 Grant

Grant

    Advanced Member

  • Members
  • PipPipPip
  • 130 posts
  • LocationAustralia

Posted 13 August 2012 - 04:48 AM

Can some one tell me what is wrong here. I've found this example all over the web but when I try it, FileStream, FileMode, StreamReader and StreamWriter all come up with red under lines. First I thought I needed to reference SecretLabs.NETMF.IO and I did. but it didn't fix it.

Please Help.

using System;
using System.IO;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using Microsoft.SPOT.IO;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.IO;
using SecretLabs.NETMF.Hardware.NetduinoPlus;
using System.Text;

namespace ServerExample
{
    public class SDCard
    {
        public static void CardRead()
        {
            using (var filestream = new FileStream(@"\SD\dontpanic.txt", FileMode.Create))
            {
                StreamWriter streamWriter = new StreamWriter(filestream);
                streamWriter.WriteLine("This is a test of the SD card support on the netduino...This is only a test...");
                streamWriter.Close();
            }

            using (var filestream = new FileStream(@"\SD\dontpanic.txt", FileMode.Open))
            {
                StreamReader reader = new StreamReader(filestream);
                Debug.Print(reader.ReadToEnd());
                reader.Close();
            }
        }
    }
}


#14 Grant

Grant

    Advanced Member

  • Members
  • PipPipPip
  • 130 posts
  • LocationAustralia

Posted 13 August 2012 - 11:15 AM

Got it. Had to go to reference and add system.io; Sorry.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

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