Losing Data at Serial Communication

wildbill:
I've read about folks using 300,000 baud successfully,

250,000 baud and 500,000 baud work without any timing errors on a 16MHz (and 8Mhz) Arduino.

Maybe the OP needs to try a different terminal program on his PC such as PuTTY or RealTerm ?

...R

@Robin2
I tried both, writing to file; stripped your code down to only print millis(). Still the same issue.

// Edit
Board is a Mega

sterretje:
@Robin2
I tried both, writing to file; stripped your code down to only print millis(). Still the same issue.

When I saw your Reply #15 I had assumed it was a demo of perfect working.

I presume, now, that I was wrong. What is wrong with the output in your Reply #15 ? What should it show?

...R

Look for the word missing

sterretje:
Look for the word missing

Oops. It had never occurred to me to scroll down :slight_smile:

I'll have another look at my code in the morning.

...R

I've just run another test. I could see no problems with the data displayed on the screen using the minicom terminal program.

I then told mincom to capture the data to a file and out of 11,496 lines there were only glitches on lines 371, 376 and 388.

...R

Glitches as in missing or as in duplicates; the latter is to be expected.

This is the output

180,154741
180,154742
180,788,180432
187,180433
191,180434
187,180435
186,180436
180427
189,180428
188,180428
188,180429
188,180430
188,180431
188,180432
187,180433
191,180434
187,180435
186,180436
186,180438
180428
188,180428
188,180429

...R

Just if anyone is interested, I also just tried to maximally send 4 digits for (up to 9998) the time variable to minimize the amount that needs to be sent. I did it with this code:

#define sf 1000 //change this for wanted sampling fq
//~ #define tc (1000/(sf))     // time constant
#define tc 1000
unsigned int ADC_Value = 0;    //ADC current value
unsigned long last_time = 0;
unsigned long t = 0;
unsigned long reset = 0;
void setup() {
  Serial.begin(500000);
}


// the loop routine runs over and over again forever:
void loop() {

  if (micros() - last_time >= tc) {
    last_time = micros();
    ADC_Value = analogRead(A0);
    
    // to not have more than 4 digits to send
    t = millis()-reset;
    if (t >= 9999){
      t = 1;
      reset = millis();
      }
    
    Serial.print(ADC_Value);
    Serial.print(',');
    Serial.print(t);
    Serial.println();
    }
}

I still got missing data. E.g.:

312,6712
311,6713
311,6714
3283
316,8284
315,8285
314,8286

I will probably just change the experiment so that I never have to collect data for longer than 20 seconds or so. Usually the problem starts not before 50s of data collection.

Maitschi95:
I still got missing data.

How often does that happen?

In my own test there were only 3 errors in over 11,000 lines and all the errors were close to each other and close to the start of the file.

...R

Maitschi95:
Just if anyone is interested, I also just tried to maximally send 4 digits for (up to 9998) the time variable to minimize the amount that needs to be sent. I did it with this code:

#define sf 1000 //change this for wanted sampling fq

//~ #define tc (1000/(sf))    // time constant
#define tc 1000
unsigned int ADC_Value = 0;    //ADC current value
unsigned long last_time = 0;
unsigned long t = 0;
unsigned long reset = 0;
void setup() {
  Serial.begin(500000);
}

// the loop routine runs over and over again forever:
void loop() {

if (micros() - last_time >= tc) {
    last_time = micros();
    ADC_Value = analogRead(A0);
   
    // to not have more than 4 digits to send
    t = millis()-reset;
    if (t >= 9999){
      t = 1;
      reset = millis();
      }
   
    Serial.print(ADC_Value);
    Serial.print(',');
    Serial.print(t);
    Serial.println();
    }
}




I still got missing data. E.g.:

312,6712
311,6713
311,6714
3283
316,8284
315,8285
314,8286

I will probably just change the experiment so that I never have to collect data for longer than 20 seconds or so. Usually the problem starts not before 50s of data collection.

That sounds like a practical solution. Or You could treat a miss of data as a swap to the next measurement sequence.