Why does "if(Serial)" returns true even after "Serial.end();"?

So I have a very simple sketch. I have a buzzer connected to pin 12 and my code is as follows:

void setup()
{
  Serial.begin(115200);
  Serial.println("Hello World!");
  delay(1000); //I added this delay to see if it would make a difference - it didn't.
  Serial.end();

  delay(1000); //Same as above.
  if (Serial)
  {
    digitalWrite(12, HIGH);
    delay(3000);
    digitalWrite(12, LOW);
  }
  else
  {
    digitalWrite(12, HIGH);
    delay(100);
    digitalWrite(12, LOW);
    delay(100);
    digitalWrite(12, HIGH);
    delay(100);
    digitalWrite(12, LOW);
  }

  while(true) {} //There is nothing on the loop, so we end here.
}

So basically, it prints "Hello World!" to the Serial (and it worked), and then it call Serial.send(), and later it checks:
if (serial) -> One long beep is true - Two brief beeps if false.

for some reason it returns true even if Serial.end() was called earlier, and if I try to print something to the Serial after Serial.end() was called the sketch freezes.

Is there a reliable way to know if serial communication is activated?

 if (Serial)

I presume that this is supposed to be

if (Serial.available())

Serial.end() stops the Serial communication and frees up the Tx and Rx pins for other use if necessary but it does not flush the input buffer. Neither does Serial.flush() by the way.

for some reason it returns true even if Serial.end() was called earlier,

Yes it will because Serial.end() Disables serial communication, allowing the RX and TX pins to be used for general input and output.

Where as when you check Serial you are checking to see if the serial port is open. That means if it exists. So on a UNO and the like this will always return true because there is a real hardware UART, even if the software to drive it has been turned off. It will return true even before you give it a begin.

The main use of Serial is to check if a USB serial port emulator has been enumerated on boards like the Leonardo or the Micro.

Grumpy_Mike:
Yes it will because Serial.end() Disables serial communication, allowing the RX and TX pins to be used for general input and output.

Where as when you check Serial you are checking to see if the serial port is open. That means if it exists. So on a UNO and the like this will always return true because there is a real hardware UART, even if the software to drive it has been turned off. It will return true even before you give it a begin.

The main use of Serial is to check if a USB serial port emulator has been enumerated on boards like the Leonardo or the Micro.

Thanks buddy. I guess the solution would be to keep track of the calls to Serial.begin() and Serial.end() to know whether or not I should attempt to print() something to it without freezing the sketch.

UKHeliBob:

 if (Serial)

I presume that this is supposed to be

if (Serial.available())

Serial.end() stops the Serial communication and frees up the Tx and Rx pins for other use if necessary but it does not flush the input buffer. Neither does Serial.flush() by the way.

Not really. Serial.available() checks for data in the input buffer, just like you said. What I wanted was to check if the serial communication was enabled. Grumpy_Mike explained why it returns true, and I already figured out a solution.
The sketch I wrote on the question was just a tiny one I used to test that. I actually need to be able to determine this for a much bigger sketch.

You could also check the RX interrupt enable bit in the UART control register B which Serial.end() clears. On an Uno it would be this:

bool serial_enabled = _SFR_MEM8(0xc1) & 0x80;