Serial Issue, porting from Arduino Uno to STM32F4

Post here first, or if you can't find a relevant section!
Post Reply
Jannicux
Posts: 2
Joined: Tue Jan 30, 2024 11:37 am
Answers: 1
Location: Germany

Serial Issue, porting from Arduino Uno to STM32F4

Post by Jannicux »

Hello,

for my bachelors thesis I first wrote a code for an Arduino Uno and now I ported it to a STM32F407VG Discovery.
I also wrote a Program in Python which sends (utf-8) an array of 40 characters with " " inbetween, the µC then does a task depending on what the first entry is.

I use it in combination with a Shield from Mikroelectronika, equipped with a UART FTDI (See page 7):
https://download.mikroe.com/documents/a ... l-v100.pdf

The code itself works fine so far, however the Serial connection causes errors. I can send that array only once, then the STM32 gets faulty data I guess.
If I send the same array again, it will not run the task it is supposed to and not run "return_data(...)".

Sending this array in Arduinos' Serial monitor (ending with NL, CR) does not change effect.

However if I send "50 ", it works the first time and after an unpredictable amount of attempts, somewhere between 2 - 14 times.

Resetting the STM32 before each transmission also makes it work fine for the first array of data.

This worked fine with the Arduino Uno, I could do this as often as I want to.

The algorithm of Serial communication works as follows:
-> check if Serial.Available() > 0
-> if so, enter function "receive_data()"
-> read serial data until "\n\r" or Serial.available() == 0
-> check first char of array
-> enter function "return_data()" if first char has a defined function

Code: Select all

#include <HardwareSerial.h>
HardwareSerial Serial2(USART2);
int numChars = 39;

...

void setup(){
  ...
  
  Serial2.setRx(PA3);
  Serial2.setTx(PA2);
  Serial2.begin(9600);
  while (!Serial2); 
  
  ...
  

Code: Select all

float receive_data() {
  int rc;
  char endMarker = '\n\r';


  digitalWrite(PD14, HIGH);

  while (Serial2.available() > 0 && newData == false) {

    rc = Serial2.readStringUntil(' ').toInt();

    if (rc != endMarker) {
      receivedChars[ndx] = rc;
      rc = 0;
      ndx++;
    } else {
      newData = true;
    }
    delay(5);
  }

  float checksum = 0;
  for (i = 0; i <= numChars; i++) {

    if (receivedChars[i] == -1) TransceiveByte[i] = 10;			//convert 10 to -1 since arduino reads this as LF
    else if (receivedChars[i] == -2) TransceiveByte[i] = 13;		//convert 13 to -2 since arduino reads this as CR
    else TransceiveByte[i] = receivedChars[i];


    if ((i < numChars) && i != 0 && i != 35 && i != 36 && i != 37 && i != 38) {
      checksum += TransceiveByte[i];
    }
    delay(10);
    receivedChars[i] = 0;

    if (i == numChars) {
      TransceiveByte[numChars] = checksum;
    }
  }
  //IWatchdog.reload();

  digitalWrite(PD14, LOW); //Turn LED off to signal function is done


  return checksum;
}

Code: Select all

void return_data(byte wert) {
  int return_data_int;
  digitalWrite(PD15, HIGH);


  for (j = 0; j <= numChars; j++) {
    //IWatchdog.reload();

    //if ( j % 4 == 0)   digitalWrite(motor_output, LOW);
    //if ( j % 8 == 0)   digitalWrite(motor_output, HIGH);

    if (j == 0) TransceiveByte[0] = wert;

    return_data_int = TransceiveByte[j];
    Serial2.println(return_data_int);

    delay(10);
  }

  TransceiveByte[0] = 0;


  digitalWrite(PD14, LOW);			//Indicate that this function was run, let onboard LED blink
  delay(250);
  digitalWrite(PD14, HIGH);
  delay(250);
  digitalWrite(PD14, LOW);
  delay(250);
  digitalWrite(PD14, HIGH);
  delay(250);
  digitalWrite(PD14, LOW);

  digitalWrite(PD15, LOW);
}

Code: Select all

 void loop() {
  int checksum2;


  if (Serial2.available() > 0) {
    delay(100);
    checksum2 = receive_data();
    Serial2.flush();
  }
 
    if ((HAL_GetTick() - starttimer >= endtimer) || newData == 1) {  // Check data every second

    newData = 0;

    starttimer = HAL_GetTick();


    TransceiveByte[numChars] = checksum2;


    if (TransceiveByte[0] == 50) {  // Task 1
    	...
    	return_data(51);
    }
    
    if (TransceiveByte[0] == 2) {  // Task 2
    	...
     	return_data(5);

    }
    
    ...
    
    }
}
    
I Hope you could understand my issue, it is my first time asking in a forum for coding help. :D
by Jannicux » Fri Feb 02, 2024 5:15 pm
I could resolve the issue.

The counter "ndx" was not set to 0 before or after receiving data, which lead to not filling the TransceiveByte array correctly.


Adding ndx=0 before solved the problem.
However it worked fine on arduino like this.
Go to full post
Jannicux
Posts: 2
Joined: Tue Jan 30, 2024 11:37 am
Answers: 1
Location: Germany

Re: Serial Issue, porting from Arduino Uno to STM32F4

Post by Jannicux »

I could resolve the issue.

The counter "ndx" was not set to 0 before or after receiving data, which lead to not filling the TransceiveByte array correctly.


Adding ndx=0 before solved the problem.
However it worked fine on arduino like this.
Post Reply

Return to “General discussion”