This is because the output of the serial is buffered. It's still sending the first bit of data while you change the baud rate - and from then on it's just a complete mess.
You need to force it to finish sending before you can change the baud rate by using Serial.flush():
void setup() { Serial.begin(115200); Serial.println("Hello"); Serial.flush(); }
void loop() { Serial.begin(115200); Serial.println("world"); Serial.flush(); Serial.begin(9600); Serial.println("world"); Serial.flush(); }
void setup() {
Serial.begin(115200);
Serial.println("Hello");
Serial.flush();
}
void loop() {
Serial.begin(115200);
Serial.println("world");
Serial.flush();
Serial.begin(9600);
Serial.println("world");
Serial.flush();
}
That will wait after each print until all the data is sent out "onto the wire" before changing the baud rate for the next print.