Is there a way to alias Serial1

Is there is way to alias Serial1? I’d like to use something other than “Serial1” has the serial port I reference in my code. Serial1 is non-descriptive, I’m looking for a better “name”.

You could make a constant:

#define name Serial1

Wherever you put name in your code, it will be replaced by Serial1 upon compilation.

@nrobinson2000, thank you! I didn't think of using #define.

1 Like

You can also use a reference type - which adds type safety and can also be changed at runtime (e.g. as function parameter)

USARTSerial& myUSART = Serial1;
// or any other "compatible" class reference type instead of USARTSerial

Or "stinking ol’ object pointers

USARTSerial* myUSART = &Serial1;
// or any other "compatible" class reference type instead of USARTSerial
2 Likes

This is what I was looking for! I just didn't know the type for Serial.

Thank you.

1 Like

Ok, the type for Serial is USBSerial (I found) so I'm typing the variable as

USBSerial buletoothSerial = Serial;

@shiv, is that a typo or did you really mean to use Serial instead of Serial1. Serial is the virtual USB Serial device which is NOT tied to the RX/TX pins. Also, you will notice that you are missing the ampersand “&” after USBSerial since the line creates a pointer “bluetoothSerial” to the Serial object. The correct syntax in your case is as @ScruffR defined it.

1 Like

@peekay123,

Yes it should have been Serial1 and not Serial. Thank you for highlighting the “&” as well. I hadn’t done that in my code.

Nonetheless, @ScruffR is right. Apparently a Serial1 is a USARTSerial and not a USBSerial. So the code he provided is actually correct and works.

1 Like

@shiv, he’s @ScruffR!!! I wouldn’t expect any less :wink:

2 Likes

You make me blush :blush: - I’ve been wrong before :wink: