cancel
Showing results for 
Search instead for 
Did you mean: 

UART received value conversion to float

kasun_duminda92
Associate III
Posted on December 30, 2012 at 21:28

I have been working on a program to directly transmit any character or set of characters that I transmit from the PC via UART, back to the PC immediately. This is just the first step in my project.

Code (this works):

    while(1) {

        int8_t buffer[20];

        scanf(''%s'', buffer);

        printf(''%s\n'', buffer);

        STM_EVAL_LEDToggle(LED4);

    }

Next I want use the value I receive from the PC for some mathematical operations in 'float' data type.

Can anyone please tell me how to covert the value received 'buffer' to a float value?

'Thank You' in advance.

#tags-are-pointless #stm32f0-usart #lacks-specificity #connect-uart-to-pc
4 REPLIES 4
Posted on December 30, 2012 at 21:36

Presuming you have a decent tool chain with libraries that support floating point, and more specifically floating point support in scanf/printf

float flt;

sscanf(buffer,''%f'',&flt);

Or

double dbl;

dbl = atof(buffer);

Or you could do some decimal math and parse it yourself.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
kasun_duminda92
Associate III
Posted on December 30, 2012 at 21:44

Posted on December 30, 2012 at 23:21

Both of these methods give errors. I am going to try parsing.

Perhaps some specificity to the error, and the tool chain, might help arrive at a solution.

atof() is usually part of stdlib.h, at least on Keil. I'd probably be including math.h and stdlib.h, along with stdio.h

A quick Google would probably bring up source code for something like atof(), or a lightweight equivalent thereof.

http://code.google.com/p/unladen-swallow/source/browse/branches/release-2009Q1-maint/Python/atof.c

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
kasun_duminda92
Associate III
Posted on December 31, 2012 at 09:58