How to copy data from long "String" variable to USB Pendrive

Hi all,

Striking hard for this scenario, if possible please direct me.
I'm collecting few sensor data and storing them in SD. the current format is,

28/03/2024 23:34:54 10.01 4.81 5.08 10.13 3.33 5.84 2.00 -0.38 4.59 1.45 -0.74 4.49 -3.85 PASS FAIL FAIL PASS FAIL FAIL ;
28/03/2024 23:35:03 10.01 4.81 5.08 10.13 3.33 5.84 2.00 -0.35 4.63 1.57 -0.64 4.57 -3.62 PASS FAIL FAIL PASS FAIL FAIL ;

likewise, Storing 500+ lines in a .txt file. Now, wanna transfer those data weekly once in to a pendrive.
So far I tried, I can copy 2 rows into a single Char Array and save them in pendrive. When i try to copy all, I can copy all 500 lines in to a single "String" but unable to copy / convert it to char array as the buffer limitation.
Can you please suggest a way to make the string to split and send as char array (Thats how pendrive takes the data) to pendrive though a loop may be... so that all 500 lines can be copied?

(NOTE: We can go with any format/delimiters for the texts in SD).

Current Main code:
//in gloabal declaration


char q;
String we;
char adat2[200] = "";


//in the loop
	if (B2_Val == LOW) {
    DateTime now = rtc.now();      
    sprintf(Disp_Date_Char,"%02u/%02u/%04u",now.day(),now.month(),now.year());
    appendFile(SD, "/hello.txt", Disp_Date_Char);
    appendFile(SD, "/hello.txt", " ");
    sprintf(Disp_Time_Char,"%02u:%02u:%02u",now.hour(),now.minute(),now.second());
    appendFile(SD, "/hello.txt", Disp_Time_Char);
    appendFile(SD, "/hello.txt", " ");
    appendFile(SD, "/hello.txt", Act1);
    appendFile(SD, "/hello.txt", " ");
    appendFile(SD, "/hello.txt", Act2);
    appendFile(SD, "/hello.txt", " ");
    appendFile(SD, "/hello.txt", Act3);
    appendFile(SD, "/hello.txt", " ");
    appendFile(SD, "/hello.txt", Act4);
    appendFile(SD, "/hello.txt", " ");
    appendFile(SD, "/hello.txt", Act5);
    appendFile(SD, "/hello.txt", " ");
    appendFile(SD, "/hello.txt", Act6);
    appendFile(SD, "/hello.txt", " ");
    appendFile(SD, "/hello.txt", Toler);
    appendFile(SD, "/hello.txt", " "); 
    appendFile(SD, "/hello.txt", Devi1);
    appendFile(SD, "/hello.txt", " ");
    appendFile(SD, "/hello.txt", Devi2);
    appendFile(SD, "/hello.txt", " ");
    appendFile(SD, "/hello.txt", Devi3);
    appendFile(SD, "/hello.txt", " ");
    appendFile(SD, "/hello.txt", Devi4);
    appendFile(SD, "/hello.txt", " ");
    appendFile(SD, "/hello.txt", Devi5);
    appendFile(SD, "/hello.txt", " ");
    appendFile(SD, "/hello.txt", Devi6);
    appendFile(SD, "/hello.txt", " ");
    Stat_Str1.toCharArray(Stat1_Char, Stat_Str1.length() + 1);
    appendFile(SD, "/hello.txt", Stat1_Char);
    appendFile(SD, "/hello.txt", " ");
    Stat_Str2.toCharArray(Stat2_Char, Stat_Str2.length() + 1);
    appendFile(SD, "/hello.txt", Stat2_Char);
    appendFile(SD, "/hello.txt", " ");
    Stat_Str3.toCharArray(Stat3_Char, Stat_Str3.length() + 1);
    appendFile(SD, "/hello.txt", Stat3_Char);
    appendFile(SD, "/hello.txt", " ");
    Stat_Str4.toCharArray(Stat4_Char, Stat_Str4.length() + 1);
    appendFile(SD, "/hello.txt", Stat4_Char);
    appendFile(SD, "/hello.txt", " ");
    Stat_Str5.toCharArray(Stat5_Char, Stat_Str5.length() + 1);
    appendFile(SD, "/hello.txt", Stat5_Char);
    appendFile(SD, "/hello.txt", " ");
    Stat_Str6.toCharArray(Stat6_Char, Stat_Str6.length() + 1);
    appendFile(SD, "/hello.txt", Stat6_Char);
    appendFile(SD, "/hello.txt", " ");
    appendFile(SD, "/hello.txt", ";");
    appendFile(SD, "/hello.txt", "\n");
    readFile(SD, "/hello.txt");
    Serial.println(we);
	}
	
	
	
