Netduino home hardware projects downloads community

Jump to content

The Netduino forums have been replaced by new forums at This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.


Member Since 03 Nov 2013
Offline Last Active Apr 30 2015 06:02 PM

#54047 Read problems from SD Card

Posted by rockybooth on 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            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 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 ( 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


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.