A txt file(603bytes) Reading by Teensy4.1 take 3 second. Is this a simple thing?

ishan

New member
when placed a simple txt file "DATA1.txt" on sd card . i tried read string/variables from it .
I think it took 3 seconds to read the data from one file.
I've added Time_Track variables in my code for when different events end. to see how much time it took.
The code is something like this:
C++:
#include <Wire.h>
#include <SPI.h>
#include <SD.h>               
 

  String DATA_NAME = "";                   
  String DATA_TYPE = "";
  String Temp_SubString = "";
  String Status_Path_Name = "";
  char BOOL_CHAR = 'A';
 ;
   bool P_STATUS[16] = {true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,false}; 
   byte P_EARNED[16] = {100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100};
 
                unsigned long Time_Track = 0;
 
 
void setup() {   
     Serial.begin(9600); 
    if (!SD.begin(BUILTIN_SDCARD)) {
                              Serial.println("SD init failed");
                                 return;
                                  }
      SD.begin(BUILTIN_SDCARD);     
            READ_TXT ();
}
 
 
void loop() {   
              }   

//=====================================READ TXT FILE=========  ===== 
          void READ_TXT (){               
              Time_Track = millis(); 
                      Serial.println("Start Read TXT.File.................");
                      Serial.print("Time_Track : ");     Serial.println(millis() - Time_Track); 

             File txtFile = SD.open("DATA1.txt"); 
  if (txtFile) {        int Process_Line = 0;     
       Serial.println(" if .txtFile.");     Serial.print("Time_Track : ");     Serial.println(millis() - Time_Track);     
    while (txtFile.available()) {
    
        Serial.println(" while .txt.availble.");     Serial.print("Time_Track : ");     Serial.println(millis() - Time_Track);   
      String currentLine1 = txtFile.readStringUntil('\n');
      if (currentLine1.indexOf("Bank_Name") >= 0) {       
                                           DATA_NAME = currentLine1.substring(13, currentLine1.length()); 
                                        Serial.print("TXT DATA_NAME");   Serial.println(DATA_NAME);
                                                 }
       if (currentLine1.indexOf("DATA_Type") >= 0) {     
                                          DATA_TYPE = currentLine1.substring(13, currentLine1.length()); 
                                        Serial.print("TXT DATA_TYPE");  Serial.println(DATA_TYPE); 
                                               }
          if (currentLine1.indexOf("DATA_VALUE") >= 0) {   
                                          Temp_SubString = currentLine1.substring(13,16);  //   
                                        Serial.print("TXT  DATA_VALUE : ");   Serial.println(Temp_SubString);
                                  
                                               }
                                                
          Serial.print("Process_Line : ");   Serial.println(Process_Line);      Process_Line++;     
            Serial.println("once Process_Line complete");     Serial.print("Time_Track : ");     Serial.println(millis() - Time_Track);                         
    }
    txtFile.close();
      Serial.println(" txtFile.closed");     Serial.print("Time_Track : ");     Serial.println(millis() - Time_Track);   
  }
 
  else {
    Serial.println("Error opening data.txt");
  }
   Serial.println("END Read TXT.File.................");
     Serial.println(" Ended Reading TXT .File");     Serial.print("Time_Track : ");     Serial.println(millis() - Time_Track);           
  }

 //=========== =====READ TXT FILE=============== =====================
Once the entire file is read in setup, the serial monitor output is as follows
Code:
Start Read TXT.File.................
Time_Track : 0
 if .txtFile.
Time_Track : 1
 while .txt.availble.
Time_Track : 1
Process_Line : 0
once Process_Line complete
Time_Track : 1
 while .txt.availble.
Time_Track : 1
TXT  DATA_VALUE : 000
Process_Line : 1
once Process_Line complete
Time_Track : 1
 while .txt.availble.
Time_Track : 1
TXT DATA_TYPEGENRAL
Process_Line : 2
once Process_Line complete
Time_Track : 1
 while .txt.availble.
Time_Track : 1
Process_Line : 3
once Process_Line complete
Time_Track : 1
 while .txt.availble.
Time_Track : 1
Process_Line : 4
once Process_Line complete
Time_Track : 1
 while .txt.availble.
Time_Track : 1
Process_Line : 5
once Process_Line complete
Time_Track : 1
 while .txt.availble.
Time_Track : 1
Process_Line : 6
once Process_Line complete
Time_Track : 1
 while .txt.availble.
Time_Track : 1
Process_Line : 7
once Process_Line complete
Time_Track : 1
 while .txt.availble.
Time_Track : 1
Process_Line : 8
once Process_Line complete
Time_Track : 1
 while .txt.availble.
Time_Track : 1
Process_Line : 9
once Process_Line complete
Time_Track : 1
 while .txt.availble.
Time_Track : 1
Process_Line : 10
once Process_Line complete
Time_Track : 2
 while .txt.availble.
Time_Track : 2
Process_Line : 11
once Process_Line complete
Time_Track : 2
 while .txt.availble.
Time_Track : 2
Process_Line : 12
once Process_Line complete
Time_Track : 2
 while .txt.availble.
Time_Track : 2
Process_Line : 13
once Process_Line complete
Time_Track : 2
 while .txt.availble.
Time_Track : 2
Process_Line : 14
once Process_Line complete
Time_Track : 2
 while .txt.availble.
Time_Track : 2
Process_Line : 15
once Process_Line complete
Time_Track : 2
 while .txt.availble.
Time_Track : 2
Process_Line : 16
once Process_Line complete
Time_Track : 2
 while .txt.availble.
Time_Track : 2
Process_Line : 17
once Process_Line complete
Time_Track : 2
 while .txt.availble.
Time_Track : 2
Process_Line : 18
once Process_Line complete
Time_Track : 2
 while .txt.availble.
Time_Track : 2
Process_Line : 19
once Process_Line complete
Time_Track : 2
 while .txt.availble.
Time_Track : 2
Process_Line : 20
once Process_Line complete
Time_Track : 2
 while .txt.availble.
Time_Track : 2
Process_Line : 21
once Process_Line complete
Time_Track : 2
 while .txt.availble.
Time_Track : 2
Process_Line : 22
once Process_Line complete
Time_Track : 2
 while .txt.availble.
Time_Track : 2
Process_Line : 23
once Process_Line complete
Time_Track : 2
 while .txt.availble.
Time_Track : 2
Process_Line : 24
once Process_Line complete
Time_Track : 2
 while .txt.availble.
Time_Track : 2
Process_Line : 25
once Process_Line complete
Time_Track : 2
 while .txt.availble.
Time_Track : 2
Process_Line : 26
once Process_Line complete
Time_Track : 2
 while .txt.availble.
Time_Track : 2
Process_Line : 27
once Process_Line complete
Time_Track : 2
 while .txt.availble.
Time_Track : 2
Process_Line : 28
once Process_Line complete
Time_Track : 2
 while .txt.availble.
Time_Track : 2
Process_Line : 29
once Process_Line complete
Time_Track : 2
 while .txt.availble.
Time_Track : 2
Process_Line : 30
once Process_Line complete
Time_Track : 2
 while .txt.availble.
Time_Track : 2
Process_Line : 31
once Process_Line complete
Time_Track : 2
 while .txt.availble.
Time_Track : 2
Process_Line : 32
once Process_Line complete
Time_Track : 2
 while .txt.availble.
Time_Track : 2
Process_Line : 33
once Process_Line complete
Time_Track : 2
 while .txt.availble.
Time_Track : 2
Process_Line : 34
once Process_Line complete
Time_Track : 1002
 txtFile.closed
Time_Track : 1002
END Read TXT.File.................
 Ended Reading TXT .File
