Netduino home hardware projects downloads community

Jump to content


Photo

Read problems from SD Card

netduino_plus_2 visual_basic SD

  • Please log in to reply
8 replies to this topic

#1 rockybooth

rockybooth

    Member

  • Members
  • PipPip
  • 16 posts

Posted 12 November 2013 - 06:52 PM

Hi All:

 

I have been having mixed success with SD.   I can usually write to the SD card, and read the contents in my PC.  However, when I execute the code below, it will ALWAYS crash during the read segment (ReadFile).  It always crashed with this being displayed in the debug window: A first chance exception of type 'System.IndexOutOfRangeException' occurred in System.IO.dll

Error in ReadFile; Exception was thrown: System.IndexOutOfRangeException
 
This may happen after 78 lines read, or after as many as 778 reads, or various numbers in between.

 

I am using the latest 4.2 firmware, Visual Studio 2012 Pro, Visual Basic, NetFramework 4.2.

I am using a SanDisk 128MB microSD card.

 

As this fails after different number of reads, perhaps the hardware is bad.  On this assumption I ordered another Netduino Plus 2 which I will try tonight. 

 

I see in the forums that there seem to be quite a bit of problems with the SD card.  If anyone has any suggestion on how to resolve this, please comment!  

 

Thanks .  

 

Imports System.netImports System.Net.SocketsImports System.IOImports System.TextImports System.ThreadingImports System.IO.PortsImports Microsoft.SPOTImports Microsoft.SPOT.HardwareImports Microsoft.SPOT.Net.NetworkInformationImports Microsoft.SPOT.IOImports SecretLabs.NETMF.HardwareImports SecretLabs.NETMF.Hardware.NetduinoImports Microsoft.VisualBasic.ConstantsNamespace CFcardUsage    Public Module Module1        Dim cardPresent As Boolean = True        Sub Main()            ' eject handler suggested by Stephan at http://forums.netduino.com/index.php?/topic/5076-detect-eject-sd-card-in-vbnet/            AddHandler SPOT.IO.RemovableMedia.Eject, AddressOf RemovableMedia_Eject            AddHandler Microsoft.SPOT.IO.RemovableMedia.Insert, AddressOf RemovableMedia_Insert            Debug.Print("Startup CF Card Usage")            ' Define path and filename            Dim strPath As String = "SD" & DateTime.Now.ToString("yyyyMM")            Dim strFileName As String = strPath & "" & DateTime.Now.ToString("yyyyMMdd") & ".log"            ' make sure disk is there and create directory if needed            Try                If Not File.Exists(strFileName) Then ' ** This shows a System.io.ioexception AND the following line executes, also throwing an exception                    If Not Directory.Exists(strPath) Then '** This also shows a System.IO.IOexception in debugger, and the following line executes                        Directory.CreateDirectory(strPath) ' ** the exception appears to be thrown her and goes to the catch                    End If                End If            Catch ex As Exception                Debug.Print("Exception thrown - " & ex.Message)                Debug.Print("Presumably there is no readable card present")                cardPresent = False            End Try            ' if no card is found, wait for one to be inserted            Do While True                If cardPresent Then Exit Do                Debug.Print("insert card")                Threading.Thread.Sleep(1000)            Loop            Debug.Print(strFileName)            ' open the streamwriter            Dim sw As StreamWriter = New StreamWriter(File.OpenWrite(strFileName))            sw.WriteLine("This is to write the files to the netduino card")            'lets learn how fast we can write to these cards            Dim StartTime As DateTime = DateTime.Now            Dim excp As Boolean = False            For i As Integer = 0 To 10000                Dim t As String = DateTime.Now.ToString                Dim EndTime As TimeSpan = DateTime.Now.Subtract(StartTime)                Try  ' if this fails, lets get the error, and stop writing so I can see if prior data is ok                    sw.Write("Memory usage " & Microsoft.SPOT.Debug.GC(False).ToString & "; Elapsed time " & EndTime.Seconds.ToString & "." & EndTime.Milliseconds.ToString)                    sw.WriteLine("; line number " & i.ToString & " at " & t & ", ms counter = " & DateTime.Now.Millisecond.ToString & ", tics= " & DateTime.Now.Ticks.ToString & ", machine time " & Hardware.Utility.GetMachineTime.Ticks.ToString)                Catch ex As Exception                    Debug.Print("Exception " & ex.Message & ", at i=" & i.ToString & ", ET= " & EndTime.ToString)                    ' exit the write loop                    Exit For                End Try                If i Mod 1000 = 0 Then Debug.Print(i.ToString)            Next            ' try everything possible to make sure the writes are complete to the card            sw.Flush()            sw.BaseStream.Flush()            sw.BaseStream.Close()            sw.Dispose()            ' if there was a write error, just exit, otherwise read back the contents            If Not excp Then ReadFile(strFileName)        End Sub        Sub ReadFile(filename As String)            ' this will read and display the contents of the card.            If File.Exists(filename) = False Then                Debug.Print("File does not exist")                Return            End If            Dim sr As StreamReader = New StreamReader(filename)            Dim s As String = ""            Do                Try                    s = sr.ReadLine() ' crashes with index out of range exception (system.io.dll) after some number of reads                Catch ex As Exception ' must use a try/catch or file will be deleted or not found. Does not seen to always work                    Debug.Print("Error in ReadFile; " & ex.Message)                    Exit Do                End Try                Debug.Print("Mem now: " & Microsoft.SPOT.Debug.GC(False).ToString & "; Recorded data: " & s)            Loop Until sr.EndOfStream            Debug.Print("Last line read was: " & s)            sr.Close()            sr.Dispose()        End Sub        Sub RemovableMedia_Eject(sender As Object, e As Microsoft.SPOT.IO.MediaEventArgs)            Debug.Print("Ejected")            cardPresent = False        End Sub        Sub RemovableMedia_Insert(sender As Object, e As Microsoft.SPOT.IO.MediaEventArgs)            Debug.Print("Inserted or present")            cardPresent = True        End Sub    End ModuleEnd Namespace

 