// outside loop

void appendFile(fs::FS &fs, const char *path, const char *message) {
  Serial.printf("Appending to file: %s\n", path);

  File file = fs.open(path, FILE_APPEND);
  if (!file) {
    Serial.println("Failed to open file for appending");
    return;
  }
  if (file.print(message)) {
    Serial.println("Message appended");
  } else {
    Serial.println("Append failed");
  }
  file.close();
}


void readFile(fs::FS &fs, const char *path) {
  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: ");
  we = "";
  while (file.available()) {
    //Serial.write(file.read());
      q=file.read();
      we.concat(q);
  }
  file.close();
}




now i have all the data from SD ~500+rows in String variable "we"



// for pendrive I input number "2" to appended

      case 50: //2
      
        we.toCharArray(adat2, we.length());
        printInfo("COMMAND2: Append data to file: TEST1.TXT");               // Append data to the end of the file.
        flashDrive.setFileName("TEST1.TXT");  //set the file name
        if(flashDrive.openFile() == ANSW_USB_INT_SUCCESS){               //open the file
        	flashDrive.moveCursor(CURSOREND);     //if the file exist, move the "virtual" cursor at end of the file, with CURSORBEGIN we actually rewrite our old file
        	//flashDrive.moveCursor(flashDrive.getFileSize()); // is almost the same as CURSOREND, because we put our cursor at end of the file
        }
        for(int a = 0; a < 2; a++){          //write text from string(adat) to flash drive 1 timee
        	if(flashDrive.getFreeSectors()){ //check the free space on the drive
        		flashDrive.writeFile(adat2, strlen(adat2)); //string, string length
        	} else {
        		printInfo("Disk full");
        	}
        }
        flashDrive.closeFile();               //at the end, close the file
        printInfo("Done!");
        break;
		

here if i keep the sd data only 2 rows, those data are copied into pendrive.
more data in SD, lead it to reboot and not copying.
char adat2[200] = ""; -> I hope the size can't be increased in thousands. so please help me to break the String, and send as packages.

"Suggestions for the Arduino Project category" is not for your projects but for THE Arduino project. Hence your topic has been moved to a more suitable location on the forum.

Thanks for using code tags in your first post. Please post your complete code.

How will your pen drive be connected to the Arduino? Or will it be on a PC?
Which Arduino are you using?

Actually, my code was 7k lines as im connecting with 2ADC's with 8 sensor data & plottings, RTC, SD, USB, DWIN_Display integrals and added some other user features. hope the code i posted is more than enough as,
my doubt is how to transfer the 500 lines of data stored in a String "we" to split up and send packages of char adat2[200] in a loop or something....

If its realy necessary for people to analyse and help, i will post it as per request.
Thanks.

How will your pen drive be connected to the Arduino? Or will it be on a PC? -> CH376S module connected through UART with MEGA/ESP32S3
Which Arduino are you using? --> MEGA/ESP32S3 - trying both boards

You can create a small example code with some fixed data.

Have you managed to write to the pen drive using that setup? I'm not familiar with USB hosts on Arduino but maybe there is something in ch376s arduino - Google Search.

Edited best as possible. Please have a look on what specific i needed.


String we = "28/03/2024 23:34:54 10.01 4.81 5.08 10.13 3.33 5.84 2.00 -0.38 4.59 1.45 -0.74 4.49 -3.85 PASS FAIL FAIL PASS FAIL FAIL ;28/03/2024 23:34:54 10.01 4.81 5.08 10.13 3.33 5.84 2.00 -0.38 4.59 1.45 -0.74 4.49 -3.85 PASS FAIL FAIL PASS FAIL FAIL ;";

