2023-01-27 12:41 PM
Below 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
Solved! Go to Solution.
2023-01-27 01:30 PM
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().
2023-01-27 01:30 PM
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().
2023-01-27 02:51 PM
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.).
2023-01-27 07:37 PM
I feel a basic console C example in STM32Cube would help most users...
2023-01-30 06:52 AM
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
2023-01-30 06:53 AM
Thank you gbm , I cleared my doubt with this tips
2023-01-30 08:26 AM
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().