Go Down

Topic: reading text from an sd card (Read 1 time) previous topic - next topic

mrboni

I'm playing with the sd card read/write tutorial.  The example sketch works fine.

When I create a text file in notepad, copy it to an sd card and open it using the arduino, the formatting is different.  Ie -

Original text - this is a test 1 2 34
Text printed in serial monitor - tisisa es 12 4

I've tried asni, utf8 etc

any ideas?

pylon


wanderson


I'm playing with the sd card read/write tutorial.  The example sketch works fine.

When I create a text file in notepad, copy it to an sd card and open it using the arduino, the formatting is different.  Ie -

Original text - this is a test 1 2 34
Text printed in serial monitor - tisisa es 12 4

I've tried asni, utf8 etc

any ideas?



Original text - this is a test 1 2 34
Serial text    -  t  is is a  es  1 2    4


As previously mentioned code would help; however, the serial text is a subset of the original which would indicate data being dropped, either in the serial transmission to the computer or possibly in your code reading the SD card.
New true random number library available at: http://code.google.com/p/avr-hardware-random-number-generation/

Current version 1.0.1

mrboni

Ha, I just rewrote the code and it works. Not sure what it was as the code is mostly just the example sketch.

One question, this code is printing to serial using Serial.write, not Serial.print.  As I understand it this is because the data is in binary form. Given this, how do I assign the text (ascii) contents of this file to a string?


Here is the code -

Code: [Select]

#include <SD.h>

File myFile;

void setup()
{
  Serial.begin(9600);
  Serial.print("Initializing SD card...");
  // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
  // Note that even if it's not used as the CS pin, the hardware SS pin
  // (10 on most Arduino boards, 53 on the Mega) must be left as an output
  // or the SD library functions will not work.
   pinMode(53, OUTPUT);
   
  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");
 
 
  // re-open the file for reading:
  myFile = SD.open("test.txt");
  if (myFile) {
    Serial.println("test.txt:");
   
    // read from the file until there's nothing else in it:
    while (myFile.available()) {
    Serial.write(myFile.read());
    }
    // close the file:
    myFile.close();
  } else {
  // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
}

void loop()
{
// nothing happens after setup
}


michael_x

The corresponding method to Serial.read() is Serial.write().

Additionally, write() can also send null terminated strings, if you provide a char* parameter.
Serial.print()  and println() are rather convenience methods to format various data types into readable text.

To read longer texts from a file into variables you have to take care where to store it ( and how long ), as Arduino RAM is very limited.

The sample code does not store it at all, but simply uses every read character just as output parameter to Serial.write().
This way, it can handle files of any length.

For any other use, the solution depends on what you need.

Go Up