If you have convincing evidence that flush() does not work with Serial2, post an issue with the developers. They have quite a mountain of them to work through already.
Flush is working on my sketch for serial2!
Thanks for the tip, I was just getting cresy trying to find a solution for "serialsoftware" limitation to one istance.
... Now I'm short of 1 analog input, just need to find a solution ...
flush blocks to wait for outgoing data. Since you aren't sending anything on Serial2 the flush here is doing nothing. There is no purpose if you aren't sending outgoing data.
I thought It removes any buffered incoming serial data... as the word would also suggest. That's fine...I'll delete It and check for correct functionality. Thanks guys
The only place where flush means that is in toilets. In just about everything computer related it means to wait while data goes out that would normally be async.
For example flushing a file means stopping while everything left in the write buffer gets written out to the file.
For what it is worth, early on in Arduino days, Serial2.flush() was defined to do this. In releases like 030... It changed when Arduino released version 1.0... It took a while before several of the board types I used back then were updated to this.
Note: Some boards have an equivalent method to do this, for example on a Teensy 4.x you
could do this like: Serial2.clear();
But there are no standards for a method to do this.
So, I typically revert to something like: while (Serial2.read() != -1) {}
Processing 'language' has Stream.clear() and Arduino was created 'emulating' Processing in C++. So yes clear is the right name. I recently used it for ESP32 WiFi Client to rename flush which was still clearing input.
There has been some improvement to flush() with the latest board update (1.1.0). Serial2 now returns 2 bytes early and Serial1 only 1 byte early. So you can now use a small delay to be sure the buffer is empty.
Have you looked at the code for this? I wouldn't expect it to work.
flush just waits until there is nothing available in the tx buffer. THE CODE NEVER EVEN USES THE TX BUFFER! Nothing ever goes into it. So available on it always returns false.
On the R4 with UART serial, flush literally does nothing. It might as well be implemented as return false;
Exactly...
And the flush() function does not help, other than it takes some time for it to call all of the layers of code that do nothing... So maybe you get closer.
Why is this the case?
The UART is not configured to use it's output FIFO (SCI0 and 1 have FIFO, but probably not used here or you would probably lose a lot more.
So instead it uses double buffering. That is there is an output shift register TSR, that outputs the data, but the code outputs to the Transmit Data Register (TDR). When the TSR finishes outputting the current character, it looks for data in the TDR and moves it into the TSR and the status of the register now says TDR is empty, so place a new character in it. So in your case if you are outputting: ABCD
When the C character is finished shifting out the D is moved into the TSR, we place the CR into the TDR and the Serial2.write code goes I have nothing more to output and returns...
EDIT: Adding insult to injury: (Unless changed recently)
If your code was like:
Serial2.print("ABCD");
Serial2.println();
The code that starts up to do your current output does not first check to see
if the TDR register is empty and blindly puts the first character of the next print into
the TDR...
So sometimes depending on timing, your might only see: ABC