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:
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.
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);
}
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.
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 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?
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.