system.flush vs system.println

Can someone shed some light on a query i have?

My interpretation of the documentation seems to suggest that

system.flush()

will pause following commands executing on a Arduino until all serial data has been transmitted.
Would that then mean that

system.print or system.println

operates in a asynchronous manner
i.e you call it, this copy's data to a send buffer and immediately starts executing other commands?

The reason i ask is that i'm writing a data-logger for Arduino that accepts inputs on the ADC pins and then outputs this to Java. Whilst i have a working prototype I am recording this data in a wave file.
To build the wave file i have to define the number of frames upfront (frames are derived from SMPS & duration).

Currently my sampling rate is 40 SMPS +- 2. Just about ok for Siesmometer work.

If possible i would like to get accurate timings of each time i call the serial command in order to get 40SMPS +- 0.

I have tried to measure the millis before and after each Serial transmission in order to work out a dynamic adjustment to pause the main loop the correct number of millis to ensure it triggers every 1/40 of a second however over 30 mins this is drifting by 20 - 30 seconds.

I may scrap txing data every loop of the program in favour of tx'ing a whole buffer similar to Girino if it seems that i cant get more accurate timings.

Code below for Arduino please note that the delay time was calibrated by measuring the time it took to execute the loop, i noted sometimes the serial transmission would take +- 1ms.

bool hello = false;
int inputA = 0;
int inputB = 0;
int inputC = 0;
int delayTime =0;
unsigned long millis1 = 0;
unsigned long millis2 = 0;

void setup() {
  Serial.begin( 115200 );    // start the serial port
  
}

void loop() {
  // put your main code here, to run repeatedly:
   
  if(hello ==false){    
      Serial.print("HELLO:");     
  } 
  
  String input = Serial.readStringUntil(':');
 
      if(hello==true){
          Serial.print("I:");
      }
      if(input == "HELLO"){
          hello = true;  
      }
      if(input == "RESET"){
          hello = false; 
      }
      if(input == "STREAM"){
          while(true){
              millis1 = millis();
              inputA = 0;
              inputB = 0;
              inputC = 0;
              inputA = analogRead(1);
              inputB = analogRead(2);
              inputC = analogRead(3);
              char buffer[9]= "";    
              Serial.print("DATA");
              Serial.print(",");    
              sprintf(buffer,"A%04d",inputA);
              Serial.print(buffer);
              Serial.print(",");
              sprintf(buffer,"B%04d",inputB);
              Serial.print(buffer);
              Serial.print(",");
              sprintf(buffer,"C%04d",inputC);
              Serial.print(buffer);
              Serial.print(":");
              millis2 = millis();
              
//              Serial.print("Time to complete:");
              delayTime = 25 - (millis2 - millis1);
//              Serial.println(delayTime);
              delay(delayTime);
          }
      }
  }

Would that then mean that

system.print or system.println

operates in a asynchronous manner
i.e you call it, this copy's data to a send buffer and immediately starts executing other commands?

Yes, that's exactly how it's implemented.

I have tried to measure the millis before and after each Serial transmission in order to work out a dynamic adjustment to pause the main loop the correct number of millis to ensure it triggers every 1/40 of a second however over 30 mins this is drifting by 20 - 30 seconds.

If you want a more accurate timing, don't use the delay() call to pause your sketch but wait in the main loop until millis() reached a specific value. See the BlinkWithoutDelay example of how you can achieve that.

Thankyou for your guidance i will go and have a look at that example.

As system.println is asynchronous I am now considering a buffered approach to sending serial data. I will use the Girino oscilloscope project as a example for a ring buffer of ints. It would mean that my Java code would need to check for serial bytes less often - hey i could even use a interrupt and free up some CPU cycles hence reduce power usage (My secondary reason for determining the system.print behaviour).

Is there a document / code example that i can read to figure out how the buffering works in Arduino?

At the moment I get some garbage at the start of the serial stream, I have coded around this in the Java app by having a protocol in place, however there are a couple of little quirks I would like to fix but need to understand the Arduino serial buffer better.

hey i could even use a interrupt and free up some CPU cycles hence reduce power usage

Be cautious, interrupts are a frequent source of problems here in the forum. Keep them short and fast and don't do anything you don't know what timing it has (as calling some libraries you're not familiar with). For example: Never call any method of the Serial object.

Is there a document / code example that i can read to figure out how the buffering works in Arduino?

You get the complete code with your IDE. hardware/arduino/avr/core/arduino/HardwareSerial.cpp
It's a quite simple circular buffer.

Thankyou for your help. I will retrofit the blink without delay method of timing into my code and let it loose this evening. Following that i will have a good read though of the buffer code.