Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.12 KB | None | 0 0
  1.  
  2. #include <SD.h>
  3. #include <SPI.h>
  4.  
  5. const int chipSelect = 4;
  6. String incomingByte;
  7. String lastTwo;
  8. int len;
  9. File root;
  10.  
  11.  
  12. void setup() {
  13. // Open serial communications and wait for port to open:
  14. Serial1.begin(9600);
  15. Serial.begin(9600);
  16.  
  17. while (!Serial) {
  18. ; // wait for serial port to connect. Needed for native USB port only
  19. }
  20. root = SD.open("/");
  21. Serial.println("");
  22. Serial1.print("OK");
  23. Serial.println("");
  24.  
  25. // // see if the card is present and can be initialized:
  26. // if (!SD.begin(chipSelect)) {
  27. // Serial.println("Card failed, or not present");
  28. // // don't do anything more:
  29. // while (1);
  30. // }
  31. // Serial1.println("");
  32. //
  33. // Serial1.println("card initialized.");
  34. // //delay(2000);
  35.  
  36. // open the file. note that only one file can be open at a time,
  37. // so you have to close this one before opening another.
  38. // Serial.println("Lecture de /DATA/2019/0619.csv.");
  39. // //delay(2000);
  40. // File dataFile = SD.open("/DATA/2019/06.csv");
  41. //
  42. // // if the file is available, write to it:
  43. // if (dataFile) {
  44. // while (dataFile.available()) {
  45. // Serial1.write(dataFile.read());
  46. // //delay(200);
  47. // }
  48. // dataFile.close();
  49. // }
  50. // // if the file isn't open, pop up an error:
  51. // else {
  52. // Serial.println("error opening datalog.txt");
  53. // }
  54. }
  55.  
  56. void loop() {
  57.  
  58. incomingByte = "";
  59. String read = "read";
  60.  
  61. String dump = "dump";
  62.  
  63. int fileNames_nbr = 0;
  64.  
  65. while (Serial1.available() > 0) {
  66.  
  67. incomingByte += char(Serial1.read());
  68.  
  69.  
  70. if (incomingByte != "")
  71. {
  72. Serial.print(incomingByte);
  73.  
  74.  
  75. }
  76.  
  77. // Serial.println("incomingByte: "+incomingByte);
  78. delay(10);
  79.  
  80. if (incomingByte == "list") {
  81.  
  82. //Serial1.println("Contenu de la carte SD :");
  83. //Serial1.println("");
  84. delay(500);
  85. Serial.print("Initializing SD card...");
  86.  
  87. if (!SD.begin(chipSelect)) {
  88. Serial.println("initialization failed!");
  89.  
  90. }
  91. Serial.println("initialization done.");
  92.  
  93. root = SD.open("/DATA");
  94.  
  95. printDirectory(root, 0);
  96.  
  97. //Serial1.println("end");
  98.  
  99. }
  100.  
  101.  
  102.  
  103. if (incomingByte.indexOf("read ") >= 0) {
  104. while (Serial1.available() > 0) {
  105. incomingByte += char(Serial1.read());
  106. }
  107. if (!SD.begin(4)) {
  108. Serial.println("initialization failed!");
  109. while (1);
  110. }
  111.  
  112. String fileName;
  113. char fileNamestr[incomingByte.length() - 5];
  114. //Serial1.println("Lecture d'un fichier");
  115. Serial1.println("");
  116. fileName = incomingByte.substring(5);
  117.  
  118. //Serial1.println("filename:" + fileName);
  119. //Serial1.println("incoming byte: " + incomingByte);st
  120. fileName.toCharArray(fileNamestr, sizeof(fileNamestr));
  121. File myFile = SD.open(fileNamestr);
  122. if (myFile) {
  123. //Serial1.println("Lecure de " + fileName);
  124.  
  125. // read from the file until there's nothing else in it:
  126. while (myFile.available()) {
  127. Serial1.write(myFile.read());
  128. //Serial1.println("Lecure en cours");
  129. //Serial1.println("myFile:"+myFile);
  130. }
  131. // close the file:
  132. myFile.close();
  133. incomingByte = "";
  134. Serial1.println("");
  135. //Serial1.println("end");
  136. } else {
  137. // if the file didn't open, print an error:
  138. Serial1.println("Ce fichier n'existe pas");
  139. Serial1.println("");
  140. //Serial1.println("myFile:"+myFile);
  141. incomingByte = "";
  142. }
  143.  
  144.  
  145. }
  146.  
  147.  
  148. if (incomingByte.indexOf("dump ") >= 0) {
  149. while (Serial1.available() > 0) {
  150. incomingByte += char(Serial1.read());
  151. }
  152.  
  153.  
  154. String fileNames;
  155.  
  156. char fileNamesstr[incomingByte.length() - 5];
  157. //Serial1.println("Lecture d'un fichier");
  158. Serial1.println("");
  159.  
  160. fileNames = incomingByte.substring(5);
  161.  
  162. fileNames.toCharArray(fileNamesstr, sizeof(fileNamesstr));
  163.  
  164. for (int i = 0; i < strlen(fileNamesstr); i++) {
  165.  
  166. if (fileNames[i] == ';')
  167. {
  168. fileNames_nbr++;
  169. }
  170. }
  171. Serial1.write("Nombre de fichiers: ");
  172. Serial1.println(fileNames_nbr);
  173. //Serial1.println("Fichiers à traiter: ");
  174.  
  175.  
  176. String fichier="";
  177. if (!SD.begin(4)) {
  178. Serial.println("initialization failed!");
  179. while (1);
  180. }
  181.  
  182. for (int j = 0; j < fileNames_nbr + 1; j++) {
  183.  
  184. fichier = getValue(fileNames, ';', j);
  185.  
  186. //Serial1.println(fichier);
  187. //Serial1.println(fichiers[j]);
  188.  
  189.  
  190.  
  191.  
  192. File myFiled = SD.open(fichier);
  193. if (myFiled) {
  194. //Serial1.println("Lecure de " + fichier);
  195.  
  196. // read from the file until there's nothing else in it:
  197. while (myFiled.available()) {
  198. Serial1.write(myFiled.read());
  199. //Serial1.println("Lecture en cours");
  200. //Serial1.println("myFile:"+myFiled);
  201. }
  202. // close the file:
  203. myFiled.close();
  204. fichier="";
  205. }
  206. }
  207. }
  208. }
  209. }
  210.  
  211. void printDirectory(File dir, int numTabs) {
  212. while (true) {
  213.  
  214. File entry = dir.openNextFile();
  215. if (! entry) {
  216. // no more files
  217. break;
  218. }
  219. for (uint8_t i = 0; i < numTabs; i++) {
  220. Serial1.print('\t');
  221. }
  222. Serial1.print(entry.name());
  223. if (entry.isDirectory()) {
  224. //Serial1.println("/");
  225. printDirectory(entry, numTabs + 1);
  226. } else {
  227. // files have sizes, directories do not
  228. //Serial1.print("\t\t");
  229. //Serial1.println(entry.size(), DEC);
  230. }
  231. entry.close();
  232. Serial1.println("");
  233.  
  234. }
  235. }
  236.  
  237. void readFile(String fileName, File myFile) {
  238.  
  239.  
  240.  
  241. }
  242.  
  243. String getValue(String data, char separator, int index)
  244. {
  245. int found = 0;
  246. int strIndex[] = {0, -1};
  247. int maxIndex = data.length() - 1;
  248.  
  249. for (int i = 0; i <= maxIndex && found <= index; i++) {
  250. if (data.charAt(i) == separator || i == maxIndex) {
  251. found++;
  252. strIndex[0] = strIndex[1] + 1;
  253. strIndex[1] = (i == maxIndex) ? i + 1 : i;
  254. }
  255. }
  256.  
  257. return found > index ? data.substring(strIndex[0], strIndex[1]) : "";
  258. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement