Programmatically direct Print

I'm not sure that I'm using the correct search criteria or the right words so I thought I'd solicit human responses as the search engines have turned up nothing.

I have an Uno attached to my PC with the USB and SoftwareSerial connecting to a terminal via RS485.

Works great.

What I want is for the Uno to respond to requests coming from either serial port. These are all canned responses to known requests so there's no drama there. What isn't known is which terminal (hence which serial port) will produce the request.

I don't want a duplicate list of print statements, like Serial.print("Hello Moon") and RS485.print("Hello Moon"). I want to be able to flag the source of the request and then use that to direct my print statement, ala
if serial com = serial
else com = rs485
com.print("Hello Moon")
I don't think this is a tough one, I've just hit a mental wall. Any suggestions where I should be looking?

You can do that using a reference or pointer to the device, cast as a reference/pointer to an instance of the Print class.

For example:

void doPrint(Print &device, char *str)

doPrint(Serial, "Hello, world!");
doPrint(Serial1, "Hello, world!");

Ray L.

Perfect Ray, thanks. I’ve been using this template and it works real well Serial <<… or RS485 <<…

template inline Print &operator << (Print &obj, T arg) {obj.print(arg); return obj;}

Was trying to come up with a way of choosing one or the other.

Yeah, that's fine if you're OK with the overhead and potential problems of using String instead of simple char arrays.

Ray L.

No. Moving everything away from string floating point. All must now be char arrays and fixed point math.

I'll take your example and make a macro or inline function of it.