Some Serial ports Support additional Pins that indicate a physical Connection ("Terminal ready"). To Keep the number of Pins to a Minimum, the Arduino Serial port does NOT Support those Pins. Detecting if something is attached or not is therefore a function that Needs to be handled in the Software and is usually done by sending out a Signal and expecting a Response. Much like "Hi, how are you?" and if the guy wants to talk he'll answer with "Fine and yourself?"
HardwareSerial *SerialInUse;
void setup()
{
Serial.begin(115200);
while (!Serial && millis() < 5000); // Give USB up to five seconds to connect
if (!Serial)
{
Serial1.begin(115200);
SerialInUse = Serial1; // UART
}
else
SerialInUse = Serial; // USB
SerialInUse->println(F("A serial port has been chosen!"));
}
neiklot:
Serial's always true except on micro and Leo
And on UNO/Nano/Mini there is no Serial1. On MEGA it will assume you always want to use Serial because there is no way to know if the PC is connected or not.