2016-05-04 22 views
1

私の最終的な目標は、XBEEを介して別のarduinoに30 KBのファイルを送信することです。しかし今のところ私は最初のarduinoに接続されたSD上に4KBのファイルを複製しようとしています。最初に、データを1バイトずつ送信しようとしました.1つのbyte.itが処理され、ファイルが正常に複製されました。私はバッファを持っていなければならないし、64バイトパケットのデータを64バイトのパケットで読み書きできるようにXBEEにデータを送る必要があります。arduinoでのファイル転送

#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() { 

} 

それが正常に100%まで、その進捗状況を終えるが、私はラップトップ上でSDを開くと、ファイルが作成されますが、それが0キロバイトのファイルとして表示:これは私がやっていることです。

何が問題ですか?

+0

は、コメントを追加: 私はちょうど行を追加: endFile.closeを(); 出力ファイルは2 KBで、破損しています。ソースファイルは3 KBです。 – alireza

答えて

2

.writeあなたのバッファの長さは何であるかわからないので、それはヌル終端文字列ではないと思うでしょう。

さらに、内側のループは、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); 
} 
+0

ありがとうございました... Uは私を助けました:)これは、ファイルが正常に複製されるようになりました。 – alireza