B4R Question SD open next file

Discussion in 'B4R Questions' started by Mostez, Jul 16, 2018.

  1. Mostez

    Mostez Active Member

    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
     
  2. Peter Simpson

    Peter Simpson Expert Licensed 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...
    Code:
    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
     
  3. Mostez

    Mostez Active Member

    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.

    Code:
    private Sub UploadAllUserAccounts(SendFileInfo As Boolean,StreamSource As AsyncStreamsAs 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(18As 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(66As 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
    Code:
    private Sub SendStreamMessage(Message() As Byte,BytestoSend As Byte,SendCRLF As Boolean,StreamSource As AsyncStreams)
        
    If SendCRLF Then
            
    Dim ForSend(BytestoSend + 2As 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
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    Inline C will not help here.

    Does this code work:
    Code:
    For Each F As File In SDCard.ListFiles("/")
            
    Log("Name: ", F.Name, ", Size: ", F.Size)
        
    Next
    ?
     
  5. Mostez

    Mostez Active Member

    Yes Erel, it worked OK with 75 files in folder
    EDIT: i increased number of files to 225, listed OK
     
    Last edited: Jul 16, 2018
  6. Erel

    Erel Administrator Staff Member Licensed User

    To answer your question from the first post, using inline C will not help at all. There is no problem with iterating over hundreds of files. The problem is in the code that handles each file. For further discussion please start a new thread and post the relevant code.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice