You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

преди 10 години
преди 10 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * This example reads a simple CSV, comma-separated values, file.
  3. * Each line of the file has three values, a long and two floats.
  4. */
  5. #include <SPI.h>
  6. #include <SdFat.h>
  7. // SD chip select pin
  8. const uint8_t chipSelect = SS;
  9. // file system object
  10. SdFat sd;
  11. // create Serial stream
  12. ArduinoOutStream cout(Serial);
  13. char fileName[] = "TESTFILE.CSV";
  14. //------------------------------------------------------------------------------
  15. // store error strings in flash to save RAM
  16. #define error(s) sd.errorHalt(F(s))
  17. //------------------------------------------------------------------------------
  18. // read and print CSV test file
  19. void readFile() {
  20. long lg;
  21. float f1, f2;
  22. char text[10];
  23. char c1, c2, c3; // space for commas.
  24. // open input file
  25. ifstream sdin(fileName);
  26. // check for open error
  27. if (!sdin.is_open()) error("open");
  28. // read until input fails
  29. while (1) {
  30. // Get text field.
  31. sdin.get(text, sizeof(text), ',');
  32. // Assume EOF if fail.
  33. if (sdin.fail()) break;
  34. // Get commas and numbers.
  35. sdin >> c1 >> lg >> c2 >> f1 >> c3 >> f2;
  36. // Skip CR/LF.
  37. sdin.skipWhite();
  38. if (sdin.fail()) error("bad input");
  39. // error in line if not commas
  40. if (c1 != ',' || c2 != ',' || c3 != ',') error("comma");
  41. // print in six character wide columns
  42. cout << text << setw(6) << lg << setw(6) << f1 << setw(6) << f2 << endl;
  43. }
  44. // Error in an input line if file is not at EOF.
  45. if (!sdin.eof()) error("readFile");
  46. }
  47. //------------------------------------------------------------------------------
  48. // write test file
  49. void writeFile() {
  50. // create or open and truncate output file
  51. ofstream sdout(fileName);
  52. // write file from string stored in flash
  53. sdout << pstr(
  54. "Line 1,1,2.3,4.5\n"
  55. "Line 2,6,7.8,9.0\n"
  56. "Line 3,9,8.7,6.5\n"
  57. "Line 4,-4,-3.2,-1\n") << flush;
  58. // check for any errors
  59. if (!sdout) error("writeFile");
  60. sdout.close();
  61. }
  62. //------------------------------------------------------------------------------
  63. void setup() {
  64. Serial.begin(9600);
  65. while (!Serial) {} // wait for Leonardo
  66. cout << pstr("Type any character to start\n");
  67. while (Serial.read() <= 0) {}
  68. delay(400); // catch Due reset problem
  69. // initialize the SD card at SPI_HALF_SPEED to avoid bus errors with
  70. // breadboards. use SPI_FULL_SPEED for better performance
  71. if (!sd.begin(chipSelect, SPI_HALF_SPEED)) sd.initErrorHalt();
  72. // create test file
  73. writeFile();
  74. cout << endl;
  75. // read and print test
  76. readFile();
  77. cout << "\nDone!" << endl;
  78. }
  79. void loop() {}