cancel
Showing results for 
Search instead for 
Did you mean: 

sscanf function unable to copy float value in STM32CubeIDE

Rahul Chowdary
Associate II

I have win forms GUI and sending data from this GUI to STM32 Nucleo L4R5ZI µC over UART in byte array format. After receiving data over UART in µC, I am converting the data to their native formats such as Int ad Float. I don't have issue converting int, but float data is converted with too much precision i.e) 1.3 from GUI is converted as 1.29999995 in cubeIDE. That'y I implemented a function with sprintf and sscanf to convert 1.29999995 to 1.3. Sprintf copied the value to str (str is char array) 1.30, but sscanf is unable to copy the value back to float variable. Please have a look at the image for more info and also have a look at the code snippet. Please help me in this regard.

float conv(float var)
{
	convvar = var;
    sprintf(str, "%.2f", convvar);
 
    // scan string value in var
    sscanf(str, "%f", &convvar);
    return convvar;
}

0693W000001qBqBQAU.png

10 REPLIES 10

@PMish.1

That's a Different problem, where the libraries need to be pulled in for more comprehensive support, rather than smaller integer only.

The original problem is one of numeric representation of fractional decimal values in binary, and low precision floats.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..