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