B4R Question SD open next file

Mostez

Well-Known Member
Licensed User
Longtime User
Hello,
I want to read files inside folder one after one and send data over USB. With rSD list files function i get list of files and read them one by one but if number of files are more than 50 or so MEGA starts over. I read this article about SD open next file and SD rewind directory and I want to know how to call it from inline C, or if it is easy to modify recent rSD library(sorry but I have no C experience).

Thanks
 

Peter Simpson

Expert
Licensed User
Longtime User
Can you post the code that you are using, the Mega should'nt be starting over.

Have you read this posy by Erel, manipulating Inline C is extremely simple to do, even with little to no experience in C, just play about with the code until you get it working and most importantly learn from it.

Inline C tutorial
https://www.b4x.com/android/forum/threads/inline-c-c.65714/#content

I use code something like this via an ESP8266, it can easily be modified to loop through and read every single file on an SD card...
B4X:
    Dim SDCard As SD
    SDCard.Initialize(4) 'Pins 4 and 8 work (maybe also 10 on other devices)
    For Each F As File In SDCard.ListFiles("/")
        Log("Name: ", F.Name, ", Size: ", F.Size)
    Next
  
    SDCard.OpenReadWrite("SOLIHU~1.TXT")
    Dim Buffer(SDCard.CurrentFile.Size) As Byte
    SDCard.Stream.ReadBytes(Buffer, 0, SDCard.CurrentFile.Size)
    SDCard.Close
 
Upvote 0

Mostez

Well-Known Member
Licensed User
Longtime User
Thanks for reply, yes I read Erel's post, but I don't know how to integrate inline-C SD object with used SD object in B4R code? here is the code I use.

B4X:
private Sub UploadAllUserAccounts(SendFileInfo As Boolean,StreamSource As AsyncStreams) As Boolean
    Dim Size As ULong = 0
    SetSD(ON)
    LCD.Clear
    PutString(0,1,"Please Wait...",True)
    If SendFileInfo Then
        For Each Fl As File In SD.ListFiles("dat") ' get total file size in folder
            Size = Size + 66 + 2 'only first four fields
        Next
        'send file info only for once, upload user files as stream of ONE united file
        Dim Tmp(18) As Byte
        Dim RAF As RandomAccessFile
        
        RAF.Initialize(Tmp,False)
        RAF.WriteByte(0x02,RAF.CurrentPosition)
        RAF.WriteBytes("USERS.DB".GetBytes,0,8,RAF.CurrentPosition)
        RAF.WriteByte(SYS_DATA_FILES_DELIMITION_CHR,RAF.CurrentPosition)
        RAF.WriteBytes(NumberFormat(Size,8,0).GetBytes,0,8,RAF.CurrentPosition) 'file size
        SetSD(OFF)
        SendStreamMessage(Tmp,Tmp.Length,True,StreamSource)
        SetSD(ON)
        
    End If
    
    'Dim cb(101) As Byte
    Dim cb(66) As Byte
    For Each f As File In SD.ListFiles("dat")
        If SD.OpenRead(GetUserAccountFileName(f.Name)) = False  Then
            LCD.Clear
            Return False
        End If
        'SD.Stream.ReadBytes(cb,0,101) 'read 101 bytes
        SD.Stream.ReadBytes(cb,0,66)
        SD.Stream.Flush
        Delay(10)
        SD.close
        SetSD (OFF)
        SendStreamMessage(cb,cb.Length,True,StreamSource)
        SetSD (ON)
    Next
    LCD.Clear
    SetSD (OFF)
    SendStreamMessage(Array As Byte (0x04),1,True,StreamSource)'end of transmission
    'SetSD (True)
    Return True
    
End Sub
B4X:
private Sub SendStreamMessage(Message() As Byte,BytestoSend As Byte,SendCRLF As Boolean,StreamSource As AsyncStreams)
    If SendCRLF Then
        Dim ForSend(BytestoSend + 2) As Byte
        ForSend(BytestoSend) = 0x0D
        ForSend(BytestoSend + 1) = 0x0A
    Else
        Dim ForSend(BytestoSend) As Byte
    End If
    
    BC.ArrayCopy2(Message,0,ForSend,0,BytestoSend)
    StreamSource.Write(ForSend)
End Sub
 
Upvote 0

Mostez

Well-Known Member
Licensed User
Longtime User
Yes Erel, it worked OK with 75 files in folder
EDIT: i increased number of files to 225, listed OK
 
Last edited:
Upvote 0
Top