ReadCsvFile.ino 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #include "SdFat.h"
  2. // SD_FAT_TYPE = 0 for SdFat/File as defined in SdFatConfig.h,
  3. // 1 for FAT16/FAT32, 2 for exFAT, 3 for FAT16/FAT32 and exFAT.
  4. #define SD_FAT_TYPE 0
  5. /*
  6. Change the value of SD_CS_PIN if you are using SPI and
  7. your hardware does not use the default value, SS.
  8. Common values are:
  9. Arduino Ethernet shield: pin 4
  10. Sparkfun SD shield: pin 8
  11. Adafruit SD shields and modules: pin 10
  12. */
  13. // SDCARD_SS_PIN is defined for the built-in SD on some boards.
  14. #ifndef SDCARD_SS_PIN
  15. const uint8_t SD_CS_PIN = SS;
  16. #else // SDCARD_SS_PIN
  17. // Assume built-in SD is used.
  18. const uint8_t SD_CS_PIN = SDCARD_SS_PIN;
  19. #endif // SDCARD_SS_PIN
  20. // Try max SPI clock for an SD. Reduce SPI_CLOCK if errors occur.
  21. #define SPI_CLOCK SD_SCK_MHZ(50)
  22. // Try to select the best SD card configuration.
  23. #if HAS_SDIO_CLASS
  24. #define SD_CONFIG SdioConfig(FIFO_SDIO)
  25. #elif ENABLE_DEDICATED_SPI
  26. #define SD_CONFIG SdSpiConfig(SD_CS_PIN, DEDICATED_SPI, SPI_CLOCK)
  27. #else // HAS_SDIO_CLASS
  28. #define SD_CONFIG SdSpiConfig(SD_CS_PIN, SHARED_SPI, SPI_CLOCK)
  29. #endif // HAS_SDIO_CLASS
  30. #if SD_FAT_TYPE == 0
  31. SdFat sd;
  32. File file;
  33. #elif SD_FAT_TYPE == 1
  34. SdFat32 sd;
  35. File32 file;
  36. #elif SD_FAT_TYPE == 2
  37. SdExFat sd;
  38. ExFile file;
  39. #elif SD_FAT_TYPE == 3
  40. SdFs sd;
  41. FsFile file;
  42. #else // SD_FAT_TYPE
  43. #error Invalid SD_FAT_TYPE
  44. #endif // SD_FAT_TYPE
  45. char line[40];
  46. //------------------------------------------------------------------------------
  47. // Store error strings in flash to save RAM.
  48. #define error(s) sd.errorHalt(&Serial, F(s))
  49. //------------------------------------------------------------------------------
  50. // Check for extra characters in field or find minus sign.
  51. char* skipSpace(char* str) {
  52. while (isspace(*str)) str++;
  53. return str;
  54. }
  55. //------------------------------------------------------------------------------
  56. bool parseLine(char* str) {
  57. char* ptr;
  58. // Set strtok start of line.
  59. str = strtok(str, ",");
  60. if (!str) return false;
  61. // Print text field.
  62. Serial.println(str);
  63. // Subsequent calls to strtok expects a null pointer.
  64. str = strtok(nullptr, ",");
  65. if (!str) return false;
  66. // Convert string to long integer.
  67. int32_t i32 = strtol(str, &ptr, 0);
  68. if (str == ptr || *skipSpace(ptr)) return false;
  69. Serial.println(i32);
  70. str = strtok(nullptr, ",");
  71. if (!str) return false;
  72. // strtoul accepts a leading minus with unexpected results.
  73. if (*skipSpace(str) == '-') return false;
  74. // Convert string to unsigned long integer.
  75. uint32_t u32 = strtoul(str, &ptr, 0);
  76. if (str == ptr || *skipSpace(ptr)) return false;
  77. Serial.println(u32);
  78. str = strtok(nullptr, ",");
  79. if (!str) return false;
  80. // Convert string to double.
  81. double d = strtod(str, &ptr);
  82. if (str == ptr || *skipSpace(ptr)) return false;
  83. Serial.println(d);
  84. // Check for extra fields.
  85. return strtok(nullptr, ",") == nullptr;
  86. }
  87. //------------------------------------------------------------------------------
  88. void setup() {
  89. Serial.begin(9600);
  90. // Wait for USB Serial
  91. while (!Serial) {
  92. yield();
  93. }
  94. Serial.println("Type any character to start");
  95. while (!Serial.available()) {
  96. yield();
  97. }
  98. // Initialize the SD.
  99. if (!sd.begin(SD_CONFIG)) {
  100. sd.initErrorHalt(&Serial);
  101. return;
  102. }
  103. // Remove any existing file.
  104. if (sd.exists("ReadCsvDemo.csv")) {
  105. sd.remove("ReadCsvDemo.csv");
  106. }
  107. // Create the file.
  108. if (!file.open("ReadCsvDemo.csv", FILE_WRITE)) {
  109. error("open failed");
  110. }
  111. // Write test data.
  112. file.print(
  113. F("abc,123,456,7.89\r\n"
  114. "def,-321,654,-9.87\r\n"
  115. "ghi,333,0xff,5.55"));
  116. // Rewind file for read.
  117. file.rewind();
  118. while (file.available()) {
  119. int n = file.fgets(line, sizeof(line));
  120. if (n <= 0) {
  121. error("fgets failed");
  122. }
  123. if (line[n - 1] != '\n' && n == (sizeof(line) - 1)) {
  124. error("line too long");
  125. }
  126. if (!parseLine(line)) {
  127. error("parseLine failed");
  128. }
  129. Serial.println();
  130. }
  131. file.close();
  132. Serial.println(F("Done"));
  133. }
  134. void loop() {}