Compiling Errors when using second serial in BluePill

I need to setup 2 serial ports on a Blue Pill F103c8, one for getting data from a GPS module and the other for communicating with the screen. Here is a section of the code:

#include <Arduino.h>
#include <HardwareSerial.h>
#include <TinyGPSPlus.h>
HardwareSerial Serial2(PA3,PA2); //RX, TX
// Define the GPS object
TinyGPSPlus gps;

// Initialize Serial for debugging and UART1 for GPS (PA9=TX, PA10=RX)
// Ensure the GPS TX connects to PA10 (RX) and GPS RX connects to PA9 (TX)
#define GPS_BAUD 115200

void setup() {
  Serial2.begin(115200); // For debugging output
  Serial1.begin(GPS_BAUD); // UART1 for GPS communication
  delay(20000);
  Serial.println("GPS Initialization Started");
  delay(5000);
}

When compiling I am getting this error from line 4:

Compilation error: conflicting declaration 'arduino::HardwareSerial Serial2'

How can I get this to work please?

The error message makes it sound like there's already a definition for Serial2, most likely in the core. Check in HardwareSerial.h and I would imagine you'll find it in there.

@xuraax
To test that Serial2 already defined, as stated by @van_der_decken - just comment the line:

and try to compile again

This is the new error when compiling with that line commented out.

GPSManager.ino.cpp:(.text.setup+0x34): undefined reference to `Serial2'

What is the GPSManager.ino ?

Please show a compile output in full.

Here is the full program. It may contain other unrelated errors:

#include <Arduino.h>
#include <HardwareSerial.h>
#include <TinyGPSPlus.h>
//HardwareSerial Serial2(PA3,PA2); //RX, TX
// Define the GPS object
TinyGPSPlus gps;

// Initialize Serial for debugging and UART1 for GPS (PA9=TX, PA10=RX)
// Ensure the GPS TX connects to PA10 (RX) and GPS RX connects to PA9 (TX)
#define GPS_BAUD 115200

void setup() {
  Serial2.begin(115200); // For debugging output
  Serial1.begin(GPS_BAUD); // UART1 for GPS communication
  delay(20000);
  Serial.println("GPS Initialization Started");
  delay(5000);
}

void loop() {
  // Read data from GPS module and encode it
  while (Serial1.available() > 0) {
    gps.encode(Serial1.read());
  }

  // Check if valid location data is available
  if (gps.location.isValid()) {
    Serial.print("Latitude: ");
    Serial.println(gps.location.lat(), 6);
    Serial.print("Longitude: ");
    Serial.println(gps.location.lng(), 6);
    Serial.print("Fix Age: ");
    Serial.print(gps.location.age());
    Serial.println(" ms");
  } else {
    Serial.println("No valid GPS data yet. Ensure antenna has a clear view of the sky.");
  }
  
  delay(1000);
}   

@xuraax
Please show a whole compile output.

What is the Arduino STM32 package you used?

Your sketch in post #6 compiled fine for me using an ancient STM32 core that I've never seen the need to update.

arduino-cli compile -b STM32:STM32F1:genericSTM32F103C:device_variant=STM32F103C8,upload_method=DFUUploadMethod,opt=osstd --warnings all --output-dir ~/tmp --no-color (in directory: /home/me/Documents/sketchbook/STM32/test)
Sketch uses 23324 bytes (35%) of program storage space. Maximum is 65536 bytes.
Global variables use 3712 bytes (18%) of dynamic memory, leaving 16768 bytes for local variables. Maximum is 20480 bytes.
Compilation finished successfully.

@xuraax
Your code from message #6 compiled for me using both an ancient RogerClark's STM32 core and a new STM32Duino core.

To avoid compile errors you need

  • comment Serial2 declaration line for RogerClark's core
  • preserve this line for STM32duino core (v2.12.0)

@xuraax
It seems that your first error :

perhaps is the result that you selected a wrong board due the compilation the code.

@b707 said:

I do not understand what you mean with the above. If you are referring to the board manager .json file than that would be the version by STMduino.

The board chosen is that for the BluePill STM32F103c8.

Here are the complete list of error messages:

home/joe/.cache/arduino/sketches/C656E3E6A3B4683C5C7E39D91A884C0F/sketch/requiredLibraries.cpp -o /home/joe/.cache/arduino/sketches/C656E3E6A3B4683C5C7E39D91A884C0F/sketch/requiredLibraries.cpp.o
/home/joe/Arduino/GPSManager/GPSManager.ino:4:16: error: conflicting declaration 'arduino::HardwareSerial Serial2'
4 | HardwareSerial Serial2(PA3,PA2); //RX, TX
| ^~~~~~~
In file included from /home/joe/.arduino15/packages/STMicroelectronics/hardware/stm32/3.0.0/cores/arduino/WSerial.h:5,
from /home/joe/.arduino15/packages/STMicroelectronics/hardware/stm32/3.0.0/cores/arduino/wiring.h:103,
from /home/joe/.arduino15/packages/STMicroelectronics/hardware/stm32/3.0.0/cores/arduino/Arduino.h:37,
from /home/joe/Arduino/GPSManager/GPSManager.ino:1:
/home/joe/.arduino15/packages/STMicroelectronics/hardware/stm32/3.0.0/cores/arduino/Serial.h:229:15: note: previous declaration as 'Uart Serial2'
229 | extern Uart Serial2;
| ^~~~~~~
Using library TinyGPSPlus at version 1.0.3 in folder: /home/joe/Arduino/libraries/TinyGPSPlus
Using library SrcWrapper at version 1.0.1 in folder: /home/joe/.arduino15/packages/STMicroelectronics/hardware/stm32/3.0.0/libraries/SrcWrapper
exit status 1

Compilation error: conflicting declaration 'arduino::HardwareSerial Serial2'

There were whole pages of compilation data in that list which I trimmed off as I fear the message got too bulky

@xuraax
As I see, you use a recent 3.0.0 release of the STMicroelectronics core:

The release has a new syntax of many ArduinoCore-API functions.
In replace of

you should use the:

Uart Serial2(PA3,PA2);

See details in conflicting declaration 'arduino::HardwareSerial Serial1' · Issue #3033 · stm32duino/Arduino_Core_STM32 · GitHub

Spot on!!!! Thank you.

Compilation went smoothly with this change. It compiled with or without the line:

#include <HardwareSerial.h>

What I find irritating is that I spent a lot of hours searching the internet for a solution and all suggestions were for the use of HardwareSerial not Uart.

Even deepwiki for STM32duino still suggests the use of the former. In the link you provided the change was mentioned in Issues section not in proper Documentation; or at least I am not able to find such docs.

I get this issue of finding it hard to find good docs for most libraries very often.

Is this normal or am I missing something of were to look?

Thanks again

In case of STM32duino project the reason is that the new 3.0.0 version has been released just in last month. Most internet sources and code examples still related to previous 2.x.x versions and even the STM32duino package wiki was not updated yet.

OK fair enough.