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.

112 satır
2.7KB

  1. // Function to read a CSV text file one field at a time.
  2. //
  3. #include <SPI.h>
  4. #include <SdFat.h>
  5. #define CS_PIN SS
  6. SdFat SD;
  7. File file;
  8. /*
  9. * Read a file one field at a time.
  10. *
  11. * file - File to read.
  12. *
  13. * str - Character array for the field.
  14. *
  15. * size - Size of str array.
  16. *
  17. * delim - String containing field delimiters.
  18. *
  19. * return - length of field including terminating delimiter.
  20. *
  21. * Note, the last character of str will not be a delimiter if
  22. * a read error occurs, the field is too long, or the file
  23. * does not end with a delimiter. Consider this an error
  24. * if not at end-of-file.
  25. *
  26. */
  27. size_t readField(File* file, char* str, size_t size, const char* delim) {
  28. char ch;
  29. size_t n = 0;
  30. while ((n + 1) < size && file->read(&ch, 1) == 1) {
  31. // Delete CR.
  32. if (ch == '\r') {
  33. continue;
  34. }
  35. str[n++] = ch;
  36. if (strchr(delim, ch)) {
  37. break;
  38. }
  39. }
  40. str[n] = '\0';
  41. return n;
  42. }
  43. //------------------------------------------------------------------------------
  44. #define errorHalt(msg) {Serial.println(F(msg)); SysCall::halt();}
  45. //------------------------------------------------------------------------------
  46. void setup() {
  47. Serial.begin(9600);
  48. // Wait for USB Serial
  49. while (!Serial) {
  50. SysCall::yield();
  51. }
  52. Serial.println("Type any character to start");
  53. while (!Serial.available()) {
  54. SysCall::yield();
  55. }
  56. // Initialize the SD.
  57. if (!SD.begin(CS_PIN)) errorHalt("begin failed");
  58. // Create or open the file.
  59. file = SD.open("READTEST.TXT", FILE_WRITE);
  60. if (!file) errorHalt("open failed");
  61. // Rewind file so test data is not appended.
  62. file.rewind();
  63. // Write test data.
  64. file.print(F(
  65. "field_1_1,field_1_2,field_1_3\r\n"
  66. "field_2_1,field_2_2,field_2_3\r\n"
  67. "field_3_1,field_3_2\r\n" // missing a field
  68. "field_4_1,field_4_2,field_4_3\r\n"
  69. "field_5_1,field_5_2,field_5_3" // no delimiter
  70. ));
  71. // Rewind the file for read.
  72. file.rewind();
  73. size_t n; // Length of returned field with delimiter.
  74. char str[20]; // Must hold longest field with delimiter and zero byte.
  75. // Read the file and print fields.
  76. while (true) {
  77. n = readField(&file, str, sizeof(str), ",\n");
  78. // done if Error or at EOF.
  79. if (n == 0) break;
  80. // Print the type of delimiter.
  81. if (str[n-1] == ',' || str[n-1] == '\n') {
  82. Serial.print(str[n-1] == ',' ? F("comma: ") : F("endl: "));
  83. // Remove the delimiter.
  84. str[n-1] = 0;
  85. } else {
  86. // At eof, too long, or read error. Too long is error.
  87. Serial.print(file.available() ? F("error: ") : F("eof: "));
  88. }
  89. // Print the field.
  90. Serial.println(str);
  91. }
  92. file.close();
  93. }
  94. //------------------------------------------------------------------------------
  95. void loop() {
  96. }