char adat2[200] = "";
char q;

#include <Ch376msc.h>
#include <SoftwareSerial.h>
SoftwareSerial Pendrive(3, 4);  // RX, TX
Ch376msc flashDrive(Pendrive);
char adatBuffer[255];
char adat[]="Vivamus nec nisl molestie, blandit diam vel, varius mi. Fusce luctus cursus sapien in vulputate.\n";

unsigned long totSect = 0;
unsigned long freeSect = 0;
byte percentg = 0;
byte tmpCommand;  //used to store data coming from serial port
boolean readMore;
static char helpString[] = { "h:Print this help\n\n1:Create\n2:Append\n3:Read\n4:Read date/time\n"
                             "5:Modify date/time\n6:Delete\n7:List dir\n8:Print free space"
                             "\n9:Open/Create folder(s)/subfolder(s)" };


void setup() 

{
  Serial.begin(115200);
  Pendrive.begin(9600);
  flashDrive.init();
  printInfo(helpString);
}


void loop()   
{
    if (flashDrive.checkIntMessage()) {
      if (flashDrive.getDeviceStatus()) {
        Serial.println(F("Flash drive attached!"));
      } else {
        Serial.println(F("Flash drive detached!"));
      }
    }
    if (Serial.available()) {
      tmpCommand = Serial.read();                                                  //read incoming bytes from the serial monitor
      if (((tmpCommand > 48) && (tmpCommand < 58)) && !flashDrive.driveReady()) {  // if the data is ASCII 1 - 9 and no flash drive are attached
        printInfo("Attach flash drive first!");
        tmpCommand = 10;  // change the command byte
      }
      switch (tmpCommand) {

      case 49: //1
        printInfo("COMMAND1: Create and write data to file : TEST1.TXT");    // Create a file called TEST1.TXT
          flashDrive.setFileName("TEST1.TXT");  //set the file name
          flashDrive.openFile();                //open the file

          for(int a = 0; a < 20; a++){          //write text from string(adat) to flash drive 20 times
            flashDrive.writeFile(adat, strlen(adat)); //string, string length
          }
          flashDrive.closeFile();               //at the end, close the file
        printInfo("Done!");
        break;

          //*****************************************************************************************************************************************************
      case 50: //2
      
        we.toCharArray(adat2, we.length());
        printInfo("COMMAND2: Append data to file: TEST1.TXT");               // Append data to the end of the file.
        flashDrive.setFileName("TEST1.TXT");  //set the file name
        if(flashDrive.openFile() == ANSW_USB_INT_SUCCESS){               //open the file
        	flashDrive.moveCursor(CURSOREND);     //if the file exist, move the "virtual" cursor at end of the file, with CURSORBEGIN we actually rewrite our old file
        	//flashDrive.moveCursor(flashDrive.getFileSize()); // is almost the same as CURSOREND, because we put our cursor at end of the file
        }
        for(int a = 0; a < 2; a++){          //write text from string(adat) to flash drive 20 times
        	if(flashDrive.getFreeSectors()){ //check the free space on the drive
        		flashDrive.writeFile(adat2, strlen(adat2)); //string, string length
        	} else {
        		printInfo("Disk full");
        	}
        }
        flashDrive.closeFile();               //at the end, close the file
        printInfo("Done!");
        break;
        
          //*****************************************************************************************************************************************************
      case 51: //3
        printInfo("COMMAND3: Read File: TEST1.TXT");                         // Read the contents of this file on the USB disk, and display contents in the Serial Monitor
        flashDrive.setFileName("TEST1.TXT");  //set the file name
        flashDrive.openFile();                //open the file
        readMore = true;
                //read data from flash drive until we reach EOF
        while(readMore){ // our temporary buffer where we read data from flash drive and the size of that buffer
        	readMore = flashDrive.readFile(adatBuffer, sizeof(adatBuffer));
        	Serial.print(adatBuffer);          //print the contents of the temporary buffer
        }
        flashDrive.closeFile();               //at the end, close the file
        printInfo("Done!");
        break;
        
          //*****************************************************************************************************************************************************
        case 52:                                                  //4
          printInfo("COMMAND4: Read File date/time: TEST1.TXT");  // Read the date and time of file, default 2004.01.01 - 00:00:00
          flashDrive.setFileName("TEST1.TXT");                    //set the file name
          flashDrive.openFile();                                  //open the file
                                                                  //print informations about the file
          Serial.println(flashDrive.getFileName());
          Serial.print(flashDrive.getYear());
          Serial.print("y\t");
          Serial.print(flashDrive.getMonth());
          Serial.print("m\t");
          Serial.print(flashDrive.getDay());
          Serial.print("d\t");
          Serial.print(flashDrive.getHour());
          Serial.print("h\t");
          Serial.print(flashDrive.getMinute());
          Serial.print("m\t");
          Serial.print(flashDrive.getSecond());
          Serial.println('s');
          flashDrive.closeFile();  //at the end, close the file
          printInfo("Done!");
          break;

          //*****************************************************************************************************************************************************
        case 53:                                                    //5
          printInfo("COMMAND5: Modify File date/time: TEST1.TXT");  // Modify the file date/time and save
          flashDrive.setFileName("TEST1.TXT");                      //set the file name
          flashDrive.openFile();                                    //open the file

          flashDrive.setYear(2019);
          flashDrive.setMonth(12);
          flashDrive.setDay(19);
          flashDrive.setHour(03);
          flashDrive.setMinute(38);
          flashDrive.setSecond(42);

          flashDrive.saveFileAttrb();  //save the changed data
          flashDrive.closeFile();      //and yes again, close the file after when you don`t use it
          printInfo("Done!");
          break;
          //*****************************************************************************************************************************************************
        case 54:                                          //6
          printInfo("COMMAND6: Delete File: TEST1.TXT");  // Delete the file named TEST1.TXT
          flashDrive.setFileName("TEST1.TXT");            //set the file name
          flashDrive.deleteFile();                        //delete file
          printInfo("Done!");
          break;
          //*****************************************************************************************************************************************************
        case 55:                                                      //7
          printInfo("COMMAND7: List directory");                      //Print all file names in the current directory
          while (flashDrive.listDir()) {                              // reading next file
            if (flashDrive.getFileAttrb() == CH376_ATTR_DIRECTORY) {  //directory
              Serial.print('/');
              Serial.println(flashDrive.getFileName());  // get the actual file name
            } else {
              Serial.print(flashDrive.getFileName());  // get the actual file name
              Serial.print(" : ");
              Serial.print(flashDrive.getFileSize());  // get the actual file size in bytes
              Serial.print(" >>>\t");
              Serial.println(flashDrive.getFileSizeStr());  // get the actual file size in formatted string
            }
          }
          printInfo("Done!");
          break;
          //*****************************************************************************************************************************************************
        case 56:                                         //8
          totSect = flashDrive.getTotalSectors();        // get the total sector number
          freeSect = flashDrive.getFreeSectors();        // get the available sector number
          percentg = map(freeSect, totSect, 0, 0, 100);  // convert it to percentage (0-100)
          Serial.print("Disk size in bytes: ");
          /*if the sector number is more than 8388607 (8388607 * 512 = 4294966784 byte = 4Gb (fits in a 32bit variable) )
    	    							 e.g. 8388608 * 512 = 4294967296 byte (32bit variable overflows) */

          if (totSect > 8388607) {
            Serial.print(">4Gb");
          } else {
            Serial.print(totSect * SECTORSIZE);
          }
          Serial.print("\tFree space in bytes: ");
          if (freeSect > 8388607) {
            Serial.print(">4Gb");
          } else {
            Serial.print(freeSect * SECTORSIZE);
          }
          Serial.print(F("\tDisk usage :"));
          Serial.print(percentg);
          Serial.print(F("%"));
          switch (flashDrive.getFileSystem()) {  //1-FAT12, 2-FAT16, 3-FAT32
            case 1:
              Serial.println(F("\tFAT12 partition"));
              break;
            case 2:
              Serial.println(F("\tFAT16 partition"));
              break;
            case 3:
              Serial.println(F("\tFAT32 partition"));
              break;
            default:
              Serial.println(F("\tNo valid partition"));
              break;
          }
          break;
          //*****************************************************************************************************************************************************
        case 57:  //9
          switch (flashDrive.cd("/DIR1/DIR2/DIR3", 1)) {
            case CH376_ERR_LONGFILENAME:  //0x01
              Serial.println(F("Directory name is too long"));
              break;

            case ANSW_USB_INT_SUCCESS:  //0x14
              Serial.println(F("Directory created successfully"));
              break;

            case ANSW_ERR_OPEN_DIR:  //0x41
              Serial.println(F("Directory opened successfully"));
              break;

            case ANSW_ERR_MISS_FILE:  //0x42
              Serial.println(F("Directory doesn't exist"));
              break;

            case ANSW_ERR_FOUND_NAME:  //0x43
              Serial.println(F("File exist with the given name"));
              break;

            default:

              break;
          }
          break;
          //*****************************************************************************************************************************************************
        case 104:  //h
          printInfo(helpString);
          break;
        default:
          break;
      } 

    }  
}



