I am working with some code for remote temperature sensor nodes. They turn the debug interface on and off depending on if anything is conneced to the serial port or not.
The code is on Github here. Line 111 is the line of interest:
if (Serial) debug = 1; else debug=0; //if serial UART to USB is connected show debug O/P.
So, a simple question: On a hardware and software level, how does this work? I can't see anything in HardwareSerial or Stream that would return true/false if anything was connected or not.
Note that I am not confident that it actually works. This simple test indicates it doesn't work as the LED flashes regardless of the presence of a serial adapter or connection:
boolean debug;
const int toggle = 6;
void setup()
{
if (Serial) debug = 1; else debug = 0;
pinMode(toggle, OUTPUT);
digitalWrite(toggle, LOW);
if (debug)
{
Serial.begin(9600);
Serial.println("Setup");
digitalWrite(toggle, HIGH);
delay(1000);
digitalWrite(toggle, LOW);
}
}
void loop()
{
if (debug)
{
Serial.println("Loop");
digitalWrite(toggle, HIGH);
delay(1000);
digitalWrite(toggle, LOW);
delay(1000);
}
}