cancel
Showing results for 
Search instead for 
Did you mean: 

unexpected output

RS009
Associate III

0693W00000Y9N0cQAF.pngBelow is my code:

uint8_t line[50]="Application is Running\n";

uint8_t Buf[50];

uint8_t A=0,B=0;

  while(! HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_13)); // wait for user button press

  HAL_Delay(200);

  strcpy((char*)Buf," \n\nI am in the Loop \n");

  HAL_UART_Transmit(&huart1, Buf, strlen((char*)Buf), HAL_MAX_DELAY);

  HAL_Delay(200);

  strcpy((char*)Buf," Enter the number A : ");

  HAL_UART_Transmit(&huart1, Buf, strlen((char*)Buf), HAL_MAX_DELAY);

  HAL_UART_Receive(&huart1,&A, 2, 5000);

  strcpy((char*)Buf," \nEnter the number B : ");

  HAL_UART_Transmit(&huart1, Buf, strlen((char*)Buf), HAL_MAX_DELAY);

  HAL_UART_Receive(&huart1, &B, 2, 5000);

  sum=A+B;

  sprintf((char*)Buf,"\n Number A = %d",A);

  HAL_UART_Transmit(&huart1, Buf, strlen((char*)Buf), HAL_MAX_DELAY);

  sprintf((char*)Buf,"\n Number B = %d",B);

  HAL_UART_Transmit(&huart1, Buf, strlen((char*)Buf), HAL_MAX_DELAY);

I am getting unexpected output on serial monitor

what is wrong

0693W00000Y9MyRQAV.png 

1 ACCEPTED SOLUTION

Accepted Solutions

1) UART_Receive reading two bytes and writes them into one byte variable -> that is dangerous mistake (array overflow)

2) If you are looking for way "to read numbers from terminal" you are confused about strings, ASCII codes and numbers. From UART you want to read strings (array of characters), then you have to convert it to numbers (by sscanf() or atoi() or atol() or another way) and then you can print these numbers by sprintf().

View solution in original post

6 REPLIES 6

1) UART_Receive reading two bytes and writes them into one byte variable -> that is dangerous mistake (array overflow)

2) If you are looking for way "to read numbers from terminal" you are confused about strings, ASCII codes and numbers. From UART you want to read strings (array of characters), then you have to convert it to numbers (by sscanf() or atoi() or atol() or another way) and then you can print these numbers by sprintf().

gbm
Lead III

The output is at least partially expected. It's a miracle your program doesn't fail completely, since you are attempting to read 2 bytes into one-byte variable. I guess by pure luck A is placed in memory right after B, so while trying to receive two digits into B you actually read one digit into B and another into A, overwriting the previous value of A. ASCII code of digit n is 48+n, so the output is correct (48 -> digit 0, 51 - digit 3, 52 -> digit 4, etc.).

S.Ma
Principal

I feel a basic console C example in STM32Cube would help most users...

Thank you Michal, I really confused about that. I cleared it now but I have question, what if I want to store 40505600.56989 (frequency) in a variable by entering in a terminal

RS009
Associate III

Thank you gbm , I cleared my doubt with this tips

The same way as integer.

1) Receive string from UART (google "receiving strings UART STM32 HAL")

2) Then convert received string to floating point number for example by sscanf() or atof().