Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Arduino Serial object data type, to create a variable holding a reference to a port

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?".

like image 839
Steve Avatar asked Aug 08 '12 13:08

Steve


1 Answers

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);
like image 113
tinman Avatar answered Sep 24 '22 14:09

tinman