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

Committing File to SD Card

File Write Commit SD

  • Please log in to reply
36 replies to this topic

#1 teknix1

teknix1

    Member

  • Members
  • PipPip
  • 19 posts

Posted 01 June 2013 - 03:48 AM

Hi,

 

I've been trying to write files to the SD card using my Netduino Plus 2 (4.2.2.0 Firmware). I can't seem to get the file to commit. I can write the file in code, read it back, but if I move the card to another machine, my files are nowhere to be found. Looks like I need some sort of commit, but I can't find that anywhere...

 

Where is StorageDevice.Unmount? Was that moved out in the latest framework?

 

Thanks

Nic



#2 hanzibal

hanzibal

    Advanced Member

  • Members
  • PipPipPip
  • 1287 posts
  • LocationSweden

Posted 01 June 2013 - 08:45 AM

I trust you close the file after creating and writing to it?

#3 teknix1

teknix1

    Member

  • Members
  • PipPip
  • 19 posts

Posted 01 June 2013 - 02:36 PM

Hi Hanzibal,

 

Thanks for taking the time to reply.

 

Yes, I have tried closing, flushing, using, nothing seems to work. There are other threads on this forum pertaining to a similar issue but they are always for another model of the Netduino or different version of the firmware. It would seem that the work around is to unmount and remount the SD card but that seems to be hidden away in the latest firmware.

 

Here's the piece of code I am currently using:

using (FileStream fs = new FileStream(@"SDLog.txt", FileMode.Create)){    Byte[] data = Encoding.UTF8.GetBytes("test");    fs.Write(data, 0, data.Length);    fs.Flush();    fs.Close();}

Thanks

Nic



#4 ziggurat29

ziggurat29

    Advanced Member

  • Members
  • PipPipPip
  • 244 posts

Posted 02 June 2013 - 12:42 PM

add reference

SecretLabs.NETMF.IO

to get the Storage class visible, and it's Unmount method

StorageDevice.Unmount("SD");



#5 teknix1

teknix1

    Member

  • Members
  • PipPip
  • 19 posts

Posted 02 June 2013 - 02:15 PM

Hi Ziggurat,

 

Thanks for pointing me inthe right direction. I guess I have to go back to the drawing board, calling Unmount on the Netduino Plus is unsupported... http://forums.netdui...o-plus/?p=23772

 

I'm running out of ideas... I don't really want to have to build my own firmware, or have to flash another one. Is anybody able to write to the SD card and have it committed right away?

 

Thanks

Nic



#6 hanzibal

hanzibal

    Advanced Member

  • Members
  • PipPipPip
  • 1287 posts
  • LocationSweden

Posted 02 June 2013 - 04:20 PM

Have you tried different SD cards, formatting, sizes, etc? Also, try writing a large file, say 4k+ and see what happens. There seems to be some kind of lazy writing going on.

#7 teknix1

teknix1

    Member

  • Members
  • PipPip
  • 19 posts

Posted 02 June 2013 - 09:05 PM

Hi Hanzibal,

 

Unfortunately I do not have different SD cards, I have two of the same, the only ones I could find locally... Transcend 2GB. I have tried FAT16 and 32 and writing large files. Every once in a while I see that my file got created or modified and most of the time when that happens, the file is corrupted and I must run chkdsk on my computer to fix it, which doesn't fix it...

 

I'm thinking exactly the same as you; delayed writing. There must be a way to control that... which is why I was trying to unmount it earlier, but after looking at the source code of the StorageDevice class, I have determined that the card is not loaded through that class on the Netduino Plus 2. So therefore I cannot unload it through that, the class doesn't have the necessary internal variables set in order to allow me to unmount it.

 

Thanks

Nic



#8 hanzibal

hanzibal

    Advanced Member

  • Members
  • PipPipPip
  • 1287 posts
  • LocationSweden

Posted 03 June 2013 - 01:04 AM

There was this other similar case a while back, maybe you've already seen it:

http://forums.netdui...other-problems/

Unfortunately, I don't beleive it was solved eventually but there might be something useful in there.

#9 teknix1

teknix1

    Member

  • Members
  • PipPip
  • 19 posts

Posted 03 June 2013 - 01:43 AM

Thanks for pointing me to the thread. I tried the suggested code, but I get the same result, either a corrupted file or no file at all. There's got to be a way to fix this... I have to wonder if this is fixed in the .Net Micro Framework 4.3.

 

Nic



#10 Bi Qin

Bi Qin

    Member

  • Members
  • PipPip
  • 23 posts

Posted 03 June 2013 - 06:04 AM

Same problem with me,and still no solution.



#11 hanzibal

hanzibal

    Advanced Member

  • Members
  • PipPipPip
  • 1287 posts
  • LocationSweden

Posted 03 June 2013 - 08:27 AM

Obviously, it's not supposed to be this way. Admin: This issue should be escalated.

#12 teknix1

teknix1

    Member

  • Members
  • PipPip
  • 19 posts

Posted 03 June 2013 - 12:33 PM

Yes, definitely shouldn't be this hard to write a file. I will try to find some time to test out .Net Micro Framework 4.3. Nic

#13 louyo

louyo

    Member

  • Members
  • PipPip
  • 18 posts

Posted 06 June 2013 - 12:23 AM

I try this little program:

 

 byte[] readdata = Encoding.UTF8.GetBytes("this is a test");
            using (var picFile = new FileStream(@"SDtest.txt", FileMode.Create, FileAccess.Write))
            {
                try
                {              
                    picFile.Write(readdata, 0, readdata.Length);                 
                }
            finally
            {
                picFile.Flush();
                picFile.Close();
            }
             
            }
            return;

The file is not on the SD card when I plug it into my host. Workarounds: 

1. Set a breakpoint on the return statement, it is there.

2. run the program in non debug mode and it is there.

3. Add  Debug.Print("Writing complete"); before the return statement and it is there.

 

Workarounds, not fixes. :)



#14 teknix1

teknix1

    Member

  • Members
  • PipPip
  • 19 posts

Posted 06 June 2013 - 03:32 AM

That is so interesting, everything works fine when the debugger is not connected. I simply click stop in VS 2010 after running my code (which disconnects from the netduino but the code is still running) and the file gets written. How can this be? I will have to see if this works all the time. Hopefully this is not just a fluke.

 

Thanks for pointing it out.

 

Nic



#15 Robert L.

Robert L.

    Advanced Member

  • Members
  • PipPipPip
  • 100 posts

Posted 24 June 2013 - 11:29 PM

So I was having the same issue as discussed in this thread.  Namely that issuing a Flush(); and a Close(); did not cause StreamWriter data to get written to the SD card.  I assume it would *eventually* get written, but I needed it written *now*, not at some point in the future. 

 

The solution I came up with is a bit strange, but may have less overhead than unmounting the SD card, which others have said also works.  I just added these two lines to my program, after the Flush() and Close(); statements:

 byte[] b = System.Text.Encoding.UTF8.GetBytes( "dummy data to force StreamWriter data to get written to SD" ); File.WriteAllBytes(@"SDdummy.txt", b );

Apparently creating/writing a new file causes the data from the other file to make it's way immediately to the SD card.  I am guessing there is a single buffer or set of buffers that is shared.  So when the above needs them, the prior content gets written.

 

Note that the file "dummy.txt" ends up being created but empty, but I do not care about it's contents in this case.



#16 Jack Chidley

Jack Chidley

    Advanced Member

  • Members
  • PipPipPip
  • 99 posts

Posted 30 June 2013 - 06:47 PM

Tried all of the above - nothing works.  N2+ 4.3 and 4.2.2. 



#17 Capt Paul

Capt Paul

    New Member

  • Members
  • Pip
  • 3 posts

Posted 01 July 2013 - 10:50 PM

  I have found the examples in Chapter 15, of "Professional's Guide to .Net Micro Framework Application Development" by Sean D.Liming and John R. Malin to work just fine on N+ running 4.1.6.  I have not tried it on my N+2.

 

The only thing I noticed is that the file name is formed like this

var filename = Path.Combine("SD","test.txt");

....

and used like this:

File.WriteAllBytes(filename,writebytes);

 

Works for me. See exercise EX1501 in the book.



#18 Jack Chidley

Jack Chidley

    Advanced Member

  • Members
  • PipPipPip
  • 99 posts

Posted 01 July 2013 - 11:32 PM

Works fine on my N+ but not on the N+2

#19 Don King

Don King

    Advanced Member

  • Members
  • PipPipPip
  • 30 posts
  • LocationLawrence, KS

Posted 12 July 2013 - 01:12 AM

Hi - I just wanted to bump this topic and see if this has been addressed any further. I am more than a little frustrated with Secret Labs right now because they are refusing to answer posts it seems related to the facts that N+2 has issues with I/O to an SD Card. I was going back and forth fine with Chris, even taking his suggestion and buying the exact same card (from the link he provided to Amazon) and trying that. I uninstalled and reinstalled frameworks and all of that. I even had purchased the N+2 (already had the 1st gen card) to get support for One Wire.

 

Here is my interaction in the Visual Studio 2012 thread:

 

http://forums.netdui...-support/page-2

 

Secret Labs - This is really unacceptable to just stop communicating with your user base. I am a software developer and understand there are issues with software that have to be addressed. But part of being a professional shop is acknowledging and communicating about issues. 

 

Please provide some feedback on this issue.

 

Thanks!

 

Don



#20 teknix1

teknix1

    Member

  • Members
  • PipPip
  • 19 posts

Posted 12 July 2013 - 02:35 AM

Hi Don,

 

As far as I know nothing has moved, we are still where we were quite some time ago. Although a bug like this is quite bad in my opinion, it is limited to debug only. If I run my application with the netduino disconnected from my computer, everything is fine. I'm sure this is not what you wanted to hear, at least it is fine in a "production" environment.

 

I would be more than happy to see something like this fixed.

 

Nic






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.