#2 rockybooth

rockybooth

    Member

  • Members
  • PipPip
  • 16 posts

Posted 17 November 2013 - 11:06 PM

Hi:

I reproduced this bug on another Netduino 2+ card with 1GB Sandisk card. 

I would really appreciate if someone could reproduce or not reproduce this issue.  At this point I am wondering if the SD card read is dependable (at least in VB). Alternately if anyone has a test sequence in VB that I could try, I would appreciate it.

Here is some more information on my toolchain:

Thanks

 

Create TS.
 Loading start at 806a988, end 8085288
Assembly: mscorlib (4.2.0.0)
Assembly: Microsoft.SPOT.Native (4.2.0.0)
Assembly: Microsoft.SPOT.Hardware (4.2.0.0)
Assembly: Microsoft.SPOT.Net (4.2.0.0)
Assembly: System (4.2.0.0)
Assembly: Microsoft.SPOT.Hardware.SerialPort (4.2.0.0)
Assembly: Microsoft.SPOT.IO (4.2.0.0)
Assembly: System.IO (4.2.0.0)
Assembly: Microsoft.SPOT.Hardware.PWM (4.2.0.1)
Assembly: Microsoft.SPOT.Hardware.Usb (4.2.0.0)
Assembly: SecretLabs.NETMF.Diagnostics (4.2.0.0)
Assembly: SecretLabs.NETMF.Hardware.Netduino (4.2.1.0)
Assembly: Microsoft.SPOT.Hardware.OneWire (4.2.0.0)
Assembly: Microsoft.SPOT.Time (4.2.0.0)
Loading Deployment Assemblies.
Attaching deployed file.
Assembly: Microsoft.VisualBasic (1.0.0.0)
Attaching deployed file.
Assembly: Microsoft.SPOT.Graphics (4.2.0.0)
Attaching deployed file.
Assembly: SecretLabs.NETMF.IO (4.2.0.0)
Attaching deployed file.
Assembly: CFcardUsage (1.0.0.0)
Attaching deployed file.
Assembly: SecretLabs.NETMF.Hardware.AnalogInput (4.2.2.0)
Attaching deployed file.
Assembly: SecretLabs.NETMF.Hardware (4.2.0.0)
Resolving.
The debugging target runtime is loading the application assemblies and starting execution.
Ready.

 



#3 Duefectu

Duefectu

    Advanced Member

  • Members
  • PipPipPip
  • 34 posts

Posted 06 December 2013 - 12:06 PM

I have the same problema in c#. I open a file with StreamReader, and I can read entire file.

But if the file has more tan 100 lines, the ReadLine method fails with the System.IndexOutOfRangeException.

var sr = new StreamReader(fichero);while (!sr.EndOfStream){	string t = sr.ReadLine();	if (t == itemToSearch) return true;}sr.Close();


#4 rockybooth

rockybooth

    Member

  • Members
  • PipPip
  • 16 posts

Posted 23 December 2013 - 05:16 AM

Hi:

I was wondering if anyone had any suggestions on this, or if I should just consider the SDCard ability unreliable, perhaps until the next OS revision comes along?

Have a great Christmas!



#5 Duefectu

Duefectu

    Advanced Member

  • Members
  • PipPipPip
  • 34 posts

