0

I wrote a program that uses a normal Serial port and a Bluetooth Serial Port to send logs to UART and Bluetooth respectively

HardwareSerial Serial;
BluetoothSerial SerialBT;

for example when i want to send something with them i write this

Serial.println("Hello World"); //prints Hello world in normal Serial port
SerialBT.println("Hello World"); // prints Hello World in Bluetooth serial

i defined some preprocessors that checks whether Bluetooth or Normal Serial port is active and if one of them is active print in the active one

my problem is here that i want to declare a variable like "Debugger" that i can assign it with "SerialBT" or "Serial" to write something like this

somewere in my init function

#if BLUETOOTHDEBUG
Debugger=&SerialBT;
#else
Debugger=&Serial;
#endif

and in code

Debbugger->println("Hello World")

instead of using if in whole code

10
  • 2
    Do these have a common base-class? Could they have one? If so it's easy. If not the answer is basically "no". Commented Jul 4, 2018 at 19:55
  • 1
    You can always design a wrapper class to expose them in a common interface. Commented Jul 4, 2018 at 19:58
  • 2
    HardwareSerial and BluetoothSerial both seem derived from Stream, so perhaps Stream &Debugger should work just fine? Commented Jul 4, 2018 at 20:02
  • 2
    include the full error and relevant code in your question Commented Jul 4, 2018 at 20:20
  • 2
    Create a minimal reproducible example, because that sounds like you messed up something unrelated. If class B and class C both inherit from class A, then A *a = new B() will work; likewise, if you have B b, then A *a = &b will as well. Commented Jul 4, 2018 at 20:31

1 Answer 1

1

based on Comments and the fact that HardwareSerial and BluetoothSerial class are both derived from Stream class, I added

Stream *Debugger

and it worked

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.