我和Arduino和Lora一起工作。Arduino A有照相机,劳拉收音机和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] = '\0'; // 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();
发布于 2022-11-05 00:55:04
从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();
https://stackoverflow.com/questions/74323166
复制相似问题