Serial is connected check

Hi all
I have a project that has two arduinos communicating via serial.

I have the communication part working fine, however im looking for a way to detect if serial is connected or not.

my code has checks built in that I want to bypass if the two devices are not connected, skipping out a bunch of code that would slow things down while waiting for responses that will never come.

Something like
if(serial(connected)){
//do serial processing
}
//rest of code

Any ideas?
Thanks

The only way to know that Serial is connected is to receive a message.

There is no need to spend time waiting for a message. Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

...R

Connect a 1k Ohm resistor between the Tx and Rx pins of Arduino #1.

If there is nothing connected, you can send a test character and it will "loop back" and be received by that Arduino.

If Arduino #2 is connected, its Rx output, being significantly lower resistance than the 1k loop back resistor, will dominate and Arduino #1 will receive only characters sent by Arduino #2.

Your software will have to insure that the loop back characters are distinguishable from the characters sent by the other Arduino.

Edit to add: This will interfere with programing the Arduino via USB (assuming Uno or equivalent), so the loop back resistor must be disconnected during programming.