void printInfo(const char info[]){
  
  int infoLength = strlen(info);
  if(infoLength > 40){
    infoLength = 40;
  }
    Serial.print(F("\n\n"));
    for(int a = 0; a < infoLength; a++){
      Serial.print('*');
    }
   Serial.println();
   Serial.println(info);
   for(int a = 0; a < infoLength; a++){
      Serial.print('*');
    }
   Serial.print(F("\n\n"));
}

Below are the serial monitor output -- here I inputed number "2"-to append data to pendrive and "3" to read data from pendrive

****************************************

h:Print this help

1:Create

2:Append

3:Read

4:Read date/time

5:Modify date/time

6:Delete

7:List dir

8:Print free space

9:Open/Create folder(s)/subfolder(s)

****************************************

Flash drive attached!

****************************************

COMMAND2: Append data to file: TEST1.TXT

****************************************

*****

Done!

*****

******************************

COMMAND3: Read File: TEST1.TXT

******************************

28/03/2024 23:34:54 10.01 4.81 5.08 10.13 3.33 5.84 2.00 -0.38 4.59 1.45 -0.74 4.49 -3.85 PASS FAIL FAIL PASS FAIL FAIL ;28/03/2024 23:34:54 10.01 4.81 5.08 10.13 3.33 5.84 2.00 -0.38 4.59 1.45 -0.74 4.49 -3.85 PASS FAIL FAIL PASS FAIL FAIL 28/03/2024 23:34:54 10.01 4.81 5.08 10.13 3.33 5.84 2.00 -0.38 4.59 1.45 -0.74 4.49 -3.85 PASS FAIL FAIL PASS FAIL FAIL ;28/03/2024 23:34:54 10.01 4.81 5.08 10.13 3.33 5.84 2.00 -0.38 4.59 1.45 -0.74 4.49 -3.85 PASS FAIL FAIL PASS FAIL FAIL

