Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

120 lines
2.6KB

  1. /*
  2. * This example reads a simple CSV, comma-separated values, file.
  3. * Each line of the file has a label and 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 = 0;
  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()) {
  28. error("open");
  29. }
  30. // read until input fails
  31. while (1) {
  32. // Get text field.
  33. sdin.get(text, sizeof(text), ',');
  34. // Assume EOF if fail.
  35. if (sdin.fail()) {
  36. break;
  37. }
  38. // Get commas and numbers.
  39. sdin >> c1 >> lg >> c2 >> f1 >> c3 >> f2;
  40. // Skip CR/LF.
  41. sdin.skipWhite();
  42. if (sdin.fail()) {
  43. error("bad input");
  44. }
  45. // error in line if not commas
  46. if (c1 != ',' || c2 != ',' || c3 != ',') {
  47. error("comma");
  48. }
  49. // print in six character wide columns
  50. cout << text << setw(6) << lg << setw(6) << f1 << setw(6) << f2 << endl;
  51. }
  52. // Error in an input line if file is not at EOF.
  53. if (!sdin.eof()) {
  54. error("readFile");
  55. }
  56. }
  57. //------------------------------------------------------------------------------
  58. // write test file
  59. void writeFile() {
  60. // create or open and truncate output file
  61. ofstream sdout(fileName);
  62. // write file from string stored in flash
  63. sdout << F(
  64. "Line 1,1,2.3,4.5\n"
  65. "Line 2,6,7.8,9.0\n"
  66. "Line 3,9,8.7,6.5\n"
  67. "Line 4,-4,-3.2,-1\n") << flush;
  68. // check for any errors
  69. if (!sdout) {
  70. error("writeFile");
  71. }
  72. sdout.close();
  73. }
  74. //------------------------------------------------------------------------------
  75. void setup() {
  76. Serial.begin(9600);
  77. // Wait for USB Serial
  78. while (!Serial) {
  79. SysCall::yield();
  80. }
  81. cout << F("Type any character to start\n");
  82. while (!Serial.available()) {
  83. SysCall::yield();
  84. }
  85. // Initialize at the highest speed supported by the board that is
  86. // not over 50 MHz. Try a lower speed if SPI errors occur.
  87. if (!sd.begin(chipSelect, SD_SCK_MHZ(50))) {
  88. sd.initErrorHalt();
  89. }
  90. // create test file
  91. writeFile();
  92. cout << endl;
  93. // read and print test
  94. readFile();
  95. cout << "\nDone!" << endl;
  96. }
  97. void loop() {}