Safely ejecting SD card (and other problems) - Page 2 - Netduino Plus 2 (and Netduino Plus 1) - Netduino Forums
   
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

Safely ejecting SD card (and other problems)

sd uSd datacard datalogging

  • Please log in to reply
36 replies to this topic

#21 Verdris

Verdris

    Advanced Member

  • Members
  • PipPipPip
  • 128 posts
  • LocationReno, NV

Posted 14 February 2013 - 07:46 PM

Okay, so Identical code did not work on my NP2. I'm staring at a System.IO.IOException in Microsoft.SPOT.IO.dll. Could this actually be a problem with the framework and not the Netduino?



#22 hanzibal

hanzibal

    Advanced Member

  • Members
  • PipPipPip
  • 1287 posts
  • LocationSweden

Posted 14 February 2013 - 08:49 PM

Either or borh would be equally probable ro me. Still can't create the file? It's only natural to get an exception on a surprice ejection but why isn't it caught? On what line exactly do you get the exception? Regarding corruption, you might very well get a corrupted file system depending on exactly when you eject the card with respect to what part of the code is being executed as you do so. I don't think you could ever safely eject the card runtime unless you do it when you know for sure that the Netduino is not in the process of writing to it. Why not simply put a button that you would press like a split second or so before you go ahead and actually eject the card? A corresponding interrupt could be used to set cardInserted=false just before the evet handler would. After re-inserting the card, everything would go back normal by it self. You could even put a photointerrupter or something to detect your fingers just outside the card bay using a hysteresis of say 2 seconds. Hmm... When come to think of it, the problem could be that the N2+ is much faster than the N+ and thus the N2+ spends most of its time waiting for the SD card to finish writing. This would almost guarantee that a surprice ejection always occurs during a write operation with a good chance of corrupting the file system. But on the other hand, you sleep for a whole second in the end of each lerp round so it should in fact be sitting there for like 99.99 percent of the time...hmm.

#23 Verdris

Verdris

    Advanced Member

  • Members
  • PipPipPip
  • 128 posts
  • LocationReno, NV

Posted 14 February 2013 - 09:00 PM

Either or borh would be equally probable ro me. Still can't create the file? It's only natural to get an exception on a surprice ejection but why isn't it caught? On what line exactly do you get the exception? Regarding corruption, you might very well get a corrupted file system depending on exactly when you eject the card with respect to what part of the code is being executed as you do so. I don't think you could ever safely eject the card runtime unless you do it when you know for sure that the Netduino is not in the process of writing to it. Why not simply put a button that you would press like a split second or so before you go ahead and actually eject the card? A corresponding interrupt could be used to set cardInserted=false just before the evet handler would. After re-inserting the card, everything would go back normal by it self. You could even put a photointerrupter or something to detect your fingers just outside the card bay using a hysteresis of say 2 seconds.

 

Still can't create the file. When I step through the code, I get a lot of "FileStream.cs not found" stuff, notably at using (var fileStream = new FileStream(@"SDtest.txt", FileMode.Append, FileAccess.Write));.

 

I tried using the onboard button as an interrupt to do exactly what you suggest. Didn't work.



#24 hanzibal

hanzibal

    Advanced Member

  • Members
  • PipPipPip
  • 1287 posts
  • LocationSweden

Posted 14 February 2013 - 10:19 PM

Sounds to me a bit like you're missing one or more assemblies. Which assemblies does your project refer to? EDIT: These are the ones I have: Attached File  image.jpg   23.35KB   15 downloads Clearly, I could do without some of them but shouldn't do any harm leaving them be. Also, it just occurred to me that by the error message stating "...FileStream.cs not found," it sounds like you're running with the debugger attached. If so the dugger will break on all exceptions by default and so you want to change this so that it only breaks on unhandled exceptions. I think you do this under Debug -> Debugger... or something like that.

#25 Verdris

Verdris

    Advanced Member

  • Members
  • PipPipPip
  • 128 posts
  • LocationReno, NV

Posted 15 February 2013 - 09:28 PM

Sounds to me a bit like you're missing one or more assemblies.
Which assemblies does your project refer to?

EDIT: These are the ones I have:
Posted Imageimage.jpg
Clearly, I could do without some of them but shouldn't do any harm leaving them be.

Also, it just occurred to me that by the error message stating "...FileStream.cs not found," it sounds like you're running with the debugger attached. If so the dugger will break on all exceptions by default and so you want to change this so that it only breaks on unhandled exceptions. I think you do this under Debug -> Debugger... or something like that.

I had your same assemblies. I ran the program without the debugger attached, and it still didn't make me a file.

 

EDIT: It didn't make me a file when I started the program without a memcard, and then inserted it after reset/powerup. When I started the program with a memcard already inserted, it worked.

 

Then, for giggles, I erased and put the card back in without resetting anything. The file was created, but corrupted. Maybe this was a result of ejecting mid-read. 

 

Maybe it's just temperamental. It might just need a nice reset after mounting the card.



#26 hanzibal

hanzibal

    Advanced Member

  • Members
  • PipPipPip
  • 1287 posts
  • LocationSweden

Posted 15 February 2013 - 09:54 PM

Hmm...feels like safe to say now, the goings on are getting peculiar still. Time to summon the walker: Chris, Chris, Chris we need you over here, they're in trees, they're in the skies, my god they're all over the place. Ahhh, took one in the knee, think I'm going down, reeeeech...ra-ta-ta-ta-ta-ta, pow, pow. Then...silence...tadam, tadam, where are we? I can't see for all the smoke and my eys are all swollen. Look out, they're coming back now fore the kill. Arrrgh. Mayday, juneday, anyday...Feels like I'm flying, I'm so light, can reach out and touch the clouds, so fluffy, so moist and soft, oh the, the, ermahgerd...lerp. EDIT: That is to say, I'm all out of ideas now. Perhaps somebody else could try the stripped code on their N2+?

#27 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 19 February 2013 - 02:21 AM

hanzibal, you could be a poet :)

Verdis...there are a lot of topics in a single thread here, so let's try to sort them out one at a time.

To answer your first question, the SD card is auto-mounted on Netduino Plus 2 when you insert it. Or when the board boots. It's mounted as "SD".

This means that you can Directory.Exists to check for @"SD". If it exists, an SD card is present. If it doesn't exist, no SD card is present.

I would still use exception handling in my code, generally. But you don't need to throw an exception to know that no SD card is present.

Now on the other issues... Netduino Plus 2 is much faster than Netduino Plus 1, so it is possible that we're talking (or disconnecting) too quickly with the SD card. If we can create a simple repro (<20 lines) that works on Netduino Plus but doesn't work on Netduino Plus 2...I can take a look and see what's happening.

BTW, you can use File.Exists to see if a current file exists. And then delete it. And then create a new file. The options for the file streams should work as documented too; if there's an issue there we can dig into that as well.

Chris

#28 hanzibal

hanzibal

    Advanced Member

  • Members
  • PipPipPip
  • 1287 posts
  • LocationSweden

Posted 22 February 2013 - 06:25 PM

hanzibal, you could be a poet :)

Well, I am a writer you know - a writer of code that is :-) Actually, one of the best programmers I know uses that in his resumé to state his profession as simply a "writer of code". I find that pretty cool. Anyway, we kind of cooked it down to its essentials, now I guess it's up to Verdris to stripp it further til the "core of pudle" reveils itself. Thanks for taking the time Chris.

#29 Verdris

Verdris

    Advanced Member

  • Members
  • PipPipPip
  • 128 posts
  • LocationReno, NV

Posted 23 February 2013 - 06:48 AM

Well, I am a writer you know - a writer of code that is :-) Actually, one of the best programmers I know uses that in his resumé to state his profession as simply a "writer of code". I find that pretty cool. Anyway, we kind of cooked it down to its essentials, now I guess it's up to Verdris to stripp it further til the "core of pudle" reveils itself. Thanks for taking the time Chris.

I'm working on the code to get this working. I'll update soon.

 

EDIT: As it stands, I've wrapped any code that writes to an SD card in "if(Directory.Exists(@"SD"))". What happens is an exception gets thrown every time if the card isn't inserted. Furthermore, inserting the card mid-program causes the Netduino to do...something. I'm not sure what, but it causes the debugger to stop and I need to power cycle in order to get new code to upload.

 

Starting the program with the card in works quite well. Data logs just fine. However, when ejecting the card, I get "An unhandled exception of type 'System.IO.IOException' occurred in Microsoft.SPOT.IO.dll".

 

EDIT EDIT: With the debugger not attached, after removing the SD card it goes back into normal operation, but it takes about 30 seconds. Why would that be? Re-inserting the card calls the InsertEventHandler immediately and the Netduino resumes normal operation, but the file is not written to. It seems that the best way to datalog is to make sure that the card is present at reset/powerup, and after every card insertion, reset/powerup is necessary.

 

EDIT EDIT: We're back to corrupting files constantly. After every write, I flush the streamwriter, close it, turn on an LED, and sleep for 1 second. I figure that while the LED is on, I can safely remove the SD card, as nothing is writing to or reading from it. This turns out to not be the case. Is there a specific method to call that ensures the card isn't active at the time of removal?

 

EDIT EDIT EDIT: So, it seems that this is only an intermittent problem. However, it's enough of a problem to hinder moving forward. I need to reliably collect data, and it's just not being reliable.

 

I went with a hunch that maybe "if(File.Exists())" is locking up the card, as it's ensuring that the file exists for the entire contents of the if loop. So I made a boolean that informs me if the write procedure was successful, and then another if statement that activates LED feedback:

 

if (File.Exists(@"SDgrounded.txt"))                {                    try                    {                        using (var filestream = new FileStream(@"SDgrounded.txt", FileMode.Append, FileAccess.Write))                        {                            StreamWriter writer = new StreamWriter(filestream);                            writer.WriteLine(counts);                            writer.Flush();                            writer.Close();                            writeSuccessful = true;                        }                    }                    catch (Exception ex)                    {                        Debug.Print("I found an " + ex.Message);                    }                }                else                {                    writeSuccessful = false;                }                if (writeSuccessful)                {                    RGB.Green();                    AD7714.Init();                    Thread.Sleep(1000);                    RGB.Off();                }                else                {                    RGB.Red();                    AD7714.Init();                    Thread.Sleep(1000);                    RGB.Off();                }

But this doesn't always work either. However, it does speed up the calling of the Eject handler.

 

Also, there appears to be a difference between

using (var filestream = new FileStream(@"SDgrounded.txt", FileMode.Append, FileAccess.Write))

 

and

using (var filestream = new FileStream(@"SDgrounded.txt", FileMode.Append, FileAccess.Write))

 

Why?

 

EDIT EDIT EDIT EDIT: The error I get when scanfixing the SD card on my computer is that the first allocation unit is not valid. So, I tried creating a nonempty grounded.txt file, inserting that SD card into the NP2, and letting it rip. So far, no corrupted files.

 

Also, I created a nonempty file derp.txt and let the Netduino make grounded.txt. This still occasionally corrupts grounded.txt, but derp.txt remained unaffected.

 

I think the problem is that the NP2 still has a hold on the file somehow, and when ejecting the card it becomes a matter of timing. I'm going to increase my Thread.Sleep to 2000 and see if that makes a difference.



#30 smurf

smurf

    New Member

  • Members
  • Pip
  • 2 posts

Posted 08 March 2013 - 04:43 PM

I feel you're pain bin struggling with the safely removing of sd card for the past 2 weeks..

 

At times the event handler works fine with ejecting the sd card but as soon as i insert it the the whole program freezes and need to repower the netduino just to get it up and going again.

 

Other problems I encountered is as it may work when debugging but when powering it up from external power supply meaning not via usb the event handler just doesn't do anything. Got a 2 color led hooked up red if sdcard ejected and green if inserted but its just doesnt do anything if you eject the sd card or reinsert it.

 

Would really love it if someone got a solution for this..



#31 Verdris

Verdris

    Advanced Member

  • Members
  • PipPipPip
  • 128 posts
  • LocationReno, NV

Posted 09 March 2013 - 06:02 AM

I feel you're pain bin struggling with the safely removing of sd card for the past 2 weeks..

 

At times the event handler works fine with ejecting the sd card but as soon as i insert it the the whole program freezes and need to repower the netduino just to get it up and going again.

 

Other problems I encountered is as it may work when debugging but when powering it up from external power supply meaning not via usb the event handler just doesn't do anything. Got a 2 color led hooked up red if sdcard ejected and green if inserted but its just doesnt do anything if you eject the sd card or reinsert it.

 

Would really love it if someone got a solution for this..

I hacked together a way to at least avoid too much headache. Every four hours, my board makes a backup of the datafile. I keep all the backups. That way, if I corrupt the primary datafile on eject, at worst I lose 3:59:59 of data instead of the whole weekend. Shorter experiments can have more or less frequent backups, or only write one backup, etc.



#32 hanzibal

hanzibal

    Advanced Member

  • Members
  • PipPipPip
  • 1287 posts
  • LocationSweden

Posted 10 March 2013 - 12:40 AM

Too bad but it didn't come out right after you put so much work into it but now at least you got something that kind of works rather than not at all.

 

I still think somebody else should give your code a go on their board to see if there could be some special circumstances about your "environment".



#33 Verdris

Verdris

    Advanced Member

  • Members
  • PipPipPip
  • 128 posts
  • LocationReno, NV

Posted 10 March 2013 - 12:49 AM

Too bad but it didn't come out right after you put so much work into it but now at least you got something that kind of works rather than not at all.

 

I still think somebody else should give your code a go on their board to see if there could be some special circumstances about your "environment".

I have two NP2s, and tried my code on both. No luck :(

 

I'm wondering about the "automatic unmount" that the NP2 claims. I've never heard of anything like that. Is there a particular method one might call in order to force an unmount before ejecting? It could even be wired into an interrupt with an associated boolean to prevent the main thread from accessing the card any further.



#34 hanzibal

hanzibal

    Advanced Member

  • Members
  • PipPipPip
  • 1287 posts
  • LocationSweden

Posted 10 March 2013 - 01:15 AM

I don't know about forcing an unmount, but I guess reading from SD with an actual file system on it (such as FAT or FAT32) and not just raw data, could be quite a complex and resource demanding task. So the firmware probably just releases the related resources when it detects the "card inserted" pin indicates the card has been removed. Doing so probably saves some precious RAM when you're not using the card reader.

 

I'm really reaching now but I trust you don't have any strong magnetic fields close to your workbench or anything like that?

 

How often does the file get corrupted, is it somewhat periodic in nature? Could it be related to an electric motor going on/off at times, like an air compressor or a big refrigerator?

 

You haven't forgot to mention that you live next door to a nuclear power plant, have you?

;) 



#35 Verdris

Verdris

    Advanced Member

  • Members
  • PipPipPip
  • 128 posts
  • LocationReno, NV

Posted 11 March 2013 - 11:39 PM

No crazy magnetic fields, no NMR, no MRI, no nuclear explosions, no supermassive stellar remnants.

 

The corruption seems purely random. Sometimes I can eject and happily read the memory card. Other times I can open the memory card, but the file won't open. Other times it tells me it has a problem the instant I put it in my computer.



#36 Niels R.

Niels R.

    Member

  • Members
  • PipPip
  • 28 posts

Posted 24 April 2013 - 11:59 AM

No crazy magnetic fields, no NMR, no MRI, no nuclear explosions, no supermassive stellar remnants.

 

The corruption seems purely random. Sometimes I can eject and happily read the memory card. Other times I can open the memory card, but the file won't open. Other times it tells me it has a problem the instant I put it in my computer.

Could it be that the corruption takes place when ejecting the SD card while writing to it?

 

Niels



#37 smurf

smurf

    New Member

  • Members
  • Pip
  • 2 posts

Posted 27 November 2013 - 06:24 PM

Has any body made any progress on this would still like to be able to do this without having my netduino crash or have the file corrupted.







Also tagged with one or more of these keywords: sd, uSd, datacard, datalogging

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.