*****

Done!

*****
  

When I run it, it will ask for input. when I enter 2, it saves the 2 rows of data from String "we" to Char array adat2[200] and thereby those 2 lines are saved in my pendrive.

Here if i add 10 lines instead of 2, it stop the process and no data copied.
ex, instead of current String "we",

I tried,
String we = "28/03/2024 23:34:54 10.01 4.81 5.08 10.13 3.33 5.84 2.00 -0.38 4.59 1.45 -0.74 4.49 -3.85 PASS FAIL FAIL PASS FAIL FAIL ;28/03/2024 23:34:54 10.01 4.81 5.08 10.13 3.33 5.84 2.00 -0.38 4.59 1.45 -0.74 4.49 -3.85 PASS FAIL FAIL PASS FAIL FAIL ;28/03/2024 23:34:54 10.01 4.81 5.08 10.13 3.33 5.84 2.00 -0.38 4.59 1.45 -0.74 4.49 -3.85 PASS FAIL FAIL PASS FAIL FAIL ;28/03/2024 23:34:54 10.01 4.81 5.08 10.13 3.33 5.84 2.00 -0.38 4.59 1.45 -0.74 4.49 -3.85 PASS FAIL FAIL PASS FAIL FAIL ;28/03/2024 23:34:54 10.01 4.81 5.08 10.13 3.33 5.84 2.00 -0.38 4.59 1.45 -0.74 4.49 -3.85 PASS FAIL FAIL PASS FAIL FAIL ;28/03/2024 23:34:54 10.01 4.81 5.08 10.13 3.33 5.84 2.00 -0.38 4.59 1.45 -0.74 4.49 -3.85 PASS FAIL FAIL PASS FAIL FAIL ;28/03/2024 23:34:54 10.01 4.81 5.08 10.13 3.33 5.84 2.00 -0.38 4.59 1.45 -0.74 4.49 -3.85 PASS FAIL FAIL PASS FAIL FAIL ;28/03/2024 23:34:54 10.01 4.81 5.08 10.13 3.33 5.84 2.00 -0.38 4.59 1.45 -0.74 4.49 -3.85 PASS FAIL FAIL PASS FAIL FAIL ;28/03/2024 23:34:54 10.01 4.81 5.08 10.13 3.33 5.84 2.00 -0.38 4.59 1.45 -0.74 4.49 -3.85 PASS FAIL FAIL PASS FAIL FAIL ;28/03/2024 23:34:54 10.01 4.81 5.08 10.13 3.33 5.84 2.00 -0.38 4.59 1.45 -0.74 4.49 -3.85 PASS FAIL FAIL PASS FAIL FAIL ;28/03/2024 23:34:54 10.01 4.81 5.08 10.13 3.33 5.84 2.00 -0.38 4.59 1.45 -0.74 4.49 -3.85 PASS FAIL FAIL PASS FAIL FAIL ;28/03/2024 23:34:54 10.01 4.81 5.08 10.13 3.33 5.84 2.00 -0.38 4.59 1.45 -0.74 4.49 -3.85 PASS FAIL FAIL PASS FAIL FAIL ;";

