Dear friends,
How to delete an existing file on SD card? I was unable to find a method to do the same.
How to delete an existing file on SD card? I was unable to find a method to do the same.
RunNative("fileremove",filename)
#if C
#include <SD.h>
void fileremove(B4R::Object* o) {
SD.remove(b4r_main::_filename);
}
#End if
AppStart
Connected: 1
Connecting to NIST server...
ets Jan 8 2013,rst cause:4, boot mode:(3,6)
wdt reset
load 0x4010f000, len 1264, room 16
tail 0
chksum 0x0f
csum 0x0f
~ld
Log("Remove: ", sd.Remove("/ISSUE.TXT"))
Dear Erel,
Even though I could get away with the problem, it is important to investigate as to why ESP8266 crashes because of the specific command. Please do so whenever you get time. We want to make sure our B4X is as bug free as possible !
Thanks Erel,I'm waiting for a new SD card shield to arrive. Will test it then.
#Region Project Attributes
#AutoFlushLogs: True
#CheckArrayBounds: True
#StackBufferSize: 300
#End Region
Sub Process_Globals
'These global variables will be declared once when the application starts.
'Public variables can be accessed from all modules.
Public Serial1 As Serial
Dim SD As SD
Dim GPIO4 = 4 As Int
Dim append = True As Boolean
Dim overwrite = False As Boolean
Dim BC As ByteConverter
End Sub
Private Sub AppStart
Serial1.Initialize(115200)
Log("AppStart")
SD.Initialize(GPIO4)
Log("SD initialised")
Log("Test SDwrite..................")
For a = 1 To 5
Log("---------------- test ", a, " ------------------------")
SDlist
Log("Writing to file")
SDwrite("NEW1.TXT", "testdata",append)
Next
Log("Test App finnishes ...")
End Sub
Sub SDlist
Log("SDlist here")
Dim a As Int
a= a+1
For Each f As File In SD.ListFiles("/")
Log(a, " Name: ", f.Name, ", Size: ", f.Size)
Next
End Sub
Sub SDwrite(fname As String, data As String, Append_flag As Boolean)
Dim buffer() As Byte
Log("SDwrite here")
Log(" - file - ", fname)
Log(" - data=", data)
Log(" - Append = ", Append_flag)
Dim OK As Boolean
Log(" - Test exists file ")
' -----------------------------------------------
' The following line causes NodeMCU to crash !!!!
'''''' OK = SD.Exists(fname)
'''''' report(OK)
'now open the file, append a line of text and then close it ..
OK = SD.OpenReadWrite(fname)
report(OK)
If Append_flag Then SD.Position = SD.CurrentFile .Size Else SD.Position=0
buffer = BC.StringToBytes(data)
SD.Stream.WriteBytes(buffer,0,buffer.Length)
buffer = BC.StringToBytes(CRLF)
SD.Stream.WriteBytes(buffer,0,buffer.Length)
Log("closing file")
SD.Close
End Sub
Sub report(value As Boolean)
If value Then Log("REPORT:Success") Else Log("REPORT:FAILED!")
End Sub
It is not related."C:\android\B4R_PR~1\TEst1\Objects\bin\sketch\b4r_main.cpp:57:12: warning: unused variable 'cp' [-Wunused-variable] const UInt cp = B4R::StackMemory::cp;"
#Region Project Attributes
#AutoFlushLogs: True
#CheckArrayBounds: True
#StackBufferSize: 300
#End Region
Sub Process_Globals
'These global variables will be declared once when the application starts.
'Public variables can be accessed from all modules.
Public Serial1 As Serial
Dim SD As SD
Dim GPIO4 = 4 As Int
Dim FoundInList As Boolean
End Sub
Private Sub AppStart
Serial1.Initialize(115200)
Log("AppStart")
FoundInList=False
If SD.Initialize(GPIO4) Then
Log("SD initialised")
Else
Log("SD failed")
End If
FoundInList = SDlist("NEW1.TXT")
Log("My foundinlist = ", FoundInList)
Log("test sd.exists")
If SD.Exists("NEW1.TXT") Then
Log("file exists")
Else
Log("file not found")
End If
Log("Test App finnishes ...")
End Sub
Sub SDlist(name As String) As Boolean
Log("SDlist start ----------------------")
Dim a As Int
Dim R=False As Boolean
For Each f As File In SD.ListFiles("/")
a= a+1
Log(a, " Name: ", f.Name, ", Size: ", f.Size)
If f.Name = name Then R = True
Next
Log("SDlist end ----------------------")
Return R
End Sub
#Region Project Attributes
#AutoFlushLogs: True
#CheckArrayBounds: True
#StackBufferSize: 300
#End Region
Sub Process_Globals
'These global variables will be declared once when the application starts.
'Public variables can be accessed from all modules.
Public Serial1 As Serial
Dim SD As SD
Dim GPIO4 = 4 As Int
Dim BC As ByteConverter
Dim LF() As Byte = Array As Byte(10)
Dim CR() As Byte = Array As Byte(13)
End Sub
Private Sub AppStart
Dim test As Int
Dim fname="JUNK.TXT" As String
Serial1.Initialize(115200)
Log("AppStart")
SD.Initialize(GPIO4)
Log("SD initialised")
Log("[1] No file added yet")
SDlist(fname)
Log("[2] Adding file junk.txt")
test = SDwrite(fname, "testdata-1",True)
test = SDwrite(fname, "testdata-2",True)
test = SDwrite(fname, "testdata-3",True)
SDlist(fname)
Log("[3] deleteing :", fname)
'SD.Remove(fname)
SDlist(fname)
Log("Test App finnishes ...")
End Sub
Sub SDlist(name As String)
Log("SDlist start ----------------------")
Dim a As Int
Dim str As String
For Each f As File In SD.ListFiles("/")
a= a+1
str = ""
If f.Name = name Then str = " <- found!"
Log(a, " Name: ", f.Name, ", Size: ", f.Size, str)
Next
Log("SDlist end ----------------------")
End Sub
Sub SDwrite(fname As String, data As String, Append As Boolean) As Int
Dim buffer() As Byte
Dim result As Int
Dim OK As Boolean
Dim N As Int
result = 0
OK = SD.OpenReadWrite(fname)
If Append Then SD.Position = SD.CurrentFile .Size Else SD.Position=0
buffer = BC.StringToBytes(data)
n=SD.Stream.WriteBytes(buffer,0,buffer.Length)
If n <> data.Length Then result = 2
buffer = BC.StringToBytes(CRLF)
n=SD.Stream.WriteBytes(buffer,0,buffer.Length)
If n <> data.Length Then result = 3
SD.Close
Return result
End Sub
I think that I know why it happens. It is related to a bug fixed in the standard SDK which wasn't fixed in the ESP8266 version: https://github.com/arduino/Arduino/issues/3999
Please copy the attached rSD.cpp file to rSD folder and test it (overwrite the existing file).
'arduino IDE
String FILENAME = "test.txt";
SD.exists(FILENAME);
'arduino IDE
SD.exists("test.txt");