I do not receive the float data from the BufferedSerial

I want to send the reading of a potentiometer through a serial port (BufferedSerial), I send the data as float, but I am not receiving that data. When I try with an int I receive it normally, but when it is float I don’t receive anything, what am I wrong with the code?

#include "mbed.h"

#define tiempo 1500ms

Mutex p_serie; //proteger el puerto serie

//decalraciones de pines
BufferedSerial pc(USBTX, USBRX); 
AnalogIn pot(A0); 

//prototipos de las funciones
void leer_pot (void);

//tareas o hilos
Thread hilo_leer_pot (osPriorityNormal, 2048);

//variables globales
const float voltaje_a = 3.3f; // Voltaje de referencia
char men[300]; //almacenar la cadena de caracteres
const float angulo_maximo = 290.0f; // Máximo ángulo del potenciómetro

int main() {
    hilo_leer_pot.start(leer_pot);
}

void leer_pot(){

    //configurar la velocidad de transmision
    pc.set_baud(9600);

    pc.set_format(8, BufferedSerial::None, 1); //8-N-1

    while (true){
        // Leer el valor del potenciómetro
        float valor_adc = pot.read();
        //double valor_double = static_cast<double>(valor_leido);
        //int valor_leido_entero = round(valor_leido);

        // Calcular el voltaje real
        float voltaje_pot = voltaje_a * valor_adc;

        // Calcular el ángulo
        float angulo_pot = ((voltaje_pot / voltaje_a) * angulo_maximo);

        p_serie.lock();

        // Enviar el valor por el puerto serie
        //printf("Voltaje del potenciometro: %.2f V\n", double(voltaje_real));
        /*printf(men, "El valor es: %.3f\n\r", valor_leido);
        pc.write(men, sizeof(men));*/
        
        // Convertir el número flotante a una cadena de caracteres usando snprintf
        //snprintf(men, sizeof(men), "%Lf", angulo_pot);

        // Imprimir la cadena resultante
        printf("El angulo es: %.2f grados.", angulo_pot);
        //printf("El angulo es: %s grados.\n\r", men);

        p_serie.unlock();

        // Esperar 2 segundos antes de leer el potenciómetro nuevamente
        ThisThread::sleep_for(tiempo); 
    }
}

Hello,

please check this and change settings if necessary

BR, Jan

1 Like

Bro, it still doesn’t work for me :frowning: I made the change you told me. Look at the code:
#include “mbed.h”

#define tiempo 1500ms

Mutex p_serie; //proteger el puerto serie

//decalraciones de pines
BufferedSerial pc(USBTX, USBRX);
AnalogIn pot(A0);

//prototipos de las funciones
void leer_pot (void);

//tareas o hilos
Thread hilo_leer_pot (osPriorityNormal, 2048);

//variables globales
const float voltaje_a = 3.3f; // Voltaje de referencia
char men[300]; //almacenar la cadena de caracteres
const float angulo_maximo = 290.0f; // Máximo ángulo del potenciómetro

int main() {
hilo_leer_pot.start(leer_pot);
}

void leer_pot(){

//configurar la velocidad de transmision
pc.set_baud(9600);

pc.set_format(8, BufferedSerial::None, 1); //8-N-1

while (true){
    // Leer el valor del potenciómetro
    float valor_adc = pot.read();

    // Calcular el voltaje real
    float voltaje_pot = voltaje_a * valor_adc;

    // Calcular el ángulo
    float angulo_pot = ((voltaje_pot / voltaje_a) * angulo_maximo);

    p_serie.lock();

    // Enviar el valor por el puerto serie
    sprintf(men, "El valor es: %f\n\r", angulo_pot);
    pc.write(men, sizeof(men));

    p_serie.unlock();

    // Esperar 2 segundos antes de leer el potenciómetro nuevamente
    ThisThread::sleep_for(tiempo); 
}

}

I made change in the .json file. look:



Help, I don’t know what to do anymore :frowning:

You may not touch the compile_comands.json file. The necesary settings should be in mbed_app.json file. If you do not have this file in your project, then you have to create one in your project folder.

BR, Jan

1 Like

I already created the .json file, look:
image
y en el archivo angulo_potenciometro.json metí el codigo que me dijiste, mira:



And I still have the same problem, it doesn’t print the float value, but if I put it in int it throws it back, look:

what will happen? What do I have wrong? What I’m I missing of? :frowning:

I thought I had to change the values ​​that were false in the code of the .json file, I changed them, but it didn’t help either :frowning:

Nope that Is not correct. You do not Read the instruction correctly.

BR, Jan

1 Like

What am I doing wrong? I’m not understanding :frowning: help, I just want my code to work, help me bro

bro, I found this in my project “platform/mbed_lib.json”, and there was this code that you gave me:


but the others were not there, although it was like the beginning of the code, so I put what was missing, look:
image
and the code still doesn’t work :(, what else can I do? I really appreciate what you’ve explained to me.

Another thing I’m thinking is that I’m not using “printf”, I’m using “snprintf”, don’t I have to modify the “snprintf” libraries?

Just forgot for every .JSON file in mbe-os folder and let them in origine state.
Then manually create new empty file with name mbed_app.json and place the content inside.

BR, Jan

1 Like

Do I create the file in some subfolder of the project? or do I create the file directly in the project?
sorry for the ignorance.

Directly in project folder. Same place where main.cpp Is.

BR, Jan

1 Like

it should look like this
image

BR, Jan

1 Like

Would something else be missing?, look:

After creating that file, I was going to compile the code, but I get an error and apparently it is because of that file, what could it be? look:

Please be so kind and fill the file exactly acording to my picture above. Thank you.

BR, Jan

1 Like

Ready, I already did it. What should I do now?

Just close the file and perform clean buildy.

BR, Jan

1 Like