DebugSerial.println()

I have just come across a sketch which uses: DebugSerial.println()

Is there such a function?
If so - how does it work?

Delta_G:
println is a member of the Print class, so yes there is such a function so long as DebugSerial has been defined as either an instance of a class that inherits from Print or is a pointer to some instance of such a class.

Without seeing the code you speak of, there's not much else that can be said.

The code was a WiFi connection example - but for me that is irrelevant.

I am interested to know how I can use it in my own sketches.

My interest stems from the fact that I have in a piece of code I am working on, I started out by defining modeDebug and I am printing debug values surrounded with #ifdef/#endif. I have since written a debugPrint function which I will now use instead.

When I saw the sketch using DebugSerial.println() I was wondering if I am re-inventing the wheel by writing my own function.

So any info would be appreciated.

I would guess that the program uses the hardware serial ports and uses software serial for debugging.

See here for example: Gammon Forum : Electronics : Microprocessors : Debugging using SPI/I2C and a second processor

You can derive from Print and make your own debugging, and also have it dependent on a #define so it turns into "no code" if the define is false.

You can define Macros as below:

//***************************************************************
#define DEBUG       //If you comment this line the DPRINT & DPRINTLN
//lines are defined as blank.
//examples:
//DPRINTLN("Testing123");    
//DPRINTLN(0xC0FFEEul,DEC);
//DPRINTLN(12648430ul,HEX);

#ifdef DEBUG
#define DPRINT(...)    Serial.print(__VA_ARGS__)
//OR, #define DPRINT(args...)    Serial.print(args)
#define DPRINTLN(...)  Serial.println(__VA_ARGS__)
#define DRINTF(...)    Serial.print(F(__VA_ARGS__))
#define DPRINTLNF(...) Serial.println(F(__VA_ARGS__))

#else
#define DPRINT(...)     //blank line
#define DPRINTLN(...)   //blank line
#define DPRINTF(...)    //blank line
#define DPRINTLNF(...)  //blank line

#endif
//***************************************************************

Delta_G:
No, it's not irrelevant at all. It is very relevant. Because in that code is defined this mystical DebugSerial and without seeing that all anyone can do about it is guess.

I wouldn't ask you for code if it was irrelevant.

Noted. Here is the code; and I have other similar bits of example code for a WiFi connection I am trying to make.

#define SSID "ITEAD_2"        //type your own SSID name

#define PASSWORD "itead2468"        //type your own WIFI password

#include "uartWIFI.h"

#include "SoftwareSerial.h"

WIFI wifi;

extern int chlID;        //client id(0-4)

void setup()

{

 

pinMode(6,OUTPUT); //使用mega不改变接线的话,此处改为D24脚

wifi.begin();

bool b = wifi.Initialize(STA, SSID, PASSWORD);

if(!b)

{

DebugSerial.println("Init error");

}

delay(8000); //make sure the module can have enough time to get an IP address 

String ipstring = wifi.showIP();

DebugSerial.println(ipstring);        //show the ip address of module

 

delay(2000);

wifi.confMux(1);

delay(100);

if(wifi.confServer(1,8080))

DebugSerial.println("Server is set up");

}

void loop()

{

char buf[100];

int iLen = wifi.ReceiveMessage(buf);

if(iLen > 0)

{

if (strcmp(buf, "CH0ON") == 0)

{

digitalWrite(6,HIGH); //使用mega不改变接线的话,此处改为D24脚

DebugSerial.println("CH0ON");

}

if (strcmp(buf, "CH0OFF") == 0)

{

digitalWrite(6,LOW); //使用mega不改变接线的话,此处改为D24脚

DebugSerial.println("CH0OFF");

}

}

}

My question in relation to the above code is, what would be the difference if Serial.println() was used instead of DebugSerial.println().

Errrr.... Thanks but err.... way waaaay waaaaaaaaaaaaaaaaaay over my head.
I could of course just blindly copy/paste and use it, but I won't be doing myself any favours.

For now I prefer to choose a path of coding such that I actually understand what I am doing.

But hey, one day soon I hope to understand your code :slight_smile:

aisc:
Here is the code;
...SNIP....
My question in relation to the above code is, what would be the difference if Serial.println() was used instead of DebugSerial.println().

The DebugSerial stuff is not defined in the code you posted so it must be in one of the included libraries. I note that both libraries are included with #include "uartWIFI.h" rather than #include <uartWIFI.h> which is more standard. It means that either library may have been modified to add the extra capability.

The simple answer to the question I have quoted is "try it and see what happens"

