Hi,
I spent a lot of hours with handle the SD card and I found very interesting problem.
The first test program works so I get "OpenRead - Ok", "OpenReadWrite - Ok" messages (in the appstart sub) and later if the program receive "$Sec*" message it will send a "." char to the soft serial (monitor) port. So everything is OK.
But the second program (if I remove the comments in the Astream_NewData sub) doesn't work.
I get "OpenRead - Nok" and "OpenReadWrite - Nok" (in the appstart sub!) and later I can't save the important messages ("SD write error").
I don't understand ...
I spent a lot of hours with handle the SD card and I found very interesting problem.
The first test program works so I get "OpenRead - Ok", "OpenReadWrite - Ok" messages (in the appstart sub) and later if the program receive "$Sec*" message it will send a "." char to the soft serial (monitor) port. So everything is OK.
B4X:
#Region Project Attributes
#AutoFlushLogs: True
#CheckArrayBounds: True
#StackBufferSize: 300
#End Region
Sub Process_Globals
Public Serial1 As Serial
Private sd As SD
Private astream As AsyncStreams
Private softastream As AsyncStreams
Private pin9 As Pin
Private pin7 As Pin
Private softserial As SoftwareSerial
Private bc As ByteConverter
Private eol() As Byte = Array As Byte(13, 10)
End Sub
Private Sub AppStart
Serial1.Initialize(38400)
astream.Initialize(Serial1.Stream, "Astream_NewData", "Astream_Error")
pin9.Initialize(9, pin9.MODE_OUTPUT)
pin7.Initialize(7, pin7.MODE_OUTPUT)
Dim flash As Int
For flash = 1 To 20
Delay (20)
pin9.DigitalWrite(Not(pin9.Digitalread))
Next
pin9.DigitalWrite(False)
pin7.DigitalWrite(False)
sd.Initialize(4)
softserial.Initialize(38400, 3, 2)
softastream.Initialize(softserial.Stream, "astream_newdata", Null)
softastream.Write(eol)
softastream.Write("Program is Starting v2...".GetBytes)
softastream.Write(eol)
If sd.OpenRead("writelog.txt") = True Then
softastream.Write ("OpenRead - Ok".GetBytes)
Else
softastream.Write ("OpenRead - Nok".GetBytes)
End If
Delay (500)
softastream.Write(eol)
If sd.OpenReadwrite("writelog.txt") = True Then
softastream.Write ("OpenReadWrite - Ok".GetBytes)
Else
softastream.Write ("OpenReadWrite - Nok".GetBytes)
End If
softastream.Write(eol)
End Sub
Sub Astream_NewData (buffer() As Byte)
If buffer = "$Sec*" Then
softastream.Write(".".GetBytes)
pin9.DigitalWrite(Not(pin9.Digitalread))
End If
If buffer.Length = 84 Then
Dim command () As Byte = bc.SubString2(buffer, 1,5)
If command = "Data" Then
Log("command = Data")
softastream.Write(eol)
softastream.Write("Data from sender: ".GetBytes)
softastream.Write(buffer)
softastream.Write(eol)
' If sd.OpenReadwrite("writelog.txt") = True Then
' sd.Position = sd.CurrentFile.Size
' sd.Stream.WriteBytes (buffer, 0, buffer.length)
' sd.Stream.WriteBytes(CRLF, 0, 2)
' sd.close
' softastream.Write ("SD write ok".GetBytes)
' softastream.Write(eol)
' astream.Write ("$SD write ok*".GetBytes)
' Else
' pin7.DigitalWrite(True)
' softastream.Write ("SD write error".GetBytes)
' softastream.Write(eol)
' astream.Write ("$SD write error*".GetBytes)
' End If
End If
End If
End Sub
Sub AStream_Error
Log("error")
End Sub
But the second program (if I remove the comments in the Astream_NewData sub) doesn't work.
I get "OpenRead - Nok" and "OpenReadWrite - Nok" (in the appstart sub!) and later I can't save the important messages ("SD write error").
B4X:
#Region Project Attributes
#AutoFlushLogs: True
#CheckArrayBounds: True
#StackBufferSize: 300
#End Region
Sub Process_Globals
Public Serial1 As Serial
Private sd As SD
Private astream As AsyncStreams
Private softastream As AsyncStreams
Private pin9 As Pin
Private pin7 As Pin
Private softserial As SoftwareSerial
Private bc As ByteConverter
Private eol() As Byte = Array As Byte(13, 10)
End Sub
Private Sub AppStart
Serial1.Initialize(38400)
astream.Initialize(Serial1.Stream, "Astream_NewData", "Astream_Error")
pin9.Initialize(9, pin9.MODE_OUTPUT)
pin7.Initialize(7, pin7.MODE_OUTPUT)
Dim flash As Int
For flash = 1 To 20
Delay (20)
pin9.DigitalWrite(Not(pin9.Digitalread))
Next
pin9.DigitalWrite(False)
pin7.DigitalWrite(False)
sd.Initialize(4)
softserial.Initialize(38400, 3, 2)
softastream.Initialize(softserial.Stream, "astream_newdata", Null)
softastream.Write(eol)
softastream.Write("Program is Starting v2...".GetBytes)
softastream.Write(eol)
If sd.OpenRead("writelog.txt") = True Then
softastream.Write ("OpenRead - Ok".GetBytes)
Else
softastream.Write ("OpenRead - Nok".GetBytes)
End If
Delay (500)
softastream.Write(eol)
If sd.OpenReadwrite("writelog.txt") = True Then
softastream.Write ("OpenReadWrite - Ok".GetBytes)
Else
softastream.Write ("OpenReadWrite - Nok".GetBytes)
End If
softastream.Write(eol)
End Sub
Sub Astream_NewData (buffer() As Byte)
If buffer = "$Sec*" Then
softastream.Write(".".GetBytes)
pin9.DigitalWrite(Not(pin9.Digitalread))
End If
If buffer.Length = 84 Then
Dim command () As Byte = bc.SubString2(buffer, 1,5)
If command = "Data" Then
Log("command = Data")
softastream.Write(eol)
softastream.Write("Data from sender: ".GetBytes)
softastream.Write(buffer)
softastream.Write(eol)
If sd.OpenReadwrite("writelog.txt") = True Then
sd.Position = sd.CurrentFile.Size
sd.Stream.WriteBytes (buffer, 0, buffer.length)
sd.Stream.WriteBytes(CRLF, 0, 2)
sd.close
softastream.Write ("SD write ok".GetBytes)
softastream.Write(eol)
astream.Write ("$SD write ok*".GetBytes)
Else
pin7.DigitalWrite(True)
softastream.Write ("SD write error".GetBytes)
softastream.Write(eol)
astream.Write ("$SD write error*".GetBytes)
End If
End If
End If
End Sub
Sub AStream_Error
Log("error")
End Sub
I don't understand ...