B4R Question ESP32Cam How to get inline C SD card read file data?

tzfpg

Active Member
Licensed User
Longtime User
Hi,

I am using esp32cam to read data (inline c) from SD card and pass to main.
But I failed to get the inline C data. How to get the data from inline c

Below is my source code
B4X:
#Region Project Attributes
    #AutoFlushLogs: True
    #CheckArrayBounds: True
    #StackBufferSize: 1000
#End Region

Sub Process_Globals
    Public Serial1 As Serial
    Public bc As ByteConverter
    Public resultbyte() As Byte
End Sub

Private Sub AppStart
    Serial1.Initialize(9600)
    SD.Init
    Log("AppStart")
    SD.writeFile("/testing.txt","01")
    SD.readFile("/testing.txt")
End Sub

SD module:
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
    Type myArgs(arg1() As Byte, arg2() As Byte, arg3() As Byte,arg4() As Byte)
   
End Sub

Public Sub Init
    RunNative("init", Null)
End Sub

Public Sub readFile(path As String)
    Dim b() As Byte=path.GetBytes
    Main.done_status=0
    RunNative("readFile", b)
End Sub

Public Sub writeFile(path As String,messages As String)
    Dim arg As myArgs
    arg.arg1=path.GetBytes
    arg.arg2=messages.GetBytes
    RunNative("writeFile", arg)
End Sub

#if c
#include "FS.h"                // SD Card ESP32
#include "SD_MMC.h"            // SD Card ESP32

void init(B4R::Object* o) {
    Serial.println("Starting SD Card");
    if(!SD_MMC.begin()){
      ::Serial.println("SD Card Mount Failed");
      return;
    }
   
     uint8_t cardType = SD_MMC.cardType();
     if(cardType == CARD_NONE){
       ::Serial.println("No SD Card attached");
       return;
     }
   
     Serial.print("SD_MMC Card Type: ");
     if(cardType == CARD_MMC){
        Serial.println("MMC");
     } else if(cardType == CARD_SD){
        Serial.println("SDSC");
     } else if(cardType == CARD_SDHC){
        Serial.println("SDHC");
     } else {
        Serial.println("UNKNOWN");
     }
   
     uint64_t cardSize = SD_MMC.cardSize() / (1024 * 1024);
     Serial.printf("SD_MMC Card Size: %lluMB\n", cardSize);
   
}





void readFile(B4R::Object* o){
    fs::FS &fs = SD_MMC;
    B4R::Array* b = (B4R::Array*)B4R::Object::toPointer(o);
    char* path = (char*)b->data;
    ::Serial.printf("Reading file: %s\n", path);
   
    File file = fs.open(path);
    if(!file){
        Serial.println("Failed to open file for reading");
        return;
    }

    Serial.print("Read from file: ");
    while(file.available()){
        ::Serial.print("Reading data: ");
        ::Serial.println(file.read());
    }
}

void writeFile(B4R::Object* o){
    fs::FS &fs = SD_MMC;
    _myargs* tmp = (_myargs*)B4R::Object::toPointer(o);
   
    B4R::Array* b1 = tmp->arg1;
    char* path = (char*)b1->data;
    ::Serial.printf("Writing file: %s\n", path);

    B4R::Array* b2 = tmp->arg2;
    char* message = (char*)b2->data;
      ::Serial.printf("message: %s\n", message);

    File file = fs.open(path, FILE_WRITE);
    if(!file){
        ::Serial.println("Failed to open file for writing");
        return;
    }
 
    if(file.println(message)){
        ::Serial.println("File written");
    } else {
        ::Serial.println("Write failed");
    }
   
}
#End If

log:
B4X:
Starting SD Card
SD_MMC Card Type: SDHC
SD_MMC Card Size: 30436MB
AppStart
Writing file: /testing.txt
message: 01
File written
Reading file: /testing.txt
Read from file: Reading data: 48
Reading data: 49
Reading data: 13
Reading data: 10

I want to collect form inline C 48 49 13 10 into resultbyte, how to do that?
 
Last edited:

tzfpg

Active Member
Licensed User
Longtime User
You asked the same question feb. 16 2019, a lot of people helped you and you got it working.
https://www.b4x.com/android/forum/threads/return-byte-array-from-inline-c.102729/#post-644517

i'm sure know, but this case I can't get the data like old thread, that why I
You asked the same question feb. 16 2019, a lot of people helped you and you got it working.
https://www.b4x.com/android/forum/threads/return-byte-array-from-inline-c.102729/#post-644517

ask again.
 
Upvote 0

tzfpg

Active Member
Licensed User
Longtime User
HI,

I try to add this
B4X:
b4r_main::_resultbyte->data = file.read();
into read file but I get this error

B4X:
void readFile(B4R::Object* o){
    fs::FS &fs = SD_MMC;
    B4R::Array* b = (B4R::Array*)B4R::Object::toPointer(o);
    char* path = (char*)b->data;
    ::Serial.printf("Reading file: %s\n", path);
    
    File file = fs.open(path);
    if(!file){
        Serial.println("Failed to open file for reading");
        return;
    }
    
    Serial.print("Read from file: ");
    while(file.available()){
        ::Serial.print("Reading data: ");
        ::Serial.println(file.read());
        b4r_main::_resultbyte->data = file.read();
    }

}

Error
B4X:
b4r_sd.cpp:82:42: error: invalid conversion from 'int' to 'void*' [-fpermissive]
b4r_main::_resultbyte->data = file.read();
 
Upvote 0
Top