Reputation: 153
I am using ESP32(esp32-wroom-32) and GPS neo-6m.
I am using arduino IDE for development.
we can read data from UART2 using below methods
First
void setup()
{
Serial.begin(115200);
Serial2.begin(115200);
}
void loop()
{
Serial.print(Serial2.read());
}
Second
#include <HardwareSerial.h>
HardwareSerial SerialPort(2); // use UART2
void setup()
{
Serial.begin(115200);
SerialPort.begin(115200, SERIAL_8N1, 16, 17);
}
void loop()
{
Serial.print(SerialPort.read());
}
What is the difference in above methods and scenarios based on which we select one of the above methods ?
P.S I am new to Arduino IDE and micro controller programming.
Upvotes: 1
Views: 3673
Reputation: 49
Both sections of code perform same function. The difference is FirstCase-> uses Serial2 object defined in hardwareserial.cpp library file SecondCase-> Port object is declared in the same file.
They both function same with no performance difference, it's individual preference. I would go with secondcase, because the definition is inside same code file.
Upvotes: 0
Reputation: 1
because some boards already come with a second set of serial ports and some boards don't. If the board already has designated second set, you don't need the HardwareSerial library.
Upvotes: 0