Is it OK to put serial.write in setup?

I'm not sure I understand the behavior here.

#include <Arduino.h>

void setup() {
    Serial.begin(115200);
    Serial.write(0x06);
    Serial.flush();
}

void loop() {
  char command;
    // put your main code here, to run repeatedly:
  if(Serial.available() > 0){
    Serial.print("FOOBAR");
    command=Serial.read();
    Serial.write(command);
  }
}

So I want to write an ACK byte when the serialport is ready, but randomly part of FOOBAR is written first. Does the loop async with setup? Does serial write to a buffer async? I thought flush would wait for that buffer to finish. Is my computer sending some rando bytes when the serial port is connected and is there a way to flush those first?

Can you try to illustrate the problem a little better? What do you see and where and under what conditions? Sometimes when you open the serial monitor and the board resets you see a little bit of what was running before show up. Is that what you are seeing?

The setup and loop are just normal functions that are called from the main function in main.cpp. They are called in order, setup first and then loop over and over in an infinite for loop. There are nothing special about the setup function that would cause any other function to not work there but work somewhere else.

When I first connect to the serial port sometimes I see
"FOO"

with no ACK until I press a key

Sometimes I see "FOO"
and then ACK a second later

Sometimes I see "FOO<?>" with some unreadable character

Sometimes I see more or less of the whole word "FOOBAR"

I'm expect to only see ACK until I press a key(send something to the board)

I don't want the board to spit out anything, other than ACK

Why would there be something in Serial.available() when the board resets and is there a way to clear that in the setup function?

When you connect to serial from what? The serial monitor in the IDE? Please think about the fact that I haven't been there with you all along. This is my first look at this project. Please put a little thought into getting some details out there.

When you connect to the serial port on the Arduino you are resetting the board. But you may not be resetting the serial connection on the other end. So it may be showing you thing that were sent BEFORE you caused the reset of the Arduino. Like this. Arduino is running and is in the middle of sending FOOBAR. It's got the FOO out as you open the serial monitor. You computer has received the F, O, and O already and they're waiting in some buffer to be read. As the serial monitor opens, it resets the Arduino. The serial monitor starts checking to see if anything has arrived and there is that F, O, and O still sitting there that had already arrived but hadn't been read out yet. So it reads them out of the buffer and sends them to the screen. Meanwhile Arduino has finished resetting and sends the ACK.

Put about a ten or twenty second delay at the beginning of setup and see if you aren't getting the FOO first and then ten second later get the ACK from the new run of the program.

I'm using platformio's ide and serial monitor, but I've also tried connecting to the board straight through putty and screen and got the same behavior.

I think I understand what you're saying.
When I connect through putty or screen the board resets, but it might continue running the last few lines before it resets? Which says to me that connecting to the board must send some kind of ACK SYN sequence that triggers the FOOBAR loop.

JellyToast:
I think I understand what you're saying.
When I connect through putty or screen the board resets, but it might continue running the last few lines before it resets?

Not quite. It's not that it runs any more line before it resets, it's that it already ran those lines and sent that data to your computer but your computer hadn't completely received it yet. It got the data and it was waiting in a buffer somewhere to be read out, but hadn't been read out yet. The computer doesn't know what got sent before or after the reset. The computer doesn't even know that the board is supposed to reset. It just knows that it read some data into a buffer and now some program (the serial monitor or putty) want's to see it.

I usually correct for this by sending a few newlines and a small delay at the beginning of most of my programs if I am going to use a serial monitor. That way any reset is plain to see by the blank lines and short wait and I can separate where the new run starts.