1

I have written a 2 sets of code for the transmitter and receiver section. I am trying to send a word document file from 1 arduino uno to the next arduino uno through serial TX and RX. The file is initially loaded into a micro sd card module at the transmitter section. I would like to transfer that file to the receiver section's sd card module. I am able to receive an empty word document at the receiver section. There is no information in the document. I would like some help with this. I will attach the transmitter and receiver code below.

Transmitter code:

#include <SD.h>
#include <SPI.h>
File fileIn;
void setup() {
 // put your setup code here, to run once:
 Serial.begin(9600);
 while (!Serial){
 ; // wait for serial port to connect. Needed for native USB port only
 }
 Serial.print("Initializing SD card...");
 if(!SD.begin(4)){
 Serial.println(F("Initialization failed!"));
 return;
 } 
 Serial.println("Initialization done.");
 if(!SD.exists("student.txt")){
 fileIn = SD.open("student.txt", FILE_WRITE);
 fileIn.println(F("abc.txt"));
 }
 fileIn = SD.open("student.txt", FILE_READ);
 while(fileIn.available()){
 Serial.write(fileIn);
 }
 delay(1000);
 fileIn.close();
}
void loop() {
 // put your main code here, to run repeatedly:
}

Receiver code:

#include <SD.h>
#include <SPI.h>
File fileOut;
void setup() {
 // put your setup code here, to run once:
 Serial.begin(9600);
 while (!Serial){
 ; // wait for serial port to connect. Needed for native USB port only
 }
 Serial.print("Initializing SD card...");
 if(!SD.begin(4)){
 Serial.println(F("Initialization failed!"));
 return;
 }
 Serial.println("Initialization done.");
 if(SD.exists("student2.txt")){
 SD.remove("student2.txt");
 }
 fileOut = SD.open("student2.txt", FILE_WRITE);
 while(fileOut.available()){
 Serial.write(fileOut); 
 }
 delay(1000);
 fileOut.close();
 Serial.println("It worked!");
}
void loop() {
 // put your main code here, to run repeatedly:
}
AltAir
5424 silver badges15 bronze badges
asked Nov 4, 2017 at 6:37
0

1 Answer 1

1
 while(fileOut.available()){
 Serial.write(fileOut); 
 }

This doesn't quite do what you think it does.

Instead you should send the file length first and then do:

for(unsigned long size = 0; i< fileLength; i++){
 Serial.write(fileOut.read()); 
}

You should also investigate some kind of rate limiting to avoid overwhelming the reciever and having to drop some bytes.

answered Nov 4, 2017 at 11:27
2
  • Is it possible to read the line and take that as a file length in the 1st arduino and how would I implement this with the receiver arduino? Commented Nov 4, 2017 at 16:58
  • File has a size() member function. And just send 4 bytes with the length before you send the rest of the file. on the recieve side the first 4 bytes read will be the size of the file you will be recieving. Commented Nov 4, 2017 at 17:04

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.