Serial port long data issue

Hello there,
When i send the 88.9 kb text from computer to arduino, my program writes them into sd card. After that action, When i check the difference between the orjinal and copied file, i see they're not same. Also, when i check the two copied file, i see they not same either.

Code :

#define L_LOG F("/log/holla.xlg")

void setup() {
Serial.begin(9600);

pinMode(10, OUTPUT);
digitalWrite(10, HIGH);

SD.begin(4);
SD.remove(L_LOG);
}

void loop() {
File logFile = SD.open(L_LOG, O_CREAT | O_WRITE | O_APPEND);
if (logFile) {
while (Serial.available() > 0)
{
logFile.write(Serial.read());
}
logFile.close();
}
}

Original Content : http://pastebin.com/YX4SKan5
Copied Content by Arduino : Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam dictum neque in aug - Pastebin.com
Another Copied Content by Arduino : http://pastebin.com/RAx7FzNT

You can see the difference in here : https://www.diffchecker.com/e2bgjlqa

Can you tell me what the my problem is ? What am i missing ?

Board: Arduino Mega 2560 R3 + Arduino Ethernet Shiled (With 4 GB Empty SD Class 2)

File logFile = SD.open(L_LOG, O_CREAT | O_WRITE | O_APPEND);
if (logFile) {
while (Serial.available() > 0)
{
logFile.write(Serial.read());
}
logFile.close();
}

Open the file. Read any pending serial data, which is nowhere near the whole file, and write to the file. Close the file. Repeat, as the serial buffer overflows.

Not a good idea, really.

You need to send some character that means "Start storing this data in a file". You need to send the data. Then, you need to send some character that means "Stop storing the data in the file".

You should open the file, and spin in a while loop until the start character arrives. Then, you should spin in another while loop, reading and writing, until the end marker arrives.

PS. Pick up the pace. 9600 baud is a stone age speed.

File logFile = SD.open(L_LOG, O_CREAT | O_WRITE | O_APPEND);
if (logFile) {
int time = 0;
do
{
if (Serial.available() > 0) {
time = 0;
logFile.write(Serial.read());
}
else {
time++;
}
} while (time < 5000);
logFile.close();

I edited my codes as above. It's working properly now :slight_smile:
Thank you.