Arduino Core for STM32  1.0
HarwareSerialExample
  1. Basic Serial example
    #include "HardwareSerial.h"
    //At first, enable UART1 and its NVIC in .ioc file settings.
    extern UART_HandleTypeDef huart1;
    HardwareSerial Serial(huart1); //creating instance of HardwareSerial for UART1.
    //This method have to be called in main function in main.c file.
    void app(void){
    //Enable periphery
    Serial.begin();
    //Infinite loop
    while(1){
    //Constant strings can be sent easily.
    Serial.print("Hello");
    //Strings as char array can be sent easily, too.
    char string2[] = ", this is random number: ";
    Serial.println(string2); // Use println() to print "\r\n" (new line and carriage return) characters at the end of string.
    //Number can be sent as text too.
    Serial.println(random(0,100));
    Serial.println("This is random character: ");
    //Use write() to send uint8_t as number.
    Serial.write(random(33,126)); //Random readable character from ASCII table
    //If you want to use c++ style of printing, use << operator
    Serial<<"\r\nAnother line with random number: " << random(0,100) << "\r\n";
    //Now we will wait until user will send us something
    while(!Serial.available());
    Serial.println("This is what I have received:");
    //Now we will read all characters
    while(Serial.available()){
    Serial.print(Serial.read()); //Send what is stored in FIFO buffer
    }
    //Print new line and carriage return characters.
    Serial.println();
    Serial.println("------------------------");
    }
    }
  2. NonBlocking send example
    #include "HardwareSerial.h"
    //At first, enable UART1 and its NVIC in .ioc file settings.
    extern UART_HandleTypeDef huart1;
    HardwareSerial Serial(huart1); //creating instance of HardwareSerial for UART1.
    //This variable stores the time, when data will be sent again
    uint32_t nextSentTime = 0;
    //This method have to be called in main function in main.c file.
    void app(void){
    //Enable periphery
    Serial.begin();
    //Set pin mode of built-in led to output
    pinMode(LED_BUILTIN, OUTPUT);
    //Create data, we want to send. Make sure, that your data are still on the same place and are stil accessible while data are sending.
    const uint8_t longData[] = "Hello, this is long string sent without blocking processor";
    //Infinite loop
    while(1){
    //Non blocking delay
    if(nextSentTime <= millis()){
    //Constant strings can be sent easily.
    Serial.writeNonBlocking(longData, sizeof(longData)/sizeof(uint8_t));
    nextSentTime = millis() + 5000; //Send data every 5 seconds.
    }
    //Togle LED state
    digitalToggle(LED_BUILTIN);
    //Wait 200ms
    delay(200);
    }
    }
Note
Those examples works only with c++, but STM32CubeIDE does not allow you to change main.c to main.cpp, so you have to create own c++ file, and include it in main.h. In your cpp file create function, that will be called in main() function. In this case, function name is app() and is called in main() function between tags USER CODE BEGIN 2 and USER CODE END 2.
See also
Hardware Serial documentation
HardwareSerial
HardwareSerial.h
HardwareSerial.h
This file contains class with methods to handle UART, USART and LPUART periphery.
millis
#define millis()
Returns the number of milliseconds passed since the STM board began running the current program.
Definition: wirish.h:851
delay
#define delay(millis)
Pauses the program for the amount of time (in milliseconds) specified as parameter.
Definition: wirish.h:860
random
unsigned long random(unsigned long from, unsigned long to)
The random function generates pseudo-random numbers.
Definition: wirish.cpp:1013
OUTPUT
@ OUTPUT
Basic digital output: when the pin is HIGH, the voltage is held at +3.3v (Vcc) and when it is LOW,...
Definition: wirish.h:467
pinMode
uint8_t pinMode(GPIO_TypeDef *GPIOx, uint16_t PPin, WiringPinMode mode, uint32_t speed)
Sets mode of selected pin.
Definition: wirish.cpp:250
HardwareSerial
This class is for handling UART periphery.
Definition: HardwareSerial.h:109
digitalToggle
void digitalToggle(GPIO_TypeDef *GPIOx, uint16_t PPin)
Toggles pin state.
Definition: wirish.h:645