How to direct Serial output to other Serial ports on demand

Hello,

In several parts of my code I have included output to Serial port for debugging purposes.
eg

if (debugEnable) Serial.println(F("Query Peak Power"));

I am now trying to figure out how it would be possible to redirect this output to say Serial1 or other Stream object based on the state of a certain variable.
for example something like this:

if (console==1) {Serial.println(F("Query Peak Power"));}
 else  {Serial1.println(F("Query Peak Power"));}

Obviously I wouldn't like to have to enter the text twice every time as this takes unnecessary space.

Is there a more efficient way to achieve the same result?

void debugOut(const __FlashStringHelper *s) {
   switch( console ) {
      case 0:
         Serial.println(s);
         break;
      case 1:
         Serial1.println(s);
         break;
   }
}

something like this?

Thanks so mush for the speedy reply!

Hmm i am afraid I don't understand your code :frowning:

In addition... it doesn't compile...

You could make an array of pointers to several serial objects and use console as an index for this array. E.g.,

Stream* out[] {&Serial1, &Serial};

void setup() {
  Serial.begin(9600);

  size_t console {1};
  out[console]->println(F("Hi."));
}

Or you could use a reference, e.g.,

Stream& mySerial {Serial};  // Or `Serial1`.

void setup() {
  Serial.begin(9600);

  mySerial.println(F("Hi."));
}
1 Like

Thanks... that looks like it will work. I ll try it later...

Thanks for the suggesttion.

Your first option looks like it will work.
The second one however, I dont see how it can avoid entering the "Hi" text twice..

In this case, the choice is made at compile time, when you want to disable debugging, you can set the reference to Serial1 and recompile.

what about using a ptr?

int console    = 0;
Stream *serial = & Serial;

void
setup (void)
{
    Serial.begin  (9600);
    Serial1.begin (9600);

    if (console)
        serial = & Serial1;
}

void
loop (void)
{
    serial->println ("hello");
}

Combining a couple of the ideas from this thread:

#include "Arduino.h"

class DebugPrint : public Print {
public:
	virtual size_t write(uint8_t c) {
		return printPtr->write(c);
	}

	void directPrint(Print &here) {
		printPtr = &here;
	}

	virtual ~DebugPrint() {
	}

private:
	Print *printPtr = &Serial;
};

DebugPrint debug;

void setup(){
	Serial.begin(115200);
	Serial1.begin(115200);
	delay(2000);

	debug.print("Directing Debug Output to Serial");
	debug.directPrint(Serial1);
	debug.print("Debug Output Now Going to Serial1");
	debug.directPrint(Serial);
	debug.print("Debug Output Now Back at Serial");
}

void loop(){
}

Here's a version that allows disabling of the printing:

#include "Arduino.h"

class DebugPrint: public Print {
public:
	virtual size_t write(uint8_t c) {
		if (printPtr != nullptr) {
			return printPtr->write(c);
		} else {
			return 1;
		}
	}

	void directPrint(Print &here) {
		printPtr = &here;
	}

	void disablePrinting() {
		printPtr = nullptr;
	}

	virtual ~DebugPrint() {
	}

private:
	Print *printPtr = &Serial;
};

DebugPrint debug;

void setup() {
	Serial.begin(115200);
	Serial1.begin(115200);
	delay(2000);

	debug.print("Directing Debug Output to Serial");
	debug.directPrint(Serial1);
	debug.print("Debug Output Now Going to Serial1");
	debug.directPrint(Serial);
	debug.print("Debug Output Now Back at Serial");
	debug.disablePrinting();
	debug.print("Nothing Will Print Here");
	debug.directPrint(Serial1);
	debug.print("Debug Output Now Going to Serial1 Again");
}

void loop() {
}

Combining a couple of the ideas from this thread:

Thanks for the input.
I did try the code.
There seems to be some kind of conflict when used together with the TelnetStream library. ie when one of the stream objects is TelnetStream.

In this case Telnet connection is often lost.
I will do some more testing tomorrow with minimal code.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.