Time_Track : 1002
According to the serial result, it took only 1 millisecond to complete each task. but at last it took 1002 millisecod 2 times.i think total near about 3 seconds.
Is it normal(reading Speed) for such a simple task to take 3 seconds or am I making some mistake in reading txt file in my code?
Well, I need to read upto 100 txt files as soon as possible.
Can any suggestions be made on this?
Thank u....
 

Attachments

  • DATA1.txt
    603 bytes · Views: 30
This problem happens because the last line of your file lacks a newline character, but you're calling readStringUntil() and telling it to look for a newline character.

Code:
String currentLine1 = txtFile.readStringUntil('\n');

Your program reads most of the file very quickly. Then on the last line, it waits for the Arduino default timeout (these Arduino functions are designed for parsing incoming serial data). That default is 1000, or 1 full second. You can override the timeout with setTimeout(), like this:

Code:
txtFile.setTimeout(0);

Or you could create the DATA1.txt file with a newline character on the last line.

Here's a complete modified program which reads your entire file quickly.

Code:
#include <Wire.h>
#include <SPI.h>
#include <SD.h>


String DATA_NAME = "";
String DATA_TYPE = "";
String Temp_SubString = "";
String Status_Path_Name = "";
char BOOL_CHAR = 'A';
;
bool P_STATUS[16] = {true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, false};
byte P_EARNED[16] = {100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100};

unsigned long Time_Track = 0;


void setup() {
  Serial.begin(9600);
  if (!SD.begin(BUILTIN_SDCARD)) {
    Serial.println("SD init failed");
    return;
  }
  SD.begin(BUILTIN_SDCARD);
  READ_TXT ();
}


void loop() {
}

//=====================================READ TXT FILE=========  =====
void READ_TXT () {
  Time_Track = millis();
  Serial.println("Start Read TXT.File.................");
  Serial.print("Time_Track : ");     Serial.println(millis() - Time_Track);

  File txtFile = SD.open("DATA1.txt");
  if (txtFile) {
    txtFile.setTimeout(0);
    int Process_Line = 0;
    Serial.println(" if .txtFile.");     Serial.print("Time_Track : ");     Serial.println(millis() - Time_Track);
    while (txtFile.available()) {

      Serial.println(" while .txt.availble.");     Serial.print("Time_Track : ");     Serial.println(millis() - Time_Track);
      String currentLine1 = txtFile.readStringUntil('\n');
      if (currentLine1.indexOf("Bank_Name") >= 0) {
        DATA_NAME = currentLine1.substring(13, currentLine1.length());
        Serial.print("TXT DATA_NAME");   Serial.println(DATA_NAME);
      }
      if (currentLine1.indexOf("DATA_Type") >= 0) {
        DATA_TYPE = currentLine1.substring(13, currentLine1.length());
        Serial.print("TXT DATA_TYPE");  Serial.println(DATA_TYPE);
      }
      if (currentLine1.indexOf("DATA_VALUE") >= 0) {
        Temp_SubString = currentLine1.substring(13, 16); //
        Serial.print("TXT  DATA_VALUE : ");   Serial.println(Temp_SubString);

      }

      Serial.print("Process_Line : ");   Serial.println(Process_Line);      Process_Line++;
      Serial.println("once Process_Line complete");     Serial.print("Time_Track : ");     Serial.println(millis() - Time_Track);
    }
    txtFile.close();
    Serial.println(" txtFile.closed");     Serial.print("Time_Track : ");     Serial.println(millis() - Time_Track);
  }

  else {
    Serial.println("Error opening data.txt");
  }
  Serial.println("END Read TXT.File.................");
  Serial.println(" Ended Reading TXT .File");     Serial.print("Time_Track : ");     Serial.println(millis() - Time_Track);
}

Also, while using Arduino IDE, you might occasionally press Ctrl-T to automatically reformat the indenting. It makes the code much easier to read.
 
Back
Top