abdul_muneeb
abdul_muneeb

Reputation: 153

Difference between using hardwareserial.h and serial2 with ESP32 in arduino IDE

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

Answers (2)

Rasheed Abdul
Rasheed Abdul

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

jrib
jrib

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

Related Questions