As mentioned, it stop the process and no data copied.
and its pretty obvious as the adat2[200] is limited by the size 200 so that it cant take more data with it to copy and stucked.

So my request is, I want to split that 10 rows of data in String "we" with delimiter ";" and to pass into the char "adat2" and write in pendrive for 10 times.

You have a terminator (the semicolon) for a record. If you read character by character from the file on the SD card till you find the terminator, you have a smaller batch of data to transfer to the pen drive.

You can copy each character that you read directly into the adat2 array.

Notes:

  1. I did not check how big one record is.
  2. Use of String objects on a Mega can be risky; there even have been reports for ESP boards that they fail when using too many String objects. I did not check your code for it.

So it obvious - you can copy data with portions of 2 lines.
Read 2 lines to the char array, than send it to pendrive, and the same for another 2 lines... and so on

Side note: Using the Strings for collecting the data is a waste of the time and memory.

You are right. Thats what exactly i needed but in the form of code sample.

How to convert a long String lets say 1000 characters that has a delimiters in between 100 characters (in this case ";") to split up and assign to char array to writeup in pendrive or any serial UART devices.

Can you please suggest a way to omit the first 2 lines of the string in the 2nd char traansfer cycle?

use the String substring method to exctract the portion of the big String

For 100 chars parts:
first portion

we.substring(0,100),

the second -

we.substring(100,200)

using substring, we can extract a specific length of data we required from a String.
is it possible to split up and assign each splits based on delimiters into char cycles?

Can you please suggest a sample code in few lines, so i can capture your idea...

Why do you need to split String in specific position if you will combine the parts in the same order in pendrive? The result will be the same string as your initial string we was.

yeah here the data is not length limited breaks, but based on delimiter breaks.
for example, the data i have is,

28/03/2024 23:35:03 10.01 4.81 5.08 10.13 3.33 5.84 2.00 -0.35 4.63 1.57 -0.64 4.57 -3.62 PASS FAIL FAIL PASS FAIL FAIL ;

here, the numerical data will change from positive, negative, three digit to 5 digit including "-" on many fields. so length based breaks are not suggested than delimiters based ones.