Posted 23 December 2013 - 09:07 AM

I changed StreamReader to FileStream and it Works, but you must to implement the ReadLine method.



#6 rockybooth

rockybooth

    Member

  • Members
  • PipPip
  • 16 posts

Posted 23 December 2013 - 07:57 PM

I do not see a ReadLine method for FileStream.  Perhaps you could post an example.

Thanks



#7 Duefectu

Duefectu

    Advanced Member

  • Members
  • PipPipPip
  • 34 posts

Posted 23 December 2013 - 08:13 PM

As I mentioned, you must implement your own ReadLine method.

 

This is a simple implementation. Not tested, and four sure, not the best implementation.

	public bool SearchText(string fileName, string textToFind)        {		var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);		string line="";		while(line!="ERROR")		{            			line=ReadLine(fs);			if(line==textToFind)			{				fs.Close();				return true;			}                }		fs.Close();                return false;        }	private static string Readline(FileStream fs)        {            try            {                byte[] buffer = new byte[1024];                byte b = 0;                byte r = 0;                int l = 1024;                for (int n = 0; n < 1024; n++)                {                    b = (byte)fs.ReadByte();                    if (b == 13)                    {                        r = (byte)fs.ReadByte();                        if (r == 10)                        {                            l = n;                            break;                        }                        else                        {                            buffer[n] = b;                            n++;                        }                    }                    bufer[n] = b;                }                string line = new string(Encoding.UTF8.GetChars(buffer, 0, l));                return line;            }            catch            {                return "ERROR";            }        }

Hope it helps!



#8 siqueira_jean

siqueira_jean

    New Member

  • Members
  • Pip
  • 6 posts

Posted 06 October 2016 - 11:20 PM

HI. 

 

I work with netduino 3 wi-fi and my configuration is:

Connecting to Netduino3_Netduino...Connected
No debugger!
Create TS.
 Loading start at 8065cac, end 808cd2c
Assembly: mscorlib (4.3.1.0)
Assembly: Microsoft.SPOT.Native (4.3.1.0)
Assembly: Microsoft.SPOT.Hardware (4.3.1.0)
Assembly: Microsoft.SPOT.Net (4.3.1.0)
Assembly: Microsoft.SPOT.Net.Security (4.3.1.0)
Assembly: System (4.3.1.0)
Assembly: System.Net.Security (4.3.1.0)
Assembly: Microsoft.SPOT.Hardware.SerialPort (4.3.1.0)
Assembly: Microsoft.SPOT.IO (4.3.1.0)
Assembly: System.IO (4.3.1.0)
Assembly: Microsoft.SPOT.Hardware.PWM (4.3.1.0)
Assembly: Microsoft.SPOT.Hardware.Usb (4.3.1.0)
Assembly: Netduino.IP (1.0.0.0)
Assembly: Netduino.IP.Interop (1.0.0.0)
Assembly: Netduino.IP.LinkLayers.CC3100 (1.0.0.0)
Assembly: SecretLabs.NETMF.Diagnostics (4.3.1.0)
Assembly: GoBus (1.5.0.0)
Assembly: SecretLabs.NETMF.Hardware.Netduino (4.3.1.0)
Assembly: Microsoft.SPOT.Hardware.OneWire (4.3.1.0)
Loading Deployment Assemblies.
Attaching deployed file.
Assembly: SecretLabs.NETMF.Hardware.NetduinoPlus (4.3.1.0)
Attaching deployed file.
Assembly: Microsoft.SPOT.Wireless.IEEE_802_15_4.Phy (4.3.1.0)
Attaching deployed file.
Assembly: System.Http (4.3.1.0)
Attaching deployed file.
Assembly: SecretLabs.NETMF.IO (4.3.1.0)
Attaching deployed file.
Assembly: Microsoft.SPOT.Wireless.IEEE_802_15_4.Mac (4.3.1.0)
Attaching deployed file.
Assembly: MFW.ModemRFIDNetduino3Internet.net (1.0.0.0)
Resolving.
 
 
The problem is only my sd card. The SdCard only works if the moment I connect i Netduino 3 on power also insert the sdcard. Otherwise it does not work.


#9 siqueira_jean

siqueira_jean

    New Member

  • Members
  • Pip
  • 6 posts

Posted 06 October 2016 - 11:35 PM

The error in Visual Studio 2013:
 
A first chance exception of type 'System.NotSupportedException' occurred in SecretLabs.NETMF.IO.dll
The thread '<No Name>' (0x4) has exited with code 0 (0x0).
System.NotSupportedException






Also tagged with one or more of these keywords: netduino_plus_2, visual_basic, SD

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.