httpwwwbethedevcom
324329524619168

Reading CSV files from a SD Card with An Arduino

Writing CSV files to an SD card is a fairly easy matter, build a string, adding a comma between each number and send that string to the ...


Writing CSV files to an SD card is a fairly easy matter, build a string, adding a comma between each number and send that string to the SD card. However, getting that data off the card and loaded back into use is not such an easy matter.

Here is a simple sketch that reads a CSV file with four numbers and two strings on each line.  It doesn't use the dangerous String class.

gps.csv

The following sketch successfully loads numbers and strings from an SD Card and is completely print values in serial (Serial Monitor).

Change the "gps.csv" file name according to your file name.

#include <SoftwareSerial.h>
#include <SD.h>
#include <SPI.h>
/* CSV File Reading */
File file;
int SC = 53; //SC - Pin 53 Arduino Mega
char location;
bool readLine(File &f, char* line, size_t maxLen) {
for (size_t n = 0; n < maxLen; n++) {
int c = f.read();
if ( c < 0 && n == 0) return false; // EOF
if (c < 0 || c == '\n') {
line[n] = 0;
return true;
}
line[n] = c;
}
return false; // line too long
}
bool readVals(long* v1, long* v2, long* v3, long* v4, String* loc,String* loc2) {
char line[200], *ptr, *str;
if (!readLine(file, line, sizeof(line))) {
return false; // EOF or too long
}
*v1 = strtol(line, &ptr, 10);
if (ptr == line) return false; // bad number if equal
while (*ptr) {
if (*ptr++ == ',') break;
}
*v2 = strtol(ptr, &str, 10);
while (*ptr) {
if (*ptr++ == ',') break;
}
*v3 = strtol(ptr, &str, 10);
while (*ptr) {
if (*ptr++ == ',') break;
}
*v4 = strtol(ptr, &str, 10);
while (*ptr) {
if (*ptr++ == ',') break;
}
String a = strtok_r(ptr, ",", &str);
String first(str);
*loc = first;
String let(a);
*loc2 = let;
return str != ptr; // true if number found
}
/* Close CSV File Reading */
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(9600);
//SD Card Reader Setup
Serial.begin(9600);
if (!SD.begin(SC)) {
Serial.println("begin error");
return;
}
file = SD.open("gps.CSV", FILE_READ);
if (!file) {
Serial.println("open error");
return;
}
}
void loop() // run over and over
{
long x, y, z, k;
String loc,loc2;
while (readVals(&x, &y, &z, &k, &loc, &loc2)) {
//First 4 Long datatype variables
Serial.println(x);
Serial.println(y);
Serial.println(z;
Serial.println(k);
//Last 2 String type variables
Serial.println(loc);
Serial.println(loc2);
}
}
Arduino sd read CSV file.

If you have any questions please post your comments below.

Thank you!

Dev 4940884141221014460

Post a Comment Default Comments Disqus Comments

  1. I have used the csv file to store latitudes and longitudes coming from adafruit gps. I want to use the data to make distance calculations. Can you please help me on how to get the last two entries in the csv file to make calculations. so that all the time i want to make a calculation i just need to get the last two entries of the cordinates.

    ReplyDelete
    Replies
    1. Sorry! I could not able to make at the time little work around. try to answer as possible. if you have any other question ! Thanks

      Delete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Could you explain how does the program read the line by line? Where is the line counter number?
    Thx David

    ReplyDelete
  4. Thanks for the example. I'm trying to do something similar, but with three strings. Would you be able to explain what these lines do? I am not sure about ptr and scr as well as the significance of * and &.

    char line[200], *ptr, *str;

    String a = strtok_r(ptr, ",", &str);
    String first(str);
    *loc = first;
    String let(a);
    *loc2 = let;

    ReplyDelete

emo-but-icon
:noprob:
:smile:
:shy:
:trope:
:sneered:
:happy:
:escort:
:rapt:
:love:
:heart:
:angry:
:hate:
:sad:
:sigh:
:disappointed:
:cry:
:fear:
:surprise:
:unbelieve:
:shit:
:like:
:dislike:
:clap:
:cuff:
:fist:
:ok:
:file:
:link:
:place:
:contact:

Home item

Popular Posts

Random Posts