How to pass serial object as parameter to function?

like in this arduino example:

unsigned long mywait ;
void setup() 
{ Serial.begin(115200);
  Serial1.begin(9600);
  Serial2.begin(9600);
  Serial3.begin(9600);  
  mywait = millis();
}
//---------------------------------
void SerialManager(HardwareSerial &port )
{ if ( port.available() )
  { Serial.write( port.read() );  // etc...  do stuff
  }
}
//---------------------------------
void loop() 
{ 
  SerialManager( Serial1 );
  SerialManager( Serial2 );
  SerialManager( Serial3 );
  
  if ( mywait <= millis() )
  { mywait = millis() + 500;
    Serial1.write("Iam1"); // TX1 to Pin RX2
    Serial2.write("Iam2"); // TX2 to Pin RX3
    Serial3.write("Iam3"); // TX3 to Pin RX1
  }
}

You could either pass it as a Stream, USARTSerial or HAL_USART_Serial

2 Likes