Arduino Serial对象数据类型,用于创建持有对端口的引用的变量

Ste*_*eve 6 embedded types serial-port arduino

我正在开发一个带有ArduinoMega2560的项目.有多个串口可用,我想有一个变量来保存对其中一个的引用,如下所示:

SerialPort port;
if (something == somethingElse)
    port = Serial;
else
    port = Serial1;

byte b = 5;
port.write(b);
Run Code Online (Sandbox Code Playgroud)

但是,Arduino文档要么有限,要么我没有找到我正在寻找的信息.我认为我需要它"Serial,Serial1等的类型是什么?".

tin*_*man 9

Serial对象的底层C++类型是HardwareSerial.你可以在文件中找到<arduino path>\hardware\arduino\cores\arduino.然后,您可以使用如下代码使用指针:

HardwareSerial *port;
if (something == somethingElse)
    port = &Serial;
else
    port = &Serial1;

byte b = 5;
port->write(b);
Run Code Online (Sandbox Code Playgroud)