0

I'm confused with what I'm reading.

I have Serial communicating with my PC (USB) and SoftwareSerial over RS485 to a remote PC. I don't want to duplicate all of my print statements to be able to send output to either (or both) terminals. What I want to do is have a single set of print statements (think of a list of error messages) and then be able to output a message to either Serial or SSerial depending on which needs it. Does this make sense?

asked Sep 20, 2017 at 18:17

1 Answer 1

1

Almost all of those things you do which involve functions like print, println, and write all derive from the Print class. You could have an array of Print objects and print to whichever you like.

Print* outs[] = {&Serial, &Serial1, &mySoftSerial};
// Later somewhere in code:
outs[0]->println("This prints to Serial");
outs[1]->println("This prints to Serial1");
outs[2]->println("This prints to mySoftSerial");
for(int i = 0; i < 3; i++){
 outs[i]->println("This prints to all 3. You could even make this bit a function");
}
answered Sep 20, 2017 at 18:35

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.