c-Arduino:从SD卡读取到字符阵列,然后保存到SD卡



我正在与Arduino和Lora合作。Arduino A有一个摄像头,Lora收音机和SD卡。Arduino B有相同的设置减去相机。计划是在Arduino a上拍摄一张照片,并将其保存到SD卡,然后读取并通过Lora发送到Arduino B,后者将在自己的SD卡上保存信息。

我目前的里程碑是读取SD卡中的测试文本文件,并将其保存为同一张卡上的副本,所有这些都在Arduino a.中

这使用标准SD库逐字节读取和写入,但速度太慢。

我想读取100个字节(最终为512(并将其保存到缓冲区(char数组(,在一条写入指令中将这100个字节写入SD卡,然后重复,直到文件完全写入。

我无法将文本分割成块,尤其是当文件中的字节数少于100时。C.代码如下。非常感谢。

oFile = SD.open("message.txt", FILE_READ); // Original file
cFile = SD.open("message2.txt", FILE_WRITE); // Copy
int cIndex = 0;
char cArray[101]; // Buffer for the blocks of data
int cByteTotal = oFile.available(); // Get total bytes from file in SD card.
for(int i=0; i < cByteTotal ; i++){ // Looping
cArray[cIndex] = (char)myFile.read(); //Cast byte as char and save to char array
if (cIndex == 100 ){ // Save 100 bytes in buffer
cArray[cIndex + 1] = ''; // Terminate the array?
Serial.println(String(cArray)); // Print to console
cFile.write(cArray); // Save 100 bytes to SD card.
memset(cArray, 0, sizeof(cArray)); // Clear the array so that its ready for next block
cIndex = -1; // Reset cIndex for next loop
}
if (cByteAvailable <= 99){ // When cByteTotal is less than 100...
Serial.print("Last block");
}
cIndex++;
}
oFile.close();
cFile.close();

从SD.read((文档中,我建议使用read(buf, len)格式每次最多读取100个字节。类似于SD.write((

int cByteTotal = oFile.size();
while (cByteTotal > 0) {
int length = cByteTotal < 100 ? cByteTotal : 100;
if (length < 100) {
Serial.print("Last block");
}
oFile.read(cArray, length);
cFile.write(cArray, length);
cByteTotal -= length;
}
oFile.close();
cFile.close();