Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SD.h>
- #include <SPI.h>
- const int chipSelect = 4;
- String incomingByte;
- String lastTwo;
- int len;
- File root;
- void setup() {
- // Open serial communications and wait for port to open:
- Serial1.begin(9600);
- Serial.begin(9600);
- while (!Serial) {
- ; // wait for serial port to connect. Needed for native USB port only
- }
- root = SD.open("/");
- Serial.println("");
- Serial1.print("OK");
- Serial.println("");
- // // see if the card is present and can be initialized:
- // if (!SD.begin(chipSelect)) {
- // Serial.println("Card failed, or not present");
- // // don't do anything more:
- // while (1);
- // }
- // Serial1.println("");
- //
- // Serial1.println("card initialized.");
- // //delay(2000);
- // open the file. note that only one file can be open at a time,
- // so you have to close this one before opening another.
- // Serial.println("Lecture de /DATA/2019/0619.csv.");
- // //delay(2000);
- // File dataFile = SD.open("/DATA/2019/06.csv");
- //
- // // if the file is available, write to it:
- // if (dataFile) {
- // while (dataFile.available()) {
- // Serial1.write(dataFile.read());
- // //delay(200);
- // }
- // dataFile.close();
- // }
- // // if the file isn't open, pop up an error:
- // else {
- // Serial.println("error opening datalog.txt");
- // }
- }
- void loop() {
- incomingByte = "";
- String read = "read";
- String dump = "dump";
- int fileNames_nbr = 0;
- while (Serial1.available() > 0) {
- incomingByte += char(Serial1.read());
- if (incomingByte != "")
- {
- Serial.print(incomingByte);
- }
- // Serial.println("incomingByte: "+incomingByte);
- delay(10);
- if (incomingByte == "list") {
- //Serial1.println("Contenu de la carte SD :");
- //Serial1.println("");
- delay(500);
- Serial.print("Initializing SD card...");
- if (!SD.begin(chipSelect)) {
- Serial.println("initialization failed!");
- }
- Serial.println("initialization done.");
- root = SD.open("/DATA");
- printDirectory(root, 0);
- //Serial1.println("end");
- }
- if (incomingByte.indexOf("read ") >= 0) {
- while (Serial1.available() > 0) {
- incomingByte += char(Serial1.read());
- }
- if (!SD.begin(4)) {
- Serial.println("initialization failed!");
- while (1);
- }
- String fileName;
- char fileNamestr[incomingByte.length() - 5];
- //Serial1.println("Lecture d'un fichier");
- Serial1.println("");
- fileName = incomingByte.substring(5);
- //Serial1.println("filename:" + fileName);
- //Serial1.println("incoming byte: " + incomingByte);st
- fileName.toCharArray(fileNamestr, sizeof(fileNamestr));
- File myFile = SD.open(fileNamestr);
- if (myFile) {
- //Serial1.println("Lecure de " + fileName);
- // read from the file until there's nothing else in it:
- while (myFile.available()) {
- Serial1.write(myFile.read());
- //Serial1.println("Lecure en cours");
- //Serial1.println("myFile:"+myFile);
- }
- // close the file:
- myFile.close();
- incomingByte = "";
- Serial1.println("");
- //Serial1.println("end");
- } else {
- // if the file didn't open, print an error:
- Serial1.println("Ce fichier n'existe pas");
- Serial1.println("");
- //Serial1.println("myFile:"+myFile);
- incomingByte = "";
- }
- }
- if (incomingByte.indexOf("dump ") >= 0) {
- while (Serial1.available() > 0) {
- incomingByte += char(Serial1.read());
- }
- String fileNames;
- char fileNamesstr[incomingByte.length() - 5];
- //Serial1.println("Lecture d'un fichier");
- Serial1.println("");
- fileNames = incomingByte.substring(5);
- fileNames.toCharArray(fileNamesstr, sizeof(fileNamesstr));
- for (int i = 0; i < strlen(fileNamesstr); i++) {
- if (fileNames[i] == ';')
- {
- fileNames_nbr++;
- }
- }
- Serial1.write("Nombre de fichiers: ");
- Serial1.println(fileNames_nbr);
- //Serial1.println("Fichiers à traiter: ");
- String fichier="";
- if (!SD.begin(4)) {
- Serial.println("initialization failed!");
- while (1);
- }
- for (int j = 0; j < fileNames_nbr + 1; j++) {
- fichier = getValue(fileNames, ';', j);
- //Serial1.println(fichier);
- //Serial1.println(fichiers[j]);
- File myFiled = SD.open(fichier);
- if (myFiled) {
- //Serial1.println("Lecure de " + fichier);
- // read from the file until there's nothing else in it:
- while (myFiled.available()) {
- Serial1.write(myFiled.read());
- //Serial1.println("Lecture en cours");
- //Serial1.println("myFile:"+myFiled);
- }
- // close the file:
- myFiled.close();
- fichier="";
- }
- }
- }
- }
- }
- void printDirectory(File dir, int numTabs) {
- while (true) {
- File entry = dir.openNextFile();
- if (! entry) {
- // no more files
- break;
- }
- for (uint8_t i = 0; i < numTabs; i++) {
- Serial1.print('\t');
- }
- Serial1.print(entry.name());
- if (entry.isDirectory()) {
- //Serial1.println("/");
- printDirectory(entry, numTabs + 1);
- } else {
- // files have sizes, directories do not
- //Serial1.print("\t\t");
- //Serial1.println(entry.size(), DEC);
- }
- entry.close();
- Serial1.println("");
- }
- }
- void readFile(String fileName, File myFile) {
- }
- String getValue(String data, char separator, int index)
- {
- int found = 0;
- int strIndex[] = {0, -1};
- int maxIndex = data.length() - 1;
- for (int i = 0; i <= maxIndex && found <= index; i++) {
- if (data.charAt(i) == separator || i == maxIndex) {
- found++;
- strIndex[0] = strIndex[1] + 1;
- strIndex[1] = (i == maxIndex) ? i + 1 : i;
- }
- }
- return found > index ? data.substring(strIndex[0], strIndex[1]) : "";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement