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 File corrupted


  • Please log in to reply
3 replies to this topic

#1 Novice Alex

Novice Alex

    Advanced Member

  • Members
  • PipPipPip
  • 59 posts

Posted 24 September 2012 - 02:56 PM

Hi Guys,

I have done up a commercial project and now doing the "burn test".
This project suppose to do data logging of the fuel consumption from different flow meters on a 5 mins interval.
However, I found that randomly the log file could be corrupted and it will cause the whole N+ to hang when accessing it.
I have tried to access the SD card using my PC and can see that that particular "problem" file exists but I am not able to access it and I can't delete it from my PC. So the only way is to reformat my SD card and start fresh. This gives me some sense of "uncertainty".

Does anyone know how can we handle such cases to avoid the N+ to hang? By the way, when I say hang, it means not even when I short the reset pin to ground or press the reset can restart N+, the only way is to power it down completely.

Below is my SD card class library.
Imports System.IO
Imports SecretLabs.NETMF.Hardware.Netduino
Imports SecretLabs.NETMF.Hardware
Imports Microsoft.SPOT


Public Class cSDCard
#Region "Enumeration"

#End Region

#Region "Declaration"
    Public Shared Event LastError(ex As Exception)
    Public Shared Event SDEject()
    Private Shared m_bSDEjected As Boolean = True
#End Region

#Region "Property"
    Public Shared Property SD_Ejected() As Boolean
        Get
            Return m_bSDEjected
        End Get
        Set(value As Boolean)
            m_bSDEjected = value
        End Set
    End Property

#End Region

#Region "Method"
    Public Shared Function MountSD() As Boolean
        MountSD = False
        SD_Ejected = True
        Try
            SecretLabs.NETMF.IO.StorageDevice.MountSD("SD", Microsoft.SPOT.Hardware.SPI.SPI_module.SPI2, 21, 57)
            Dim dirinfo As New DirectoryInfo("\SD")
            If (dirinfo.Exists) Then
                MountSD = True
                SD_Ejected = False
            End If
        Catch ex As Exception
            SD_Ejected = True
        End Try
    End Function

    Public Shared Function UnMountSD() As Boolean
        Try
            If SD_Ejected = True Then
                Exit Function
            End If

            SecretLabs.NETMF.IO.StorageDevice.Unmount("SD")
        Catch ex As Exception

        End Try
    End Function

    Public Shared Function ReadFile(sFilename As String, ByRef sContent As String, Optional bReadLastLineOnly As Boolean = False) As Boolean
        ReadFile = False
        Try
            Dim lFileSize As Long = 0
            Dim sTmp As String = ""
            sContent = ""
            If SD_Ejected = True Then
                Exit Function
            End If

            If File.Exists(sFilename) Then
                Dim myFile As New StreamReader(New FileStream(sFilename, FileMode.Open, FileAccess.Read))
                If bReadLastLineOnly = True Then
                    While (1)
                        sTmp = myFile.ReadLine
                        If Not sTmp Is Nothing Then
                            sContent = sTmp
                        Else
                            Exit While
                        End If
                    End While
                Else
                    sContent = myFile.ReadToEnd
                End If
                myFile.Close()
            Else
                Debug.Print(sFilename & " not found!!!")
            End If

            ReadFile = True
        Catch ex As Exception
            RaiseEvent LastError(ex)
        End Try
    End Function

    Public Shared Function WriteFile(sFilename As String, sContent As String, Optional bAppend As Boolean = True) As Boolean

        WriteFile = False
        Try

            If SD_Ejected = True Then
                Exit Function
            End If

            Dim myFile As StreamWriter

            If bAppend = True Then
                myFile = New StreamWriter(New FileStream(sFilename, FileMode.OpenOrCreate, FileAccess.Write))
            Else
                DeleteFile(sFilename)
                myFile = New StreamWriter(New FileStream(sFilename, FileMode.CreateNew, FileAccess.Write))
            End If
            myFile.WriteLine(sContent)
            myFile.Close()

            WriteFile = True
        Catch ex As Exception
            RaiseEvent LastError(ex)
        End Try
    End Function

    Public Shared Function DeleteFile(sFilename As String) As Boolean
        DeleteFile = False
        Try

            If SD_Ejected = True Then
                Exit Function
            End If

            If File.Exists(sFilename) Then
                File.Delete(sFilename)
            End If
            DeleteFile = True
        Catch ex As Exception
            RaiseEvent LastError(ex)
        End Try
    End Function
#End Region
End Class

Thanks in advance.

Regards,
Alex Chan

#2 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 24 September 2012 - 05:25 PM

Hi Alex, Can you tell when the file is getting corrupted? Is it because of a power loss? Does it only happen with a certain type of card? I've never seen data corruption before, except in cases of power loss. I'm guessing that the SD card driver is having troubles accessing the card. If we can reproduce the situation we can debug into the SD drivers and figure out what's happening. BTW, .NET MF 4.2 uses a system-level-priority interrupt to handle the pushbutton and RESET handler. So if those aren't working then something is getting locked up pretty hard. Chris

#3 Novice Alex

Novice Alex

    Advanced Member

  • Members
  • PipPipPip
  • 59 posts

Posted 24 September 2012 - 11:29 PM

Hi Chris, I remember the SD card corruption happens after around 7 days of "burn test". I have been trying to do "hot swap" for newly design PCB to see any effect on the system reliability. Then it starts to hang the whole N+. After putting in a few debug statement, it came to know that it was hang during the writing to a data text file. I have tried to do file deleting on the SD card on my PC, but it still exists after I do a screen refresh. So, no choice but to reformat it. I suppose it can be caused by the loss of power during the writing (while doing the "hot swap"), but how can we recover from there if such incident happens again, given that this system will be deployed to remote sites out in the sea, so it will be quite trouble to access. From my attached class, is there any potential problem that you can foresee? Thanks in advance. Regards, Novice Alex

#4 Mariano Rodriguez

Mariano Rodriguez

    New Member

  • Members
  • Pip
  • 1 posts

Posted 19 January 2013 - 01:09 AM

Me too. The whole N+ to hang when accessing corrupted files.

 

  Private Sub DeleteJPGFiles(ByVal path As String)   Try   Dim Registros = Directory.EnumerateFiles(path)   For Each Registro As String In Registros   If (Registro.Substring(path.Length + 1)).Split(".")(1) = "jpg" Then   System.IO.File.Delete(Registro.ToString)   End If   Next   Catch ex As Exception   Debug.Print("DeleteJPGFiles: " & ex.Message)   End Try   End Sub






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.