Serial2 on Blackpill STM32F411CE

Post here first, or if you can't find a relevant section!
lubbh
Posts: 4
Joined: Sat Feb 11, 2023 10:13 am

Serial2 on Blackpill STM32F411CE

Post by lubbh »

Hello,

I am, after a long time on this problem, again desperate and asking for your help..

Im working with Neo n8m GPS with board STM32F411CE and I cannot figure out, how to communicate via Serial2 o pins RX PA3 and TX PA2. If I wont be able to figure this out, I might try to switch to Serial1 pins, but I have different sensors on them now with I2C and this should be pretty easy, but I guess it is not..

After a lot of trying, now Im currently trying communicating from PC to the board with USB. The code is simple:

Code: Select all

HardwareSerial Serial2(PA3,PA2);
void setup() {
  Serial2.begin(115200);
  Serial.begin(115200);
}
void loop() {
  
  if (Serial2.available()) {
    byte inByte = Serial2.read();
    Serial.println("OK"+String(inByte));
  }
  else {Serial.println("NO");}
  delay(1000);
}
The problem is, that monitor displays nothing, when I am generating inputs with PuTTy, but before, it wasnt doing anything with the gps also.
I've run it thru logic analyser and everything is working fine, just the Serial2 isnt printing..

I red a lot about build flags, but this should be simple as it is..

Would appreciate any help. Thank you.
GonzoG
Posts: 403
Joined: Wed Jan 15, 2020 11:30 am
Answers: 26
Location: Prudnik, Poland

Re: Serial2 on Blackpill STM32F411CE

Post by GonzoG »

What is connected to Serial2 ??
lubbh
Posts: 4
Joined: Sat Feb 11, 2023 10:13 am

Re: Serial2 on Blackpill STM32F411CE

Post by lubbh »

Serial2 is used for the GPS, but for testing the connection, now its conneted to USB programmer (CH340). But the problem seems to be somewhere else, since its not working and printing nothing with anything connected.
GonzoG
Posts: 403
Joined: Wed Jan 15, 2020 11:30 am
Answers: 26
Location: Prudnik, Poland

Re: Serial2 on Blackpill STM32F411CE

Post by GonzoG »

I've tested my black pills (F401 and F411) and Serial1 and 2 work without any problems, so it's either wiring issue or bad MCU.

Instead of trying to test it with other stuff that may or may not work, connect Serial1 to Serial2.

Code: Select all

HardwareSerial Serial2(PA3, PA2);

void setup() {
  pinMode(LED_BUILTIN,OUTPUT);
  while(!Serial){}
  Serial1.begin(115200);
  Serial2.begin(115200);
  digitalWrite(LED_BUILTIN,0);
  delay(1000);
  Serial.println("Serial1 sending \"S1 begin comm\" to Serial2");
  Serial1.print("S1 begin comm");
  delay(2);
}

void loop() {
  String inco2="";
  while(Serial2.available())
  {
    char zn = Serial2.read();
    inco2 += zn;
  }
  if(inco2.length()>0){
    Serial.print("S2in: ");
    Serial.println(inco2);
    Serial2.print(inco2);
    inco2="";
  }
  delay(2);
  String inco1="";
  while(Serial1.available())
  {
    char zn = Serial1.read();
    inco1 += zn;
  }
  if(inco1.length()>0){
    Serial.print("S1in: ");
    Serial.println(inco1);
    inco1="";
  }
}
manny
Posts: 1
Joined: Sat Feb 11, 2023 12:39 pm

Re: Serial2 on Blackpill STM32F411CE

Post by manny »

The baud rate should be set at 9600 for the GPS module....from memory.
lubbh
Posts: 4
Joined: Sat Feb 11, 2023 10:13 am

Re: Serial2 on Blackpill STM32F411CE

Post by lubbh »

GonzoG wrote: Sat Feb 11, 2023 12:13 pm I've tested my black pills (F401 and F411) and Serial1 and 2 work without any problems, so it's either wiring issue or bad MCU.

Instead of trying to test it with other stuff that may or may not work, connect Serial1 to Serial2.

Code: Select all

HardwareSerial Serial2(PA3, PA2);

void setup() {
  pinMode(LED_BUILTIN,OUTPUT);
  while(!Serial){}
  Serial1.begin(115200);
  Serial2.begin(115200);
  digitalWrite(LED_BUILTIN,0);
  delay(1000);
  Serial.println("Serial1 sending \"S1 begin comm\" to Serial2");
  Serial1.print("S1 begin comm");
  delay(2);
}

void loop() {
  String inco2="";
  while(Serial2.available())
  {
    char zn = Serial2.read();
    inco2 += zn;
  }
  if(inco2.length()>0){
    Serial.print("S2in: ");
    Serial.println(inco2);
    Serial2.print(inco2);
    inco2="";
  }
  delay(2);
  String inco1="";
  while(Serial1.available())
  {
    char zn = Serial1.read();
    inco1 += zn;
  }
  if(inco1.length()>0){
    Serial.print("S1in: ");
    Serial.println(inco1);
    inco1="";
  }
}
Thank you for your answers. Not a good sign, that yours work without any problem. Tested your code and the only thing printed is: Serial1 sending "S1 begin comm" to Serial2.
ag123
Posts: 1653
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: Serial2 on Blackpill STM32F411CE

Post by ag123 »

if you are using usb-serial, i.e. just that usb cable. remember to select USB-CDC-Serial for your Serial device.
If you didn't, or have selected some other, it may goto Uart instead.

test usb cdc serial with a simple sketch like

Code: Select all

void setup() {
	while(!Serial);
	Serial.println("hello world");
}

void loop() {
	Serial.println("hello world");
	delay(1000);
}
those should appear in your monitor, e.g. putty
connect to the appropriate com: port

when usb (cdc) serial works, you can then add ( e.g. uncomment) the codes for Serial 2.
take note also about the baud rates, uart is sensitive to baud rates. With an unmatched baud rate, the end device may not respond.
some times it respond and you'd see garbage characters. baud rate needs to match that on the gps side for the uart. i.e. Serial2
Last edited by ag123 on Sun Feb 12, 2023 3:26 am, edited 1 time in total.
lubbh
Posts: 4
Joined: Sat Feb 11, 2023 10:13 am

Re: Serial2 on Blackpill STM32F411CE

Post by lubbh »

Thank you very much, I figured out, that it was wiring issue, unfortunately combined with chaos with USB programmer which have switched RX/TX pins :roll: :x
bonuscoach
Posts: 1
Joined: Thu Feb 16, 2023 3:24 am

Re: Serial2 on Blackpill STM32F411CE

Post by bonuscoach »

I had a similar issue. Sadly, the USB programmer caused havoc in addition to the wiring issue that flipped the RX/TX :roll:
asnasta
Posts: 1
Joined: Mon Mar 13, 2023 8:09 am

Re: Serial2 on Blackpill STM32F411CE

Post by asnasta »

Please accept my sincere gratitude, I realized that the problem was with the wiring, but unfortunately it was mixed with the mess caused by the USB programmer, which had the RX/TX pins switched
Post Reply

Return to “General discussion”