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

read file from SD


  • Please log in to reply
17 replies to this topic

#1 somaia

somaia

    New Member

  • Members
  • Pip
  • 4 posts

Posted 10 October 2012 - 07:34 PM

Hello I have netduino board.I interfaced 4G SD card with it.I had saved file in the SD card then I tried to read it line by line.Each line I read I write it in a new file.The program is working normally but after reading some lines this error appear "An unhandled exception of type 'System.IndexOutOfRangeException' occurred in System.IO.dll". This is my program can anyone help me please. using System; using System.Threading; using Microsoft.SPOT; using Microsoft.SPOT.Hardware; using SecretLabs.NETMF.Hardware; using SecretLabs.NETMF.Hardware.Netduino; using System.IO; using SecretLabs.NETMF.IO; using System.Text; namespace NetduinoApplication1 { public class Program { public static void Main() { // write your code here StorageDevice.MountSD("SD", SPI_Devices.SPI1, Pins.GPIO_PIN_D10); string line; // line that will read from the pliantext FileStream aa = new FileStream("SD\\enc_f.txt", FileMode.Create, FileAccess.Write, FileShare.None); StreamWriter enc_plain = new StreamWriter(aa); FileStream sr = new FileStream("SD\\text.txt", FileMode.Open);//, FileAccess.Read,FileShare.None); StreamReader r = new StreamReader(sr); int c = 0; while ((line = r.ReadLine()) != null) { line = r.ReadLine(); enc_plain.WriteLine(line); c++; } aa.Close(); enc_plain.Close(); sr.Close(); r.Close();

#2 MrSmoofy

MrSmoofy

    Advanced Member

  • Members
  • PipPipPip
  • 47 posts
  • LocationOdessa, FL

Posted 10 October 2012 - 07:57 PM

int c = 0; while ((line = r.ReadLine()) != null) { line = r.ReadLine(); enc_plain.WriteLine(line); c++; } In that code you are actually reading 2 lines before you write 1 line. ReadLine actually reads the line so your reading a line in the while and then again at line = r.Readline so that when you get to the end of the file with the while line = r.ReadLine() has read the last line then you try to read a line again but your already past the end of the file. What you want is while (r.Peek() >= 0) { enc_plain.WriteLine(r.ReadLine()); } I removed the int c = 0; and c++ and the line = r.ReadLine(); because well you don't really need it since you are not doing anything else with it. http://msdn.microsof...r.readline.aspx

#3 somaia

somaia

    New Member

  • Members
  • Pip
  • 4 posts

Posted 11 October 2012 - 09:28 AM

Ya. but |I tried This code while (r.Peek() >= 0) { enc_plain.WriteLine(r.ReadLine()); } but I give me the same error "System.IndexOutOfRangeException' occurred in System.IO.dll"

#4 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 11 October 2012 - 04:53 PM

Hi somaia, What happens if you use a 2GB card? The 4GB cards started shipping while SDHC was still a work-in-progress, so some of them don't follow the spec properly. If a 2GB card works and if we can get ahold of a card like yours, we may be able to tweak the NETMF firmware to work with it. Chris

#5 somaia

somaia

    New Member

  • Members
  • Pip
  • 4 posts

Posted 11 October 2012 - 07:31 PM

Hi Chris I’m sorry, but I’m beginner in netduino. Do you mean, I have to replace the 4G-SD card I have with 2G-SD card. Or could you please explain the solution of the problem in more details. Thanks

#6 somaia

somaia

    New Member

  • Members
  • Pip
  • 4 posts

Posted 12 October 2012 - 11:36 PM

Hi I tried 2G-SD card but it gives me the same error " An unhandled exception of type 'System.IndexOutOfRangeException' occurred in System.IO.dll". What can i do?

#7 lastsoldier

lastsoldier

    New Member

  • Members
  • Pip
  • 1 posts
  • LocationTurkey

Posted 07 May 2013 - 09:15 PM

Hi 

I tried to read 32kb from text and send with SPI, I read data line line, line is 896bytes.(35 part). When read is finished, I send 32kb data with SPI  there is no problem but this way about 15 seconds. If I want to send another one I must wait 15seconds.  What can I do?



#8 Nutz95

Nutz95

    Member

  • Members
  • PipPip
  • 11 posts

Posted 15 July 2013 - 06:40 PM

I also have tried to read a file line by line, i've got the excat same exception.

I have this exception once i hit position 2048. (my file is more than 5000 characters)

Is ther any limitation to 2048 chars per file?



#9 Duefectu

Duefectu

    Advanced Member

  • Members
  • PipPipPip
  • 34 posts

Posted 06 December 2013 - 12:11 PM

I have this exception Reading files in the 101 line of the file!



#10 Nutz95

Nutz95

    Member

  • Members
  • PipPip
  • 11 posts

Posted 06 December 2013 - 06:36 PM

So i guess you have the exact same issue as mine.



#11 Duefectu

Duefectu

    Advanced Member

  • Members
  • PipPipPip
  • 34 posts

Posted 06 December 2013 - 06:51 PM

@Nutz95, do you use Ethernet connection at same time, TCP Listener or Socket?

Today I making test without Ethernet connection, and it Works!



#12 Nutz95

Nutz95

    Member

  • Members
  • PipPip
  • 11 posts

Posted 06 December 2013 - 07:10 PM

@Duefectu

It depends, i have 1 project that is using ethernet, and another one that is not.

 

Did you do something special to disable the ethernet part?

i'll give it some tries when i get back my board.



#13 Duefectu

Duefectu

    Advanced Member

  • Members
  • PipPipPip
  • 34 posts

Posted 06 December 2013 - 08:16 PM

To disable network, I unplug the Ethernet cable! :rolleyes:

 

I re-flashed one ND+2, and now the same program Works!

I can't reproduce the error! :unsure:



#14 Nutz95

Nutz95

    Member

  • Members
  • PipPip
  • 11 posts

Posted 06 December 2013 - 08:28 PM

Do you know if your network interface is configured in DHCP or with static IP once reflashed?



#15 Duefectu

Duefectu

    Advanced Member

  • Members
  • PipPipPip
  • 34 posts

Posted 06 December 2013 - 09:04 PM

I setup network interface to static in every start. I never use DHCP on this devices.



#16 Duefectu

Duefectu

    Advanced Member

  • Members
  • PipPipPip
  • 34 posts

Posted 09 December 2013 - 05:47 PM

Today I take the same error without any change on the code nor ND+2.

 

I changed StreamReader way by FileStream, and it works fine, but with more code.



#17 Nutz95

Nutz95

    Member

  • Members
  • PipPip
  • 11 posts

Posted 09 December 2013 - 07:42 PM

Thank you 'ill give it a look.

So you are saying the StreamReader is buggy but the FileStream is working?



#18 Duefectu

Duefectu

    Advanced Member

  • Members
  • PipPipPip
  • 34 posts

Posted 09 December 2013 - 07:54 PM

Right (or not), StremReader has a bug, at least for me. FileStream seams to work, at least for now!






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.