UART DYNAMIC assign to variable

hello we have a query regarding the uart. is there any global typdef where we can declare pointer of that type and later assign uart something like

Stream uart;
int port = 0;

switch(port){
 case 1:
  uart = Serial1;
 case 2:
 uart  = Serial2;
}

since we are using couple of sensors with interchangable uart the problem is with uart we don’t want to hardcode uart

thank you

Serial1 and Serial2 are of type USARTSerial

But you would not want to do something like this

  USARTSerial uart = Serial1;

but rather

  USARTSerial *uart = &Serial1;

Or if you can opt for a reference

  USARTSerial& uart = Serial1;

This is especially suitable for a sensor class where you pass the respective interface to use to the constructor.

Thank you sir it works

1 Like