3 years, 3 months ago.

How to retype std::string to string

I am testing communication on Nucleo-32. I have got the string message from USB into a buffer - message is: 0.750

#include "mbed.h"
#include <string>

char buff[128];
std::string message;
float pw_p;


//In buffer I have separate character buff[3] is 0, buff[4] is . , buff[5] is 7, buff[6] is 5, buff[7] is 0,

//Then I have tried to retype it to float

                pw_p = atof(message.c_str());
                pocitac.printf("Info:  %f\n", pw_p);

But in the pw_p is the value 0.000 :( Do you have any idea, where is a problem and how to do it correctly? Thank you very much for your help.

2 Answers

3 years, 2 months ago.

Hi there,

Your code is very close, you're just missing a few components of a C++ String. In C++, in order for a string to be valid, they must end with a null termination or the character "\0". So you need to append that to the end of your string in addition to the characters from your buff[128] array. You also are not appending to your message string correctly. Since you previously initialized the std::string message; on line 5, in your code you need to append the buffer onto your existing initialized variable:

message = message + buff[3] + buff[4] + buff[5] + buff[6] + buff[7] + '\0';

Then your pw_p variable should have the correct float value.

Please let me know if you have any questions!

- Jenny, team Mbed

Accepted Answer
3 years, 2 months ago.

Hello Lukas,

Let's make first the gets(char * buff, int num) function work properly. It reads characters from the serial line and stores them as a C string into buff until (num-1) characters have been read or either a newline or the end-of-file is reached, whichever happens first. It includes in the buff string any ending newline character ('\n') and also a terminating null character ('\0') is automatically appended after the characters copied. So if you send a string like 0.750 with appended newline character ('\n') from the serial monitor running on your PC then the buff should contain a string like "0.750\n\0" (make sure that the serial monitor is configured to automatically append a newline char to the string you send). Then you do not need to use std::string in your code at all. When that works then you can easily convert the alphanumeric string to floating point number and set the PWM duty cycle:

#include "mbed.h"
Serial  pocitac(USBTX, USBRX);
char    buff[128];
float   pw_p;
PwmOut  PWM_Out1(p21);  // substitute p21 with the PWM pin name on your board
int main(void)
    while (1)
        // Receive a string (that has a newline character at the end) representing a floating point number
        pocitac.printf("Enter a number:\r\n");
        pocitac.gets(buff, 128);
        pocitac.printf("buff: %s", buff);
        // Convert the string to floating point number
        pw_p = atof(buff);
        pocitac.printf("number = %4.3f\r\n", pw_p);
        // Set PWM duty cycle
        PWM_Out1.write(pw_p );        

Thank you very much. I have solved it based on your recommendation.

posted by Lukas Bier 03 Apr 2018