当前位置:   article > 正文

ESP32学习笔记-挂载SD卡读写TXT_esp32 读取sd卡

esp32 读取sd卡

写在前面

几经波折,终于达到预期,在此做个记录。

测试代码

  1. //加载库
  2. #include "Arduino.h"
  3. #include "FS.h"
  4. #include "SD.h"
  5. #include "SPI.h"
  6. //定义SD针脚
  7. #define TFT_SD D7
  8. void setup()
  9. {
  10. //开启串口
  11. Serial.begin(115200);
  12. //挂载SD
  13. MountSD();
  14. //路径
  15. String path1 = "/hello.txt";
  16. //内容
  17. String str1 = "hello world !";
  18. //写入(覆盖)
  19. writeTxt(SD,path1,str1);
  20. //路径
  21. String path2 = "/love.txt";
  22. //内容
  23. String str2 = "i love you !";
  24. //写入(续写)
  25. writeTxt_APPEND(SD,path2,str2);
  26. //读取1
  27. String txt1 = readTxt(SD,path1);
  28. Serial.println( txt1 );
  29. //读取2
  30. String txt2 = readTxt(SD,path2);
  31. Serial.println( txt2 );
  32. }
  33. void loop()
  34. {
  35. }
  36. //挂载SD
  37. void MountSD()
  38. {
  39. //开启SD
  40. if(SD.begin())
  41. {
  42. //打印成功
  43. Serial.println("Card Mount succeed");
  44. }
  45. else
  46. {
  47. //打印失败
  48. Serial.println("Card Mount Failed");
  49. //挂载失败,延时重启
  50. delay(1000);
  51. //重启
  52. return;
  53. }
  54. //获取SD类型
  55. uint8_t cardType = SD.cardType();
  56. switch(cardType)
  57. {
  58. case 1:
  59. Serial.println("CARD_MMC");
  60. break;
  61. case 2:
  62. Serial.println("CARD_SD");
  63. break;
  64. case 3:
  65. Serial.println("CARD_SDHC");
  66. break;
  67. case 4:
  68. Serial.println("CARD_UNKNOWN");
  69. break;
  70. default:
  71. Serial.println("CARD_NONE");
  72. Serial.println("Please insert SD card");
  73. delay(1000);
  74. return;
  75. }
  76. }
  77. //写入(FILE_WRITE
  78. void writeTxt(fs::FS &fs,String path,String str)
  79. {
  80. //打开文件
  81. File myFile = fs.open(path, FILE_WRITE); //FILE_WRITE 覆写
  82. //如果文件打开,则进行写入
  83. if (myFile)
  84. {
  85. //打印开始
  86. Serial.println("Start write:" + path);
  87. //写入
  88. myFile.println(str);
  89. // 关闭文件
  90. myFile.close();
  91. //打印完成
  92. Serial.println("Complete write:" + path);
  93. }
  94. //如果文件没有打开,则打印错误
  95. else
  96. {
  97. Serial.println("error:Write failure");
  98. }
  99. }
  100. //写入(FILE_APPEND)
  101. void writeTxt_APPEND(fs::FS &fs,String path,String str)
  102. {
  103. //打开文件
  104. File myFile = fs.open(path, FILE_APPEND); //FILE_APPEND 续写
  105. //如果文件打开,则进行写入
  106. if (myFile)
  107. {
  108. //打印开始
  109. Serial.println("Start write_APPEND:" + path);
  110. //写入
  111. myFile.println(str);
  112. // 关闭文件
  113. myFile.close();
  114. //打印完成
  115. Serial.println("Complete write_APPEND:" + path);
  116. }
  117. //如果文件没有打开,则打印错误
  118. else
  119. {
  120. Serial.println("error:Write failure");
  121. }
  122. }
  123. //读取
  124. String readTxt(fs::FS &fs,String path)
  125. {
  126. //创建返回值
  127. String txt;
  128. //打开文件
  129. File myFile = fs.open(path,FILE_READ); //FILE_READ 只读
  130. //如果文件打开,则进行读取
  131. if (myFile)
  132. {
  133. //打印开始
  134. Serial.println("Start Read:" + path);
  135. //读取
  136. if( myFile.available() )
  137. {
  138. txt = myFile.readString();
  139. }
  140. //关闭文件打开状态
  141. myFile.close();
  142. //打印完成
  143. Serial.println("Complete read:" + path);
  144. }
  145. //如果文件没有打开,则打印错误
  146. else
  147. {
  148. Serial.println("error:Read failure");
  149. }
  150. //返回
  151. return txt;
  152. }

测试结果

注意事项

1、确认SD卡针脚

2、fs.open(path,mode)函数,mode有多种方式

        FILE_WRITE       以可写的方式打开,覆写内容

        FILE_APPEND    以可写的方式打开,续写内容

        FILE_READ         以只读的方式打开

3、fs.readString()函数,直接读取文本,并返回String

参考

Arduino UNO:在TF卡中创建并读写TXT文件_arduino uno sd卡 error opening test.txt-CSDN博客

玩转 ESP32 + Arduino (二十四) SD卡读写-CSDN博客

DFR0654_FireBeetle_Board_ESP32_E (dfrobot.com.cn)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/煮酒与君饮/article/detail/977099
推荐阅读
相关标签
  

闽ICP备14008679号