Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

SD Card read txt file specific data

imranahmed

Advanced Member level 3
Joined
Dec 4, 2011
Messages
765
Helped
3
Reputation
6
Reaction score
2
Trophy points
1,298
Location
Karachi,Pakistan
Activity points
6,115
Please let me know I want to read data from SD Card file name is driverID.txt , there are driver ID`s in it.
2741#8785#3922#5805# like this.

I want to read 3922# but when I read it is removes data from start to some characters.

How to read it properly please let me know.
 

KlausST

Super Moderator
Staff member
Joined
Apr 17, 2014
Messages
19,284
Helped
4,272
Reputation
8,547
Reaction score
4,236
Trophy points
1,393
Activity points
127,990
Hi,

What's the problem?
SD card hardware?
File system?
Interface?
Microcontroller?
Software?
Anything else?

You are not the first one to read a SD card with a microcontroller. Thus there are many descriptions, examples code examples, application notes, videos...

Klaus
 

imranahmed

Advanced Member level 3
Joined
Dec 4, 2011
Messages
765
Helped
3
Reputation
6
Reaction score
2
Trophy points
1,298
Location
Karachi,Pakistan
Activity points
6,115
Hi,

What's the problem?
SD card hardware?
File system?
Interface?
Microcontroller?
Software?
Anything else?

You are not the first one to read a SD card with a microcontroller. Thus there are many descriptions, examples code examples, application notes, videos...

Klaus

Code attched I want to find specific driver number how is it possible?
driverid.txt attached. PLease let me know I am stuck here please.

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

File myFile;
String incomingString = "2741#";
String myString = "";
unsigned int myArrayIndex = 0;

LiquidCrystal_I2C lcd(0x27, 20, 4);


void setup() {
  if (!SD.begin(10)) { //make sure sd card was found
    while (true);
  }

  lcd.begin();
  lcd.setCursor(0, 0);
  lcd.print("DRIVERID.txt");
  delay(2000);
  lcd.clear();
 

}

void loop() {
  myFile = SD.open("driverid.txt"); // open the file for reading
  lcd.setCursor(0, 0); //
  //lcd.autoscroll(); //enable auto-scrolling

  if (myFile) {
    while (myFile.available()) { //execute while file is available
      String letter = myFile.readString(); //read next character from file
           if (letter == "2741#"){
            lcd.print(letter);
            }
      //   delay(80);
    }
 
    myFile.close(); //close file
  }

 // lcd.clear();
}
 

Attachments

  • DRIVERID.TXT
    7.8 KB · Views: 2

andre_teprom

Super Moderator
Staff member
Joined
Nov 7, 2006
Messages
9,222
Helped
1,148
Reputation
2,315
Reaction score
1,124
Trophy points
1,403
Location
Brazil
Activity points
53,693
Still, no specific nor clear question.
Why not spent few more words to depict what exactly is working or not ?
 

andre_teprom

Super Moderator
Staff member
Joined
Nov 7, 2006
Messages
9,222
Helped
1,148
Reputation
2,315
Reaction score
1,124
Trophy points
1,403
Location
Brazil
Activity points
53,693
Ok, there is some advance in the question.
At least now we can presume that SD reading is working properly and the question is not specific to SD card reading itself.

Are you aware of the C standard String libraries ?
With these funcions you can find a text inside another.
 

imranahmed

Advanced Member level 3
Joined
Dec 4, 2011
Messages
765
Helped
3
Reputation
6
Reaction score
2
Trophy points
1,298
Location
Karachi,Pakistan
Activity points
6,115
Ok, there is some advance in the question.
At least now we can presume that SD reading is working properly and the question is not specific to SD card reading itself.
Are you aware of the C standard String libraries ?
Whith these funcions you can find text from another.
Yes SD Card working properly and question is not specific to SD card reading itself, it is reading good but I want to read specific driverid from file driverid.txt.

Yes I am aware of the C standard String libraries not all but most.
How brother please give hint, I am stuck.
 

andre_teprom

Super Moderator
Staff member
Joined
Nov 7, 2006
Messages
9,222
Helped
1,148
Reputation
2,315
Reaction score
1,124
Trophy points
1,403
Location
Brazil
Activity points
53,693
please give hint, I am stuck

There are many articles on the Web with comprehensive tutorials., why not start doing so ?
You have the above String variable "letter" within which you should seek for the keyword you need to find.
 

imranahmed

Advanced Member level 3
Joined
Dec 4, 2011
Messages
765
Helped
3
Reputation
6
Reaction score
2
Trophy points
1,298
Location
Karachi,Pakistan
Activity points
6,115
There are many articles on the Web with comprehensive tutorials., why not start doing so ?
You have the above String variable "letter" within which you should seek for the keyword you need to find.
I found many articles most of them using example of Arduino IDE of SD Card but I do not need it.
So what is the purpose of this forum if you recommend others to go to search on web, I was searched before asking on forum.
 

andre_teprom

Super Moderator
Staff member
Joined
Nov 7, 2006
Messages
9,222
Helped
1,148
Reputation
2,315
Reaction score
1,124
Trophy points
1,403
Location
Brazil
Activity points
53,693
I found many articles most of them using example of Arduino IDE of SD Card but I do not need it.
Your question is still unclear.
I said the question was no longer related to SD card, but rather to string manipulation
BTW, the purpose of this forum is people ask for help, providing as many information as possible and expect for tips.

I want to read only 5805# in driverid.txt, but is it not working it is only read first driverid 2741# only.

Since you did not seem able how to find the answer, consider doing that:

Replace this:
  • if (letter == "2741#")

By this:
  • if( letter .indexOf(" 5805#") > 0)
 

imranahmed

Advanced Member level 3
Joined
Dec 4, 2011
Messages
765
Helped
3
Reputation
6
Reaction score
2
Trophy points
1,298
Location
Karachi,Pakistan
Activity points
6,115
Your question is still unclear.
I said the question was no longer related to SD card, but rather to string manipulation
BTW, the purpose of this forum is people ask for help, providing as many information as possible as expect for tips.

Since you did not seem able how to find the answer, consider doing that:

Replace this:
if (letter == "2741#")

By this:
if( letter .indexOf(" 5805#") > 0)
Thank you I am trying it sorry I am not good in English but please try to understand.
 

andre_teprom

Super Moderator
Staff member
Joined
Nov 7, 2006
Messages
9,222
Helped
1,148
Reputation
2,315
Reaction score
1,124
Trophy points
1,403
Location
Brazil
Activity points
53,693
At the above snippet, remove the blank space, wrongly added on the beggining of the text of interest ( 5805#).
The same space before the 'dot' should be removed, apparently came with my copy-paste.
 

imranahmed

Advanced Member level 3
Joined
Dec 4, 2011
Messages
765
Helped
3
Reputation
6
Reaction score
2
Trophy points
1,298
Location
Karachi,Pakistan
Activity points
6,115
Your question is still unclear.
I said the question was no longer related to SD card, but rather to string manipulation
BTW, the purpose of this forum is people ask for help, providing as many information as possible and expect for tips.



Since you did not seem able how to find the answer, consider doing that:

Replace this:
  • if (letter == "2741#")

By this:
  • if( letter .indexOf(" 5805#") > 0)
My best brother thank you very much now I am successful because of your support thank you brother, from past 6 hours I was stuck. You are my bestd brother.
 

imranahmed

Advanced Member level 3
Joined
Dec 4, 2011
Messages
765
Helped
3
Reputation
6
Reaction score
2
Trophy points
1,298
Location
Karachi,Pakistan
Activity points
6,115
Hi,

Please let me know I wrote a code for reading data from SD Card but the function verify only runs first timer properly second time it is not working why?

Please find the attached Serial Monitor snap, only first time run works but after some delay again verification is not happening.
DriverID.txt file is attached.
 

Attachments

  • DRIVERID.TXT
    7.8 KB · Views: 4
  • DataVerify.png
    DataVerify.png
    25.6 KB · Views: 6

imranahmed

Advanced Member level 3
Joined
Dec 4, 2011
Messages
765
Helped
3
Reputation
6
Reaction score
2
Trophy points
1,298
Location
Karachi,Pakistan
Activity points
6,115
No code, no guess...

Ohh, Sorry I forgot.

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

bool verify = false;
String myString = "";
String type = "AZ-7773#";
String ID = "Vehicle ID";
File myFile;

bool verifydata(String typeID, String ID) {
  Serial.print("SCAN DATA:");
  Serial.println(typeID);
  Serial.println(ID + ".txt below Scan Data ");
  if (ID.equals("Driver ID"))
  {
    Serial.println("OPEN DRIVER ID");
    SDcardinitialize();
    myFile = SD.open("DRIVERID.txt");
  }
  else if(ID.equals("Vehicle ID"))
  {
    Serial.println("OPEN TRUCK ID");
    SDcardinitialize();
    myFile = SD.open("TRUCKID.txt");
  }

  if (myFile)
  {
    Serial.println(ID + ".txt in myFile");

    // read from the file until there's nothing else in it:
    while (myFile.available())
    {
      myString = myFile.readString();
      if (myString.indexOf(typeID) >= 0)
      {
        Serial.println("FOUND");
        myString = "";
        myFile.close();
        return 1;
      }
      else
      {       
        Serial.println("DOES NOT FOUND");
        myString = "";
        myFile.close();
        return 0;
      }

    }
    // close the file:
    myFile.close();   
  }
  else
  {
    // if the file didn't open, print an error:
    Serial.println("error opening " + ID + ".txt");
  }
}

void SDcardinitialize()
{
  Serial.print("Initializing SD card...");

  if (!SD.begin(10)) {
    Serial.println("initialization failed!");
    while (1);
  }
  Serial.println("initialization done.");
}


void setup() {
  Serial.begin(9600);
  SDcardinitialize();
  delay(2000);
}

void loop() {
verify = verifydata(type , ID);
if(verify == 0){
  Serial.print("Not Verify ");
  Serial.println(verify);
  }else{
    Serial.print("Verified ");
    Serial.println(verify);
    }
delay(4000);
}
 

andre_teprom

Super Moderator
Staff member
Joined
Nov 7, 2006
Messages
9,222
Helped
1,148
Reputation
2,315
Reaction score
1,124
Trophy points
1,403
Location
Brazil
Activity points
53,693
At a glance, once you are repeatedly calling the SD.open() funtion, seems like you missed the SD.close() function somewhere.
 

imranahmed

Advanced Member level 3
Joined
Dec 4, 2011
Messages
765
Helped
3
Reputation
6
Reaction score
2
Trophy points
1,298
Location
Karachi,Pakistan
Activity points
6,115
At a glance, once you are repeatedly calling the SD.open() funtion, seems like you missed the SD.close() function somewhere.
But I closed the file after reading in IF ELSE condition but is it not working, only first time running but after that showing un-expected behaviour.
Please let me know.
 

andre_teprom

Super Moderator
Staff member
Joined
Nov 7, 2006
Messages
9,222
Helped
1,148
Reputation
2,315
Reaction score
1,124
Trophy points
1,403
Location
Brazil
Activity points
53,693
You close the file, however you cyclically instantiate the SD.begin() without having closed it. One thing is the SD card instance ( e.g: you could read more than one SD card in your application ), other thing is the file instance ( you could have several files in your card ). As said, seems like the last instance was not closed when you try to 'begin' another one after each delay.
 

imranahmed

Advanced Member level 3
Joined
Dec 4, 2011
Messages
765
Helped
3
Reputation
6
Reaction score
2
Trophy points
1,298
Location
Karachi,Pakistan
Activity points
6,115
You close the file, however you cyclically instantiate the SD.begin() without having closed it. One thing is the SD card instance ( e.g: you could read more than one SD card in your application ), other thing is the file instance ( you could have several files in your card ). As said, seems like the last instance was not closed when you try to 'begin' another one after each delay.
I tried many options but I do not know why it is not working, it is seems like no bugs but do not know why it is not working please ask to any expert.
 

LaTeX Commands Quick-Menu:

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top