Serial buffer contains old data on new connection?

I am using arduino to send data to processing sketch. Arduino code:

// arduino
void setup()
{
  Serial.begin(38400);
  Serial.write('P');  Serial.write('P');  Serial.write('P');
}
void loop()
{
Serial.write(analogRead(0));
}
// processing
import processing.serial.*;
Serial Port;

void setup()
{
   Port = new Serial(this, Serial.list()[0], 38400);
   delay(500);
}
void draw()
{
  while(Port.available() == 0) delay(1);
    print(Port.read()+"\n");
}

While testing I restart processing sketch but never receive the first three ā€˜Pā€™ characters. The arduino is reset each time processing connects with = new Serial and the first three data should be PPP. I discovered that processing does not empty the serial buffer from the previous connection and when I am testing for PPP I am actually reading analog data from the previous run.

I tried this change to the code where I connect, stop, clear, reconnect.

// processing
import processing.serial.*;
Serial Port;

void setup()
{
   Port = new Serial(this, Serial.list()[0], 38400);
   delay(500);
   Port.stop();
   Port.clear();
   Port = new Serial(this, Serial.list()[0], 38400);
   delay(500);
}
void draw()
{
  while(Port.available() == 0) delay(1);
    print(Port.read()+"\n");
}

Now I consistently read the first three PPP every time I run the processing sketch. So this is a fix for the buffer containing old data when the sketch starts.

Is the serial buffer containing old data on new connection expected and normal behavior? I would think not, but I havenā€™t found any posts, complaints, or documentation that mentions this. Thanks for any insight or advice.

1 Like

Hello @YoSteve,

This will answer your question:

Examples:

// Arduino

int counter = 0;

void setup()
  {
  Serial.begin(38400);
  delay(400); // Delay before sending
  Serial.write('P');  Serial.write('P');  Serial.write('P');
  Serial.write('Q');  Serial.write('Q');  Serial.write('Q');
  }
  
void loop()
  {
  Serial.write(counter); // This will only send a byte which is 0 to 255!
  counter++;
  }
// Processing

import processing.serial.*;
Serial port;

void setup()
  {
  port = new Serial(this, Serial.list()[2], 38400);
  delay(200); // Some delay needed here to receive the leftovers < 400 delay in Arduino!
  println("D0:", port.available()); // Data available in buffer
  port.clear();
  println("D1:", port.available()); // Data available in buffer. Should be 0 after clearing!
  }

int count = 0;

void draw()
  {  
  while(port.available() > 0) // Reads entire buffer each cycle of draw
  //if (port.available() > 0)   // Only reads one byte each cycle of draw!
    {
    int i = port.read();
    print(i + " ");  // 80 is 'P', 81 is 'Q'
    count++;
    if (count>50) exit();
    }  
  }

I did something similar to you in past:

See ā€œProcessing Code 2ā€ in above:

I have different strategies now for receiving data depending on what I am interfacing to (not always an Arduino). I can use serial ports (there are 3 additional ones) on my Arduino Mega 2560 that do not reset when connecting.

If you are programming the device then you have full control over the code.
Try adding some handshaking to request the Arduino to send data.

References:

:)

glv, thank you for taking the time to share your experiences.

How to clear output buffer of Arduino Uno doesnā€™t help on Arduinoā€™s that reset on new connection. Maybe on Arduinoā€™s like Leonardo that donā€™t reset will require something more.

Your first example where you only use port.clear() after creating a new serial port does not reliably clear the buffer. Sometimes it works, sometimes not. If I rerun the processing sketch shortly after stopping it, it works, but if I wait more than about 20 seconds, the next time I run the processing sketch I get old data instead.

Your ā€˜Processing Code 2ā€™ is helpful and shows that I didnā€™t need to call port.clear(). Calling port.stop() seems to clear the buffer reliably.

I agree that handshaking will help and I am adding next.