我有一个Arduino项目,我需要在SD卡上写一个JSON文件。
我正在使用arduino的SD库。如果我在 .txt 文件中编写我的内容,我没有遇到任何问题,但如果我更改扩展名以放置 .json ,我就无法写入该文件。
这是工作测试代码:
#include <SD.h>
const int chipSelect = 4;
void setup()
{
Serial.begin(115200);
Serial.print("Initializing SD card...");
// make sure that the default chip select pin is set to
// output, even if you don't use it:
pinMode(10, OUTPUT);
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
Serial.println("card initialized.");
}
void loop()
{
String dataString = "";
File dataFile = SD.open("datalog.txt", FILE_WRITE);
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
Serial.println(dataString);
}
else {
Serial.println("error opening datalog.txt");
}
}
这是代码无效的工作:
#include <SD.h>
const int chipSelect = 4;
void setup()
{
Serial.begin(115200);
Serial.print("Initializing SD card...");
// make sure that the default chip select pin is set to
// output, even if you don't use it:
pinMode(10, OUTPUT);
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
Serial.println("card initialized.");
}
void loop()
{
String dataString = "";
File dataFile = SD.open("datalog.json", FILE_WRITE);
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
Serial.println(dataString);
}
else {
Serial.println("error opening datalog.txt");
}
}
我不知道问题出在哪里
提前谢谢
托马斯