Sorry, I sent this as mail by mistake.
On 09-Mar-17 5:02 PM, Mark Storkamp wrote:
> I'm somewhat familiar with c, but not so much with c++. I'm writing a
> program for an Arduino micro-processor board, and the environment is c++.
>
> For serial communication I generally use the Serial class. There are
> calls such as: Serial.open(), Serial.print(), Serial.println(), etc.
It that's literally the syntax you're using, then `Serial` is a variable.
> Sometimes my code goes on to an Arduino ProMicro board, and then instead
> of the Serial class I need to use the Serial1 class, e.g.
> Serial1.open(), Serial1.print(), etc.
Ditto.
It that's literally the syntax you're using, then `Serial1` is a variable.
> Fortunately the compiler does #define ARDUINO_AVR_PROMICRO when I'm
> using the ProMicro board. So everywhere I do serial I/O I could do:
>
> #ifdef ARDUINO_AVR_PROMICRO
> Serial1.print(...); // or .open, or .println etc
> #else
> Serial.print(...);
> #endif
>
> But I would rather, in just one place, alias a name such as ComPort to
> either Serial or Serial1, so the rest of my code could be:
> ComPort.open(), ComPort.print(), etc.
>
> Is there an easy way to do this?
Assuming those two apparent variables really are variables, then
#ifdef ARDUINO_AVR_PROMICRO
auto& my_serial = Serial1;
#else
auto& my_serial = Serial;
#endif
Cheers & hth.,
- Alf