There are a few issues about debug messages. You may want them to go through a different I/O stream - for example over the USB connection rather than via WiFi. Or you may want to include some special identifier in the message that tells the receiving program that this is not part of the normal flow of data. Or you may simply want it to be obvious to someone reading the code that it is a debug message and not part of the "real" program.

What do you want to achieve ?

...R

@Robin2: I ran the code yesterday and from memory there was nothing special about the output in fact very little.

I now realise I can save memory both flash & SRAM by making serial printing conditional.

At present I am happy to just print to the serial monitor for debugging.

This is what I do now :

#define mode_Debug

char j[] = "Hello World!";

void setup() {
  // put your setup code here, to run once:

  #ifdef mode_Debug
    Serial.begin(9600);
    while(!Serial);
    Serial.println("Serial started.");
  #endif

  #ifdef mode_Debug
    Serial.println(j);
  #endif

//  DebugSerial.println(j);
}

void loop() {
  // put your main code here, to run repeatedly:

}

If there is a DebugSerial.println() function how do I use it to achieve the same as the above code.

LarryD:
You can define Macros as below:

//***************************************************************

#define DEBUG      //If you comment this line the DPRINT & DPRINTLN
//lines are defined as blank.
//examples:
//DPRINTLN("Testing123");   
//DPRINTLN(0xC0FFEEul,DEC);
//DPRINTLN(12648430ul,HEX);

#ifdef DEBUG
#define DPRINT(...)    Serial.print(VA_ARGS)
//OR, #define DPRINT(args...)    Serial.print(args)
#define DPRINTLN(...)  Serial.println(VA_ARGS)
#define DRINTF(...)    Serial.print(F(VA_ARGS))
#define DPRINTLNF(...) Serial.println(F(VA_ARGS))

#else
#define DPRINT(...)    //blank line
#define DPRINTLN(...)  //blank line
#define DPRINTF(...)    //blank line
#define DPRINTLNF(...)  //blank line

#endif
//***************************************************************

Only noticed your post now - sorry.
C++ macros are a bit beyond me at the moment.

Anyway it seems the gist of it is DebugSerial.println() is not a function which presently exists in the IDE, and needs to be "created".

So I am thinking my basic Idea of defining a debug mode and combining it with a custom conditional Serial.print() function is a viable way to achieve my objective.

Is DEBUG a "reserved" word/term for C++?

  #ifdef mode_Debug
    Serial.begin(9600);
    while(!Serial);
    Serial.println("Serial started.");
  #endif

No, no, no.

  #ifdef mode_Debug
    Serial.begin(9600);
    while(!Serial);
    Serial.println(F("Serial started."));
  #endif

Is DEBUG a "reserved" word/term for C++?

No.

AWOL:

  #ifdef mode_Debug

Serial.begin(9600);
    while(!Serial);
    Serial.println("Serial started.");
  #endif



No, no, no.




#ifdef mode_Debug
    Serial.begin(9600);
    while(!Serial);
    Serial.println(F("Serial started."));
  #endif

LOL - ok ok !!

Where do people keep getting this idiotic piece of code from?

    Serial.begin(9600);
    while(!Serial);

That while says "do nothing until Serial is non-null". Well, if Serial is null, how is Serial.begin() supposed to work? And what could possibly change it from WITHIN an empty while loop?

The while accomplishes absolutely nothing of value, and does not belong there.

Regards,
Ray L.

Delta_G:
At least one of the boards (Leonardo I think) needs that line. I'm not sure exactly why. I've never used any board that needed it. But apparently for some boards the serial interface needs a second to "warm up". For code on UNOs and MEGAs and such it shouldn't be there. But in a lot of contributed pieces it gets left there just in case I think.

Perhaps, then, for Leonardo only, it should read:

while(!Serial)
    ;
Serial.begin(9600);

But as it was, it makes no sense at all. The Serial.begin can't possibly work if Serial is null.

Regards,
Ray L.

while (!Serial) does not compare Serial to NULL, but calls the bool() operator of the board-specific serial class. On most boards it always returns true.

This is for Leonardo and other ATmega32u4-based boards:

// This operator is a convenient way for a sketch to check whether the
// port has actually been configured and opened by the host (as opposed
// to just being connected to the host).  It can be used, for example, in
// setup() before printing to ensure that an application on the host is
// actually ready to receive and display the data.
// We add a short delay before returning to fix a bug observed by Federico
// where the port is configured (lineState != 0) but not quite opened.
Serial_::operator bool() {
        bool result = false;
        if (_usbLineInfo.lineState > 0)
                result = true;
        delay(10);
        return result;
}

