if (Serial) debug = 1; else debug=0; line in emonTH code - does it work?

I'm customising the code in emonTH and trying to better understand a few parts of it. Line 111 from the current code caught my eye:

if (Serial) debug = 1; else debug=0;

I'd never seen this used before, so looked into it and couldn't see how it worked, so I wrote a sketch to try it out. Indeed, it doesn't seem to work - it always returns true it seems.

Am I missing something?

Robert Wall's picture

Re: if (Serial) debug = 1; else debug=0; line in emonTH code - does it work?

How are you testing this?

I think I know how it should work, and what you're missing is no serial monitor. According to the language: "boolean : returns true if the specified serial port is available.". So it should prevent writing debug information to the serial port if the serial port is not available. But if you're monitoring on the serial port....

cybergibbons's picture

Re: if (Serial) debug = 1; else debug=0; line in emonTH code - does it work?

I'm monitoring a pin that I am toggling.

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);
  }
}

 

cybergibbons's picture

Re: if (Serial) debug = 1; else debug=0; line in emonTH code - does it work?

I think the short answer is that it doesn't seem to work.

Each time it sends data, it outputs ~50 characters to Serial, which in turn means that 50*8/9600 = 0.04s is spent sending the data.

Schism's picture

Re: if (Serial) debug = 1; else debug=0; line in emonTH code - does it work?

It doesn't work on my emonTH units. I discovered this because I wrap my LED pulses in if(debug) statements... then noticed it would still flash away happily when I plugged the batteries in!

 

IOW, if(Serial) is always true, whether or not anything is plugged into the serial pins.

cybergibbons's picture

Re: if (Serial) debug = 1; else debug=0; line in emonTH code - does it work?

Yes, if you look in HardwareSerial.h:

#if defined(UBRRH) || defined(UBRR0H)
  extern HardwareSerial Serial;
#elif defined(USBCON)
  #include "USBAPI.h"
//  extern HardwareSerial Serial_; 
#endif
#if defined(UBRR1H)
  extern HardwareSerial Serial1;
#endif
#if defined(UBRR2H)
  extern HardwareSerial Serial2;
#endif
#if defined(UBRR3H)
  extern HardwareSerial Serial3;
#endif

I think that all that it will do is either exist or not-exist i.e. if the hardware didn't support Serial it might not compile.

I think this might have a significant impact on battery usage.

JBecker's picture

Re: if (Serial) debug = 1; else debug=0; line in emonTH code - does it work?

As you found, if(Serial) is just a test if a serial port is available on that hardware (and this will never change on a given hardware). How should it work instead? It would be necessary to test communication with an external host to know that 'something' is connected to the serial port externally.

I have always removed all this 'serial' stuff from low power code. Why not simply use a define like _UseSerial or directly #define debug?

PS: this might be different on a Leonardo, where an existing  USB connection can be clearly detected.

cybergibbons's picture

Re: if (Serial) debug = 1; else debug=0; line in emonTH code - does it work?

It's an odd way to detect the hardware though, as it will produced a compile time error if the hardware isn't present i.e. the debug = 1/0 will never actually be used.

How should it work? For most of my projects I wait a few seconds at startup and if I detect any serial input, I switch debug output on.

It's not my code, so I was curious about this construct I had never seen before.

glyn.hudson's picture

Re: if (Serial) debug = 1; else debug=0; line in emonTH code - does it work?

Good point regarding the serial printing. 

I have updated the code on GitHub to have debug turned off by default. The .hex file has also been updated. All the emonTH sold through the shop will have debug turned off by default to improve battery life.

https://github.com/openenergymonitor/emonTH/tree/master/emonTH_DHT22_DS1...

cybergibbons's picture

Re: if (Serial) debug = 1; else debug=0; line in emonTH code - does it work?

Glyn - on a lot of my sketches (well, code, I'm only really getting into Arduino vs AVR now), I check to see if serial is available for the first 10s of operation, and if so, turn debug output on, otherwise continue.

I can put an example on github later.

glyn.hudson's picture

Re: if (Serial) debug = 1; else debug=0; line in emonTH code - does it work?

I believe it's not possible to detect if a USB to UART cable is connected, as Jbecker mentioned above the check for Serial checks to see if the serial hardware is available (which is always is on the ATmega328). Things are different on the 32u4 used on the Leonardo. 

Jérôme's picture

Re: if (Serial) debug = 1; else debug=0; line in emonTH code - does it work?

I think he means

From the emonTH perspective, listen on the USB port for any input, and if there is something, assume someone is on the other side listening.

From user perspective, when switching on, send anything on UART to have debug activated.

But I may be wrong.

JBecker's picture

Re: if (Serial) debug = 1; else debug=0; line in emonTH code - does it work?

But if you check for a serial input from an external node, then the UART is already enabled (drawing a little bit of additional current too). Then you can also enable and disable debug output by a command from the external node and it is not necessary to have it enabled all the time.  

glyn.hudson's picture

Re: if (Serial) debug = 1; else debug=0; line in emonTH code - does it work?

Would some code like this do the trick of checking at startup to see if anything is received on serial and if so turning on debug. Obviously a serial string would have to be sent during the two seconds it waits with with UART switching on. If no serial is received the UART can then be turned off to save battery

 

void setup() {
        Serial.begin(9600);     // opens serial port, sets data rate to 9600 bps
        delay(2000);            // wait two seconds
        if (Serial.available() > 0) //if serial is received
          debug==1;
        else {
        debug==0;
        Serial.end();
        }
cybergibbons's picture

Re: if (Serial) debug = 1; else debug=0; line in emonTH code - does it work?

Yes, sorry, I wasn't clear. If you call Serial.available after waiting a short period, then use that to set debug. A lot of routers and other embedded devices do similar.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.