How do i print a floating point number into a string for serial output?

24 Mar 2011

I'm not quite an experience programmer(especially in c), i tried to search on the forum for some help, but i couldn't find any.

This is probably a very simple thing. But can someone show me how would i go by printing a float as an array of characters?

Are functions like cout be used in programing mbed?

the second question is: How do i read an array of characters from the serial port and convert them back into a floating point number?

Much thanks!

24 Mar 2011

Hi,

Use printf("%f", yourfloat);

See http://www.cplusplus.com/reference/clibrary/cstdio/printf/

24 Mar 2011

Hi

for your second question:

sscanf (yourstring,"%f",&yourfloat);

See http://www.cplusplus.com/reference/clibrary/cstdio/sscanf/

But be aware of problems with the decimal separator. Some countries use a . others a ,!

24 Mar 2011
26 Mar 2011

Do i do the following?

Serial pc(USBTX, USBRX); tx, rx

int main(){ pc.sscanf (yourstring,"%f",&yourfloat); }

I don't think the mbed has "sscanf" in the library for the pc.

I understand how to program in c on the computer (at least i did it for about 2 years ish). But it is the problem of doing it with mbed and communicating it in the serial port. I'm trying to read, write via the serial port.

now the current problem for me is to scan a string of 8characters(x.xxxxxx) and convert them into a floating point number that is useful for computing in mbed. If someone can help me out, that would be great. Sorry for not being more detail in my first post.

26 Mar 2011

Wim, you linked to "ftoa" but there is no such thing, only "itoa"

Kevin, sscanf is a C function, but you are trying to use it as if it is a function inside the Serial class, it is not a class member function of Serial.

26 Mar 2011

yea, i understand that. Thanks Frank, so should I not even declare the "serial" object "pc" and directly use sscanf?

But then wouldn't it run into declaring which serial port to use on the mbed, and all sort of setup things? So with all these in mind, how should i get a string of char and make them into a regular float?

How should i do this?

26 Mar 2011

Hi,

You can do something like the following:

#include "mbed.h"

Serial pc(USBTX, USBRX);

int main() {
    float a;
    pc.scanf("%f", &a);
}

(the extra s in sscanf means from a string, so if you were using that you'd read in to a string first)

Simon

26 Mar 2011

Hi,

Sorry i ment strtod() (I digged old code from another cpu where the function was present).

char[] yourstring = "15.0";
float yourfloat = strtod(yourstring, null);

See http://www.cplusplus.com/reference/clibrary/cstdlib/strtod/

Btw a tip: you should read/study example usage code at sites like cplusplus.com. The embed C++ compiler understand C too and most of the code on cplusplus.com just works. Programming C++ does not mean everything is an member of a class. The C RTL functions are happily present in C++ and thus the ARM C++ compiler mBed uses.

The serial port object should only be declared if you want to send or retrieve something over the serial port (ie communicate with another device or pc). So in your case it would be: Yes you should declare one.

As Simon pointed out you can either chose the C sscanf method which works on text already retrieved or use the scanf of the Serial object that uses characters retrieved from the serial port directly. This might be easier in your case,

If you look at http://mbed.org/projects/libraries/svn/mbed/trunk?rev=latest you'll find the well documented header files of the mbed lib.