oqibidipo:
if(Serial) - Arduino Reference

while (!Serial) does not compare Serial to NULL, but calls the bool() operator of the board-specific serial class. On most boards it always returns true.

This is for Leonardo and other ATmega32u4-based boards:

// This operator is a convenient way for a sketch to check whether the

// port has actually been configured and opened by the host (as opposed
// to just being connected to the host).  It can be used, for example, in
// setup() before printing to ensure that an application on the host is
// actually ready to receive and display the data.
// We add a short delay before returning to fix a bug observed by Federico
// where the port is configured (lineState != 0) but not quite opened.
Serial_::operator bool() {
        bool result = false;
        if (_usbLineInfo.lineState > 0)
                result = true;
        delay(10);
        return result;
}

Wow! A great example of abuse of the ability to create a custom operator, by creating something that is both opaque and misleading. It would have been far better to simply create a function called "ready()", or something similar. Better still, just include that logic into begin(), so it is completely transparent to the user.

Regards,
Ray L.

RayLivingston:
Where do people keep getting this idiotic piece of code from?

    Serial.begin(9600);

while(!Serial);




That while says "do nothing until Serial is non-null". Well, if Serial is null, how is Serial.begin() supposed to work? And what could possibly change it from WITHIN an empty while loop?

The while accomplishes absolutely nothing of value, and does not belong there.

Regards,
Ray L.

Actually it's not idiotic.
I use an iBoardex, which is the equivalent of a Leonardo.
For most other boards, when u open the serial monitor, the sketch restarts but not for Leonardo or my iBoardex.
By having that bit of "idiotic code", whenever I open the serial monitor, my sketch restarts.
If it did not restart I would not see the initial serial printout.

For boards like Uno/Mega/Nano etc - it is not required.

Going back to the original topic...

I have written my function, but I have a problem :frowning:

#define mode_Debug

char j[] = "Hello World!";

void setup() {
  // put your setup code here, to run once:

  #ifdef mode_Debug
    Serial.begin(9600);
    while(!Serial);
    Serial.println("Serial started.");
  #endif
/*
  #ifdef mode_Debug
    Serial.println(j);
  #endif
*/
  dBugprintln(1,1,j);
  dBugprintln(0,1,j);
  dBugprintln(1,1,j);
  dBugprintln(0,1,j);
  dBugprintln(0,1,j);
  dBugprintln(1,1,j);
  dBugprintln(1,0 , "Hello World1");
  dBugprintln(0,0 , "Hello World2a");
  dBugprintln(1,0 , "Hello World2b");
  dBugprintln(0,0 , "Hello World3a");
  dBugprintln(0,0 , "Hello World3b");
  dBugprintln(1,0 , "Hello World3c");

}

void loop() {
  // put your main code here, to run repeatedly:
}

void dBugprintln(int ln, int var, char* msg){
  #ifdef mode_Debug
    if (ln == true) {
      if (var == true) Serial.println(msg);
      else Serial.println(F(msg));
    }
    if (ln != true) {
      if (var == true) Serial.print(msg);
      else Serial.print(msg);
    }
  #endif
}

I cannot/don't know how to do Serial.println(F(''sometext")); within the function.

This syntax does not compile :

Serial.println(F(msg));

The fix is probably in the error message - but above my head :frowning:

In file included from C:\Program Files\Arduino 1.6.5\hardware\arduino\avr\cores\arduino/Arduino.h:28:0,
from debug.ino:3:
debug.ino: In function 'void dBugprintln(int, int, char*)':
C:\Program Files\Arduino 1.6.5\hardware\arduino\avr\cores\arduino/WString.h:38:74: error: initializer fails to determine size of '__c'
#define F(string_literal) (reinterpret_cast<const __FlashStringHelper *>(PSTR(string_literal)))
^
debug.ino:41:27: note: in expansion of macro 'F'
C:\Program Files\Arduino 1.6.5\hardware\arduino\avr\cores\arduino/WString.h:38:74: error: array must be initialized with a brace-enclosed initializer
#define F(string_literal) (reinterpret_cast<const __FlashStringHelper *>(PSTR(string_literal)))
^
debug.ino:41:27: note: in expansion of macro 'F'
Error compiling.

A little help please... :slight_smile:

If you drop the F, it goes in RAM.

When I'm faced with a problem like this, my thought process looks like:
"I know the F() macro works for stuff like Serial.print.
I have the source code for all those functions - why don't I look at the source, and see how it's done?"