B4R Question Write different data to SD card

Mostez

Well-Known Member
Licensed User
Longtime User
Hello,
I want to save data to sd card file 'log.dat' as follows: CardID,UserName,Date_Time(hh:mm:ss,yy/mm/dd)

Date_Time components are public variables from timer sub, CardID and UserNameare sent from asyncstream. savelog sub called from ReadRFID stream.

Thanks


B4X:
Sub DS1320timer_Tick()
    'The current time must be defined in the sub. The global can not be used.
    Dim currenttime As DSTime
    currenttime = DS1302.CurrentTime
 
     Years = currenttime.years
     Months = currenttime.Months
     DayOfMonth = currenttime.DayOfMonth
     Hours = currenttime.Hours
     Minutes = currenttime.Minutes
     Seconds = currenttime.Seconds
     DayOfWeek = currenttime.DayOfWeek
  
End Sub

B4X:
private Sub ReadRFID(Buffer() As Byte)
    Dim tmpBuffer () As Byte
    Dim Str_HexNumber As String
    Dim Card_Number As ULong
    'Log(BC.HexFromBytes(Buffer))
    If BC.ArrayCompare(Buffer,BufferLast) = 0 Then 'compare last read card to recent one
        Return
    End If
  
    tmpBuffer= BC.SubString2(Buffer,3,11) 'get 8 bytes only, not whole 10 card bytes, filing system supports 8.3 format that is enogh to get more than 999 999 999 , get ride of checksum and start byte
    Str_HexNumber = BC.StringFromBytes(tmpBuffer)
    Card_Number = Bit.ParseInt(Str_HexNumber, 16) 'convert hex to decimal string
    Log(Card_Number)
    BC.ArrayCopy(Buffer,BufferLast)                    'copy last read to recent one, for next comparison              
    NoReadingTimerTicks = 0
   Savelog(Card_Number,"Mostez") 'name is hard coded for testing just for now

End Sub

B4X:
private Sub Savelog(Card_Number As ULong,UserName As String)
    Dim Buffer() As Byte
    'how can I load card number, time elements, and string into array
   
    If Sd.OpenReadWrite ("log.dat") = True Then
        Sd.Stream.WriteBytes(Buffer,0,Buffer.Length)
    End If
End Sub
 

Cableguy

Expert
Licensed User
Longtime User
And the question is.....????

(You do not clearly ask a question nor explain your problem)
 
Upvote 0

Mostez

Well-Known Member
Licensed User
Longtime User
Reference to this post, I tried this code and it worked OK.

Thanks

B4X:
private Sub Savelog(CardID As ULong, UserName As String)
    Dim Buffer() As Byte

    If Sd.OpenReadWrite ("log.dat") = True Then
        Sd.Position = Sd.CurrentFile .Size
       
       
        Buffer = BC.StringToBytes(NumberFormat (CardID,10,0))
        Sd.Stream.WriteBytes(Buffer,0,Buffer.Length)
       
        Buffer = BC.StringToBytes(",")
        Sd.Stream.WriteBytes(Buffer,0,Buffer.Length)
       
        Buffer = BC.StringToBytes(UserName)
        Sd.Stream.WriteBytes(Buffer,0,Buffer.Length)
       
        Buffer = BC.StringToBytes(",")
        Sd.Stream.WriteBytes(Buffer,0,Buffer.Length)
       
        Buffer = BC.StringToBytes(NumberFormat(Hours,2,0))
        Sd.Stream.WriteBytes(Buffer,0,Buffer.Length)
       
        Buffer = BC.StringToBytes(":")
        Sd.Stream.WriteBytes(Buffer,0,Buffer.Length)
       
        Buffer = BC.StringToBytes(NumberFormat(Minutes,2,0))
        Sd.Stream.WriteBytes(Buffer,0,Buffer.Length)
       
        Buffer = BC.StringToBytes(":")
        Sd.Stream.WriteBytes(Buffer,0,Buffer.Length)
       
        Buffer = BC.StringToBytes(NumberFormat(Seconds,2,0))
        Sd.Stream.WriteBytes(Buffer,0,Buffer.Length)
       
        Buffer = BC.StringToBytes(NumberFormat(Years,4,0))
        Sd.Stream.WriteBytes(Buffer,0,Buffer.Length)
       
        Buffer = BC.StringToBytes("/")
        Sd.Stream.WriteBytes(Buffer,0,Buffer.Length)

        Buffer = BC.StringToBytes(NumberFormat(Months,2,0))
        Sd.Stream.WriteBytes(Buffer,0,Buffer.Length)

        Buffer = BC.StringToBytes("/")
        Sd.Stream.WriteBytes(Buffer,0,Buffer.Length)
       
        Buffer = BC.StringToBytes(NumberFormat(DayOfMonth,2,0))
        Sd.Stream.WriteBytes(Buffer,0,Buffer.Length)

        Buffer = BC.StringToBytes(TAB)
        Sd.Stream.WriteBytes(Buffer,0,Buffer.Length)
       

        Sd.Close
    End If
End Sub
 
Upvote 0

derez

Expert
Licensed User
Longtime User
I have found that SD with LiquidCristal_I2C do not work together. It creates the file but returns false and writing do not succeed.
 
Upvote 0
Top