Datalogger RS232 115200bps to SD card - Losing data

Hello all, I'm new to the Arduino community and this is my first post! I'm trying to build a Datalogger tool, which will have, basically, to record some (1 to 4) 115200bps RS232 data ports to a SD card.

From Hardware point-of-view, i bought a Due (4 UARTs), a Datalogging shield from Adafruit, and some RS232 to TTL converters (MAX3232)

I started by using a very simple code, which can be summarized as: if (Serial1.available() > 0 ) { DataByte = Serial1.Read(); Logfile.Write(DataByte); Logfile.flush }

The first tests, by emitting some data generated with my Keyboard (using Teraterm on PC) were successful, so i tried to send a complete file (binary) of 600 KB. Unfortunately, it does not work properly, the first few bytes are recorded properly, then i'm losing data. It seems i'm not losing bytes in the communication, but sometimes bits!. I also tried the Serial1.Readbytes command, but the problem is the same.

Possible reasons are: - Losing data at RS232-TTL converter... Don't think so, my wiring is very short, and the chip can handle 115200bps. - Losing data in the Due UART's due to Hardware/software bug? - Main loop too slow and UART buffer overflow? - SD card too slow?

Do you have any idea?

Thanks !!!

PS: I'm using Arduino 1.5.4 BETA (not the nightly builds)

It may be that the SD writes are slowing the sketch sufficiently to prevent it from keeping up with the incoming serial stream, and the serial buffer is occasionally overflowing. You could confirm that by removing the SD writes and do a simple byte count and checksum of the incoming serial data to see if what was received exactly matched what was sent. I expect that the SD card write would be much more efficient if you buffered a few dozen bytes and wrote them to the file in a single operation.

Thanks for your answer! You're right, in the meantime i tested only the reading and it seems all bytes are received, even at 115200bps. The SD.print command appears very slow. Even with RS232 at 9600bps, while this command runs, the UART buffer overflows.

Here's my simple "optimised" code. You can see that flush is disabled. I use logfile.print(DataBytes), where DataBytes is an Array of Char, ending with a null character (string). Is there a more efficient way to write to SD?

if (Serial1.available() > 0) {
  DataByte = Serial1.read();
  DataBytes[DataBytesInBuffer] = DataByte;
  DataBytesInBuffer++;
} 
if (Serial1.available() == 0 && DataBytesInBuffer > 0) {
  DataBytes[DataBytesInBuffer] = 0;
  nrOfBytesWritten = logfile.print(DataBytes);
  //nrOfBytesWritten = DataBytesInBuffer;
  //logfile.flush();
  DataBytesInBuffer = 0;
  TotalBytesWritten = TotalBytesWritten + nrOfBytesWritten;
  Serial.print(" / TotalWritten=");
  Serial.println(TotalBytesWritten, DEC);
}

PS: I found on the forum that using SD.open(filename, O_WRITE | O_CREAT); is faster, but it does not solve my problem.

How about writing a sketch that opens a data file and just times how long it takes to write a quantity of data to it? Once you have an idea what sort of write performance it is capable of achieving, you can work out whether this is a viable storage mechanism for your project.