Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

před 5 roky
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 to select the best SD card configuration.
  21. #if HAS_SDIO_CLASS
  22. #define SD_CONFIG SdioConfig(FIFO_SDIO)
  23. #elif ENABLE_DEDICATED_SPI
  24. #define SD_CONFIG SdSpiConfig(SD_CS_PIN, DEDICATED_SPI)
  25. #else // HAS_SDIO_CLASS
  26. #define SD_CONFIG SdSpiConfig(SD_CS_PIN, SHARED_SPI)
  27. #endif // HAS_SDIO_CLASS
  28. #if SD_FAT_TYPE == 0
  29. SdFat sd;
  30. File file;
  31. #elif SD_FAT_TYPE == 1
  32. SdFat32 sd;
  33. File32 file;
  34. #elif SD_FAT_TYPE == 2
  35. SdExFat sd;
  36. ExFile file;
  37. #elif SD_FAT_TYPE == 3
  38. SdFs sd;
  39. FsFile file;
  40. #else // SD_FAT_TYPE
  41. #error Invalid SD_FAT_TYPE
  42. #endif // SD_FAT_TYPE
  43. char line[40];
  44. //------------------------------------------------------------------------------
  45. // Store error strings in flash to save RAM.
  46. #define error(s) sd.errorHalt(&Serial, F(s))
  47. //------------------------------------------------------------------------------
  48. // Check for extra characters in field or find minus sign.
  49. char* skipSpace(char* str) {
  50. while (isspace(*str)) str++;
  51. return str;
  52. }
  53. //------------------------------------------------------------------------------
  54. bool parseLine(char* str) {
  55. char* ptr;
  56. // Set strtok start of line.
  57. str = strtok(str, ",");
  58. if (!str) return false;
  59. // Print text field.
  60. Serial.println(str);
  61. // Subsequent calls to strtok expects a null pointer.
  62. str = strtok(nullptr, ",");
  63. if (!str) return false;
  64. // Convert string to long integer.
  65. int32_t i32 = strtol(str, &ptr, 0);
  66. if (str == ptr || *skipSpace(ptr)) return false;
  67. Serial.println(i32);
  68. str = strtok(nullptr, ",");
  69. if (!str) return false;
  70. // strtoul accepts a leading minus with unexpected results.
  71. if (*skipSpace(str) == '-') return false;
  72. // Convert string to unsigned long integer.
  73. uint32_t u32 = strtoul(str, &ptr, 0);
  74. if (str == ptr || *skipSpace(ptr)) return false;
  75. Serial.println(u32);
  76. str = strtok(nullptr, ",");
  77. if (!str) return false;
  78. // Convert string to double.
  79. double d = strtod(str, &ptr);
  80. if (str == ptr || *skipSpace(ptr)) return false;
  81. Serial.println(d);
  82. // Check for extra fields.
  83. return strtok(nullptr, ",") == nullptr;
  84. }
  85. //------------------------------------------------------------------------------
  86. void setup() {
  87. Serial.begin(9600);
  88. // Wait for USB Serial
  89. while (!Serial) {
  90. yield();
  91. }
  92. Serial.println("Type any character to start");
  93. while (!Serial.available()) {
  94. yield();
  95. }
  96. // Initialize the SD.
  97. if (!sd.begin(SD_CONFIG)) {
  98. sd.initErrorHalt(&Serial);
  99. return;
  100. }
  101. // Remove any existing file.
  102. if (sd.exists("ReadCsvDemo.csv")) {
  103. sd.remove("ReadCsvDemo.csv");
  104. }
  105. // Create the file.
  106. if (!file.open("ReadCsvDemo.csv", FILE_WRITE)) {
  107. error("open failed");
  108. }
  109. // Write test data.
  110. file.print(F(
  111. "abc,123,456,7.89\r\n"
  112. "def,-321,654,-9.87\r\n"
  113. "ghi,333,0xff,5.55"));
  114. // Rewind file for read.
  115. file.rewind();
  116. while (file.available()) {
  117. int n = file.fgets(line, sizeof(line));
  118. if (n <= 0) {
  119. error("fgets failed");
  120. }
  121. if (line[n-1] != '\n' && n == (sizeof(line) - 1)) {
  122. error("line too long");
  123. }
  124. if (!parseLine(line)) {
  125. error("parseLine failed");
  126. }
  127. Serial.println();
  128. }
  129. file.close();
  130. Serial.println(F("Done"));
  131. }
  132. void loop() {
  133. }