I'm working on a project with an ArduinoMega2560. There are multiple serial ports available, and I'd like to have a variable to hold a reference to one of them, something like this:
SerialPort port;
if (something == somethingElse)
port = Serial;
else
port = Serial1;
byte b = 5;
port.write(b);
However, the Arduino documentation is either limited or I haven't found the information I'm looking for. I think what I need it "What is the type for Serial, Serial1, etc?".
The underlying C++ type for the Serial objects is HardwareSerial
. You can find that in the files in <arduino path>\hardware\arduino\cores\arduino
. You can then use a pointers using code like this:
HardwareSerial *port;
if (something == somethingElse)
port = &Serial;
else
port = &Serial1;
byte b = 5;
port->write(b);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With