KarlB
Member
I am writing a data logger that appends a CSV string to a file every second. I must be able to open the file with Excel either on my cell phone or by sending it to my PC. Note that my phone is a Samsung S21 that does not support an SD card.
I have spent the last two days reading every forum and tutorial without gaining a single clue as to how to do this. I see criticism of using TextWriter but there is a comment by Erel saying that it is justified when appending lines to files. The TextEditor example works well but is far beyond my comprehension to extract the portion of the code that does what I want.
Here is what the data looks like:
2022-09-29 19-19-27,38.83687973022461,-77.47976684570312,39,0.20999999344348907,18.317604064941406,149.45,24.64,-0.17,2,-0.00,-0.06,24.66,0.400
Here is a snippet to save the data. This runs but I can't see the saved file. This comes from https://www.b4x.com/android/forum/threads/text-files.6690/
Sub getBTfullData
Private logData As String ' CSV from ESP32
Private payLoad() As String
txwBToutput.Write("D") ' request data from monitor
txwBToutput.Flush
Do While txrBTinput.Ready = False
Loop
logData = txrBTinput.ReadLine ' read a CSV string from BT
Log("Full Rcvd, " & logData)
payLoad = Regex.Split( ",", logData ) ' parse the CSV string into a field list
Private dataList As List
dataList.Initialize2( payLoad )
recID = dataList.Get(0)
Vbat = dataList.Get(1)
Ibat = dataList.Get(2)
Wbat = dataList.Get(3)
AH = dataList.Get(4)
WH = dataList.Get(5)
Vint = dataList.Get(6)
ESR = dataList.Get(7)
updateDataDisplay( dataList )
If runFlag = True Then
saveFile(logData)
End If
End Sub
Sub WriteTextWriter(fileName As String, data As String )
Dim TextWriter1 As TextWriter
Log("Write: " & data)
TextWriter1.Initialize(File.OpenOutput(File.DirInternal, fileName, True))
TextWriter1.WriteLine(data)
TextWriter1.Close
End Sub
Sub saveFile(data As String)
DateTime.DateFormat = "yyyy-MM-dd HH-mm-ss"
Dim str As String = ""
str = DateTime.Date( DateTime.Now ) & "," & latitude & "," & longitude & "," & altitude & "," & speed & "," & distance & "," & data & CRLF
WriteTextWriter(logFileName, str)
End Sub
I have spent the last two days reading every forum and tutorial without gaining a single clue as to how to do this. I see criticism of using TextWriter but there is a comment by Erel saying that it is justified when appending lines to files. The TextEditor example works well but is far beyond my comprehension to extract the portion of the code that does what I want.
Here is what the data looks like:
2022-09-29 19-19-27,38.83687973022461,-77.47976684570312,39,0.20999999344348907,18.317604064941406,149.45,24.64,-0.17,2,-0.00,-0.06,24.66,0.400
Here is a snippet to save the data. This runs but I can't see the saved file. This comes from https://www.b4x.com/android/forum/threads/text-files.6690/
Sub getBTfullData
Private logData As String ' CSV from ESP32
Private payLoad() As String
txwBToutput.Write("D") ' request data from monitor
txwBToutput.Flush
Do While txrBTinput.Ready = False
Loop
logData = txrBTinput.ReadLine ' read a CSV string from BT
Log("Full Rcvd, " & logData)
payLoad = Regex.Split( ",", logData ) ' parse the CSV string into a field list
Private dataList As List
dataList.Initialize2( payLoad )
recID = dataList.Get(0)
Vbat = dataList.Get(1)
Ibat = dataList.Get(2)
Wbat = dataList.Get(3)
AH = dataList.Get(4)
WH = dataList.Get(5)
Vint = dataList.Get(6)
ESR = dataList.Get(7)
updateDataDisplay( dataList )
If runFlag = True Then
saveFile(logData)
End If
End Sub
Sub WriteTextWriter(fileName As String, data As String )
Dim TextWriter1 As TextWriter
Log("Write: " & data)
TextWriter1.Initialize(File.OpenOutput(File.DirInternal, fileName, True))
TextWriter1.WriteLine(data)
TextWriter1.Close
End Sub
Sub saveFile(data As String)
DateTime.DateFormat = "yyyy-MM-dd HH-mm-ss"
Dim str As String = ""
str = DateTime.Date( DateTime.Now ) & "," & latitude & "," & longitude & "," & altitude & "," & speed & "," & distance & "," & data & CRLF
WriteTextWriter(logFileName, str)
End Sub