Compile Error: multiple definition of `Serial1'

Post here all questions related to STM32 core if you can't find a relevant section!
Post Reply
Chris_01234
Posts: 3
Joined: Sun Feb 14, 2021 7:21 am

Compile Error: multiple definition of `Serial1'

Post by Chris_01234 »

Hi all,

I'm using STM32 core V1.9.0 and am getting the following error when compiling:
Arduino: 1.8.13 (Windows 10), Board: "Generic STM32F0 series, STM32F030C8, STM32CubeProgrammer (SWD), Enabled (generic 'Serial'), Smallest (-Os default), Newlib Nano (default)"

c:/users/chris/appdata/local/arduino15/packages/stm32/tools/xpack-arm-none-eabi-gcc/9.2.1-1.1/bin/../lib/gcc/arm-none-eabi/9.2.1/../../../../arm-none-eabi/bin/ld.exe: C:\Users\Chris\AppData\Local\Temp\arduino_cache_819828\core\core_26975acf160c245a48c4a5d077fe08fa.a(HardwareSerial.cpp.o):(.bss.Serial1+0x0): multiple definition of `Serial1'; sketch\washer.ino.cpp.o:(.bss.Serial1+0x0): first defined here

collect2.exe: error: ld returned 1 exit status

exit status 1

Error compiling for board Generic STM32F0 series.

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
Code snippet:

Code: Select all

HardwareSerial Serial1(PB7, PB6);

void setup() {
  Serial1.begin(57600, SERIAL_8N1);                   //Open serial port 
}

void loop() {
  if (Serial1.available() > 0) {                      //If RS485 data available
    RS485_Data = Serial1.read();                        //Read RS485 data into buffer
  }
}
Any insight you could offer would be much appreciated.

Thank you!
User avatar
fpiSTM
Posts: 1738
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: Compile Error: multiple definition of `Serial1'

Post by fpiSTM »

Error is clear enough.
Serial1 is already defined by the core so you could not define a new one in your sketch.
Don't know which target you selected in 1.9.0 but the variant you used probably defined the Serial instance to be on USART1 so it is automatically defined.
Chris_01234
Posts: 3
Joined: Sun Feb 14, 2021 7:21 am

Re: Compile Error: multiple definition of `Serial1'

Post by Chris_01234 »

Hi fpiSTM, appreciate your help with this!

The syntax I used is straight from the Wiki: https://github.com/stm32duino/wiki/wiki ... wareserial

If the Wiki is incorrect, do you know the correct syntax?

Thank you
User avatar
fpiSTM
Posts: 1738
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: Compile Error: multiple definition of `Serial1'

Post by fpiSTM »

The syntax is correct and the wiki also.
The fact is by default a variant defined a default U(S)ART to use as a generic Serial.
For example the DISCO_F030R8:
https://github.com/stm32duino/Arduino_C ... #L133-L138

Code: Select all

// UART Definitions
#define SERIAL_UART_INSTANCE    1 // USART 1

// Mandatory for Firmata
#define PIN_SERIAL_RX           PA10
#define PIN_SERIAL_TX           PA9
This means the Serial1 will be instantiated by default and mapped to Serial generic instance name. So using Serial or Serial1 is the same.
This is your case, the variant you used (don't know wich one as in 1.9.0 there is no variant with STM32F030C8) define the generic Serial instance as the Serial1.
So as it is mentioned in the wiki:
By default, only one Serialx instance is available mapped to the generic Serial name.
In the variant you used if the Serial is defined to be Serial1 then you simply do not need to define it explicitly. The only thing to check if it is the RX/TX pins you want. If not simply do a:

Code: Select all

Serial.setRx(PB7); // or Serial1.setRx(PB7); it is the same.
Serial.setTx(PB6);
Chris_01234
Posts: 3
Joined: Sun Feb 14, 2021 7:21 am

Re: Compile Error: multiple definition of `Serial1'

Post by Chris_01234 »

Hi fpiSTM,

Great, thanks for explaining this, now I understand how it works!

For others, this is the final solution:

Code: Select all

//HardwareSerial Serial1(PB7, PB6);         //Not required and won't compile as Serial1 already instantiated in the variant, as detailed by fpiSTM

void setup() {
  Serial1.setRx(PB7);                       //Redefine required Serial1 RX pin
  Serial1.setTx(PB6);                       //Redefine required Serial1 TX pin
  Serial1.begin(57600, SERIAL_8N1);         //Open serial port
}
Shadrack Kipkemoi
Posts: 1
Joined: Thu Feb 09, 2023 11:02 am

Re: Compile Error: multiple definition of `Serial1'

Post by Shadrack Kipkemoi »

This is another correct solution also.
To solve your error you don't have to redeclare this "HardwareSerial Serial2(PA3, PA2);" because it has been declared in the header file. Just head straight and use the Serial2.
For example the code below to send message using gsm

Code: Select all

void setup() {
  Serial2.begin(9600);
  pinMode(PB15, OUTPUT);//Initialize gsm power key(my gsm power key is on pin 15)

  //Power gsm on
  digitalWrite(PB15, HIGH);  
  delay(3000);                      
  digitalWrite(PB15, LOW); 
  delay(15000); 
}

void loop() {

  SendMessage();                     
  delay(1000);               
}
void SendMessage()      
{
 Serial2.println("AT+CMGF=1");   

  delay(1000);  

 Serial2.println("AT+CMGS=\"+2547xxxxxxx\"\r"); 

  delay(1000);

 Serial2.println("Hi Brian welcome to Eago Group A."); 

  delay(100);

 Serial2.println((char)26);

  delay(1000);

}
User avatar
fpiSTM
Posts: 1738
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: Compile Error: multiple definition of `Serial1'

Post by fpiSTM »

Shadrack Kipkemoi wrote: Thu Feb 09, 2023 11:09 am This is another correct solution also.
To solve your error you don't have to redeclare this "HardwareSerial Serial2(PA3, PA2);" because it has been declared in the header file. Just head straight and use the Serial2.
For example the code below to send message using gsm

Code: Select all

void setup() {
  Serial2.begin(9600);
  pinMode(PB15, OUTPUT);//Initialize gsm power key(my gsm power key is on pin 15)

  //Power gsm on
  digitalWrite(PB15, HIGH);  
  delay(3000);                      
  digitalWrite(PB15, LOW); 
  delay(15000); 
}

void loop() {

  SendMessage();                     
  delay(1000);               
}
void SendMessage()      
{
 Serial2.println("AT+CMGF=1");   

  delay(1000);  

 Serial2.println("AT+CMGS=\"+2547xxxxxxx\"\r"); 

  delay(1000);

 Serial2.println("Hi Brian welcome to Eago Group A."); 

  delay(100);

 Serial2.println((char)26);

  delay(1000);

}
Serial2 is defined if it is the default Serial instance. That's all.
Post Reply

Return to “General discussion”