Reading serial values in Processing - help optimize my code

I have an Arduino sending serial data. Its deeply embedded in a project of which I do not have the code.

If I use the serial monitor of my Arduino IDE I can see the data. Its real time and snappy.

I tried writing some code so I can use this data in Processing, but it lags behind by ~500ms and seems to come in chunks. It also appears to skip readings.

What am I doing wrong?

Here is my code:

import processing.serial.*; //import the Serial library

char end = '\n';    // /nl is the end of the message
String incomingString;   // the string which is read
Serial port;  // The serial port
String[] a;

void setup() {


  port = new Serial(this, Serial.list()[1], 9600); 
  incomingString = port.readStringUntil(end);
  port.clear();  // function from serial library that throws out the first reading, in case we started reading in the middle of a string from Arduino
  incomingString = null;

}

void draw() {
  while (port.available () > 0) { //as long as there is data coming from serial port, read it and store it 
    incomingString = port.readStringUntil(end);
  
  }
  if (incomingString != null) {  //if the string is not empty  
    a = split(incomingString, ", ");  
    //this is done in order to remap the incoming values so they are in the apropreate order
 
    //print the values for debugging

    for (int i = 0; i<8; i++) {
      print(a[i]);
      print(", ");
    }
    println();
  }

}

Unlike the loop() function on the Arduino, or the read loop in the Serial Monitor application, the draw() function in Processing is NOT called again and again, as fast as possible. It is called as often as needed to maintain a specified frame rate.

That makes it less than ideal for systems that need to process serial data as fast as possible.

The serialEvent() method IS called as soon as there is serial data to read (or when the serial data contains the character specified by the bufferUntil() method).

Since your Processing application isn't actually drawing anything, based on the serial data, processing the serial data in draw() is the wrong approach.

cool. I did not know that - learnt something useful. thank you.