Page 1 of 2

Serial2 on Blackpill STM32F411CE

Posted: Sat Feb 11, 2023 10:23 am
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.

Re: Serial2 on Blackpill STM32F411CE

Posted: Sat Feb 11, 2023 11:41 am
by GonzoG
What is connected to Serial2 ??

Re: Serial2 on Blackpill STM32F411CE

Posted: Sat Feb 11, 2023 11:50 am
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.

Re: Serial2 on Blackpill STM32F411CE

Posted: Sat Feb 11, 2023 12:13 pm
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="";
  }
}

Re: Serial2 on Blackpill STM32F411CE

Posted: Sat Feb 11, 2023 12:42 pm
by manny
The baud rate should be set at 9600 for the GPS module....from memory.

Re: Serial2 on Blackpill STM32F411CE

Posted: Sat Feb 11, 2023 12:53 pm
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.

Re: Serial2 on Blackpill STM32F411CE

Posted: Sat Feb 11, 2023 2:48 pm
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

Re: Serial2 on Blackpill STM32F411CE

Posted: Sat Feb 11, 2023 5:07 pm
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

Re: Serial2 on Blackpill STM32F411CE

Posted: Thu Feb 16, 2023 3:28 am
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:

Re: Serial2 on Blackpill STM32F411CE

Posted: Mon Mar 13, 2023 8:11 am
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