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 ...
https://bethedev.blogspot.com/2017/01/reading-csv-files-from-sd-card-with.html
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
Change the "gps.csv" file name according to your file name.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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); | |
} | |
} |
If you have any questions please post your comments below.
Thank you!
Thank you! its works awesome
ReplyDeleteI 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.
ReplyDeleteSorry! I could not able to make at the time little work around. try to answer as possible. if you have any other question ! Thanks
DeleteThis comment has been removed by the author.
ReplyDeleteCould you explain how does the program read the line by line? Where is the line counter number?
ReplyDeleteThx David
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 &.
ReplyDeletechar line[200], *ptr, *str;
String a = strtok_r(ptr, ",", &str);
String first(str);
*loc = first;
String let(a);
*loc2 = let;