Mega stops streaming data to the computer

Hi all,

I have a system where I use a Mega to control an RS485 network, send data to an SD card, and stream data to a computer. For reasons unknown to me it randomly stops streaming data to the computer and SD card after 2 minutes. It keeps running the network but stops streaming data to the SD card and computer.

Does anybody have any ideas as to what causes this?

I’ve tried adding Serial.flush() after the Serial.print() functions to force the buffer to flush. That doesn’t seem to work. I’ve also tried reading data using Putty and Simulink and it exhibits the same response every time.

My guess is that the fault is with the Arduino code since it stops reading to the SD card at the same time.

Here is the code which I believe contains the problem.
The rest of the code in in an attachment because it is too long.

void IMUsave(float *data) {
  //Saves IMU data to data logger (Serial1) and also outputs to serial
  for (i = 0; i < 3; i++) {
    char charVal[10];
    dtostrf(data[i], 5, 2, charVal);
    Serial1.print(charVal);
    Serial1.print(",");
    Serial1.flush();
    Serial.print(charVal);
    Serial.print(",");
    Serial.flush();
  }
  for (i = 3; i < 6; i++) {
    char charVal[10];
    dtostrf(data[i], 8, 6, charVal);
    Serial1.print(charVal);
    Serial1.print(",");
    Serial1.flush();
    Serial.print(charVal);
    Serial.print(",");
    Serial.flush();
  }
  for (i = 6; i < 9; i++) {
    char charVal[10];
    dtostrf(data[i], 8, 3, charVal);
    Serial1.print(charVal);
    Serial1.print(",");
    Serial1.flush();
    Serial.print(charVal);
    Serial.print(",");
    Serial.flush();
  }
}

void CO2save(float data) {
  char charVal[10];
  dtostrf(data, 6, 3, charVal);
  Serial1.print(charVal);
  Serial1.print(",");
  Serial.print(charVal);
  Serial.print(",");
}

Control_box_for_deployment.ino (8.88 KB)

You don't seem to have any code in your readInstrument() function to prevent a buffer overflow. I have not looked for an equivalent problem elsewhere.

If this was my project I would define all the big arrays as globals so I would have more control over my use of memory.

...R