You are exactly right. what if i need specific date data? i can start with date and delimiters based approach right? to transfer from SD.

it doesn't matter
The method I proposed preserve your original strings with all its delimeters.

Your methos is superb to replicate the exact data from SD to Pendrive.

Actually, SD is just a storage space where I'm gona dump all data from today to next year.
and im gona take out the data in pendrive once a week/month.
If I want to copy specific date/month data to pendrive, this approach will not be effective isn't it?

I'm expecting to copy the SD data based on delimiters in to rows and can index it with date/month to copy it to pendrive... please suggest am i expecting right!!

In that case you need to find an index of data for desired day by:

we.indexOf("28/03/2024")

and than copy the data with substring method

I will recommend you to read something about string parsing

ok! How does it knows the row ends?
Sure will go through String Parsing...

Thanks!

It depends on your data format. If your day data always a single line, the end of row can be found by indexing "\r\n" ( CRLF) combination

Thanks for letting me know one way solution @b707 post #10 , post #17, post #19

As you suggested, I started looking for String Parsing related stuffs & this is what I found exciting and problem fixed.

arduino-split-string

Here I found a proper technique to splitup the String based on delimiters by substring

//------------Example------------

String originalString = "sensor1:value1;sensor2:value2";
char delimiter = ':';

void setup() {
  Serial.begin(9600);
  int startIndex = 0;
  // Iterate through the string
  for (int i = 0; i < originalString.length(); i++) {
    if (originalString[i] == delimiter) {
      // Extract substring
      String substring = originalString.substring(startIndex, i);
      Serial.println(substring);
      // Update the start index
      startIndex = i + 1;
    }
  }
  // Print the last substring
  String lastSubstring = originalString.substring(startIndex);
  Serial.println(lastSubstring);
}

void loop() {
  // Your main code here
}

Result:
sensor1
value1;sensor2
value2

I used the above logic to my requirement as,

//----Global-Declaration---

char adat2[200] = "";
char delimiter = ';';
char q;
String we;

//-----loop with trigger---

{
  int startIndex = 0;
  for (int i = 0; i < we.length(); i++) {
    if (we[i] == delimiter) {
      String substring = we.substring(startIndex, i);
      substring.toCharArray(adat2, substring.length() + 1);
      Serial.println(substring);

//---------Pendrive store---------

        printInfo("COMMAND2: Append data to file: TEST1.TXT");  // Append data to the end of the file.
        flashDrive.setFileName("TEST1.TXT");  //set the file name
        if(flashDrive.openFile() == ANSW_USB_INT_SUCCESS){               //open the file
        	flashDrive.moveCursor(CURSOREND);     //if the file exist, move the "virtual" cursor at end of the file, with CURSORBEGIN we actually rewrite our old file
        	//flashDrive.moveCursor(flashDrive.getFileSize()); // is almost the same as CURSOREND, because we put our cursor at end of the file
        }
        for(int a = 0; a < 1; a++){          //write text from string(adat) to flash drive 1 time
        	if(flashDrive.getFreeSectors()){ //check the free space on the drive
        		flashDrive.writeFile(adat2, strlen(adat2)); //string, string length
        	} else {
        		printInfo("Disk full");
        	}
        }
        flashDrive.closeFile();               //at the end, close the file
        printInfo("Done!");
      startIndex = i + 1;
    }
  }
  // Print the last substring
  String lastSubstring = we.substring(startIndex);
  Serial.println(lastSubstring);
}

Now, I can copy the data of 500+ lines exactly 1 row/entry and saved in pendrive in the format,

28/03/2024 23:35:03 10.01 4.81 5.08 10.13 3.33 5.84 2.00 -0.35 4.63 1.57 -0.64 4.57 -3.62 PASS FAIL FAIL PASS FAIL FAIL
28/03/2024 23:35:07 10.01 4.81 5.08 10.13 3.33 5.84 2.00 -0.36 4.61 1.51 -0.69 4.53 -3.74 PASS FAIL FAIL PASS FAIL FAIL
.
.

The advantage over here is,
I can easily compare each row data entry with specific date/time using the substring of each outputed subtring at ease and proceed my development.

Thanks again :grin:

1 Like