Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
File 使用arduino进行文件传输_File_Arduino - Fatal编程技术网

File 使用arduino进行文件传输

File 使用arduino进行文件传输,file,arduino,File,Arduino,我的最终目标是通过XBEE向另一个arduino发送一个30KB的文件。但现在我只是想复制一个连接到第一个arduino的SD上的4KB文件。首先,我尝试一个字节一个字节地发送数据。它工作正常,文件复制成功。但我必须有一个缓冲区,然后将64字节数据包中的数据发送到XBEE,这样我就可以读写64字节数据包中的文件。这就是我所做的: #include <SD.h> #include <SPI.h> void setup() { Serial.begin(115200);

我的最终目标是通过XBEE向另一个arduino发送一个30KB的文件。但现在我只是想复制一个连接到第一个arduino的SD上的4KB文件。首先,我尝试一个字节一个字节地发送数据。它工作正常,文件复制成功。但我必须有一个缓冲区,然后将64字节数据包中的数据发送到XBEE,这样我就可以读写64字节数据包中的文件。这就是我所做的:

#include <SD.h>
#include <SPI.h>

void setup() {

 Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
  }
 if (!SD.begin(4)) {

Serial.println("begin failed");
return;
   }

File file = SD.open("student.jpg",FILE_READ);
File endFile = SD.open("cop.jpg",FILE_WRITE);
 Serial.flush();

char buf[64];
if(file) {  

while (file.position() < file.size())
         { 
   while (file.read(buf, sizeof(buf)) == sizeof(buf))  // read chunk of 64bytes 
         {
        Serial.println(((float)file.position()/(float)file.size())*100);//progress %
        endFile.write(buf); // Send to xbee via serial
         delay(50); 
         }


      }
       file.close();
} 

 }
   void loop() {

}
#包括
#包括
无效设置(){
序列号开始(115200);
而(!串行){
;//等待串行端口连接。仅本机USB端口需要
}
如果(!SD.begin(4)){
Serial.println(“开始失败”);
返回;
}
File File=SD.open(“student.jpg”,File\u READ);
File endFile=SD.open(“cop.jpg”,File\u WRITE);
Serial.flush();
char-buf[64];
如果(文件){
while(file.position()
它成功地完成了它的进度,直到100%,但当我在笔记本电脑上打开SD时,该文件已创建,但显示为0 KB的文件


有什么问题吗?

你没有告诉
。写
你的缓冲区的长度是多少,所以它会认为它是一个以null结尾的字符串(它不是)

此外,内部循环似乎不仅不必要,甚至有害,因为如果最后一个块小于64字节,它将跳过最后一个块

看看这个:

while(file.position() < file.size()) {
    // The docs tell me this should be file.readBytes... but then I wonder why file.read even compiled for you?
    // So if readBytes doesn't work, go back to "read".
    int bytesRead = file.readBytes(buf, sizeof(buf));
    Serial.println(((float)file.position()/(float)file.size())*100);//progress %

    // We have to specify the length! Otherwise it will stop when encountering a null byte...
    endFile.write(buf, bytesRead); // Send to xbee via serial

    delay(50); 
}
while(file.position()
添加注释:我刚刚添加了一行:endFile.close();现在输出文件为2 KB且已损坏。但是源文件是3KB。谢谢你,伙计。。。你救了我:)现在可以了文件复制成功了