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 huh?


  • Please log in to reply
9 replies to this topic

#1 Steven Behnke

Steven Behnke

    Advanced Member

  • Members
  • PipPipPip
  • 70 posts
  • LocationLas Vegas, NV

Posted 30 September 2010 - 03:01 AM

So now that I've finally found some time to play with this insanely cool Netduino+ I seem to be missing something. I don't see a reference to FileStream anywhere so that I can create a file on my micro SD card. Was I suppose to update the Netduino SDK at any point in time, or am I supposed to add an additional reference? As an aside, writing a Managed C++ wrapper layer for native C++ calls so you can call it from a C# assembly is a time consuming endeavor and not a whole lot of fun. I'm suffering from programmer paralysis doing type conversions trying to remember which language I'm typing in and if I need to delete that new pointer that I just allocated or if it will get magically garbage collected for me.

#2 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 30 September 2010 - 03:25 AM

Steven, To use the SD card, add the System.IO.dll reference to your project. You may also want to update to the Netduino Plus v4.1.0.4 beta firmware. Chris

#3 Steven Behnke

Steven Behnke

    Advanced Member

  • Members
  • PipPipPip
  • 70 posts
  • LocationLas Vegas, NV

Posted 30 September 2010 - 03:35 AM

Steven,

To use the SD card, add the System.IO.dll reference to your project.

You may also want to update to the Netduino Plus v4.1.0.4 beta firmware.

Chris


I will do that indeed!

#4 Steven Behnke

Steven Behnke

    Advanced Member

  • Members
  • PipPipPip
  • 70 posts
  • LocationLas Vegas, NV

Posted 30 September 2010 - 03:56 AM

I see no references to add. Like this: https://files.me.com...n_behnke/3ca837

#5 Steven Behnke

Steven Behnke

    Advanced Member

  • Members
  • PipPipPip
  • 70 posts
  • LocationLas Vegas, NV

Posted 30 September 2010 - 04:32 AM

Okay, I got the reference added but now I'm having some difficulty after the first read. I was just testing out the library looping through all of the files in a directory and trying to read them in a little bit at a time. The first read completes successfully and then I get an exception thrown. This is with 4.1.0.4 b1.


The thread '<No Name>' (0x2) has exited with code 0 (0x0).
Current Directory: \SD\DCIM\100CANON
File: \SD\DCIM\100CANON\IMG_0870.JPG
File Size: 1418885
	read 128 bytes...
    #### Exception System.ArgumentException - 0xfd000000 (1) ####
    #### Message: 
    #### Microsoft.SPOT.IO.NativeFileStream::Read [IP: 0000] ####
    #### System.IO.FileStream::Read [IP: 0028] ####
    #### NetduinoPlusSample.Program::Main [IP: 00bd] ####
A first chance exception of type 'System.ArgumentException' occurred in Microsoft.SPOT.IO.dll
An unhandled exception of type 'System.ArgumentException' occurred in Microsoft.SPOT.IO.dll

And my code for reading:

using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;

using System.IO;

namespace NetduinoPlusSample
{
	public class Program
	{
		public static void Main()
		{
			string currentDirectory = @"\SD\DCIM\100CANON";
			Debug.Print("Current Directory: " + currentDirectory);

			string[] directories = Directory.GetDirectories(currentDirectory);
			foreach (string directory in directories)
			{
				Debug.Print("Directory: " + directory);
			}
			
			string[] files = Directory.GetFiles(currentDirectory);
			foreach (string file in files)
			{
				Debug.Print("File: " + file);
				FileInfo fInfo = new FileInfo(file);
				Debug.Print("File Size: " + fInfo.Length);

				FileStream fStream = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.None, 1024);
				byte[] buffer = new byte[128];
				int fileLocation = 0;
				while (fileLocation < fInfo.Length)
				{
					fileLocation += fStream.Read(buffer, fileLocation, System.Math.Min(128, (int)(fInfo.Length - fileLocation)));
					Debug.Print("\tread " + fileLocation + " bytes...");
				}
			}
		}

	}
}


#6 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 30 September 2010 - 04:53 AM

Steven...what happens if you dispose of your filestream before opening another file?

#7 Steven Behnke

Steven Behnke

    Advanced Member

  • Members
  • PipPipPip
  • 70 posts
  • LocationLas Vegas, NV

Posted 30 September 2010 - 05:10 AM

Steven...what happens if you dispose of your filestream before opening another file?


I can try, but this is the same file, just the next 128 bytes of it where the exception is thrown.

#8 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 30 September 2010 - 06:02 AM

I can try, but this is the same file, just the next 128 bytes of it where the exception is thrown.


On second thought...

fileLocation += fStream.Read(buffer, fileLocation, System.Math.Min(128, (int)(fInfo.Length - fileLocation)));

fileLocation is 128 on your second loop--too high of an offset in your 128-byte buffer array. Did you want to pass 0 as your second parameter?

Chris

#9 dab

dab

    Advanced Member

  • Members
  • PipPipPip
  • 54 posts
  • LocationBellevue, WA, USA

Posted 30 September 2010 - 06:11 AM

On second thought...

fileLocation += fStream.Read(buffer, fileLocation, System.Math.Min(128, (int)(fInfo.Length - fileLocation)));

fileLocation is 128 on your second loop--too high of an offset in your 128-byte buffer array. Did you want to pass 0 as your second parameter?

Chris

Also, it looks like you can just use 128 for the 3rd parameter to FileStream.Read()...if it reads less than 128 bytes, then it should be OK (the method will return the actual number of bytes it read).
Thanks,
~ David ~

#10 Steven Behnke

Steven Behnke

    Advanced Member

  • Members
  • PipPipPip
  • 70 posts
  • LocationLas Vegas, NV

Posted 30 September 2010 - 01:46 PM

Thanks everyone. I must have been tired yesterday, or I've been writing way too much C++ lately.




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.