Writing integer or float datatype on UART serial console

Hi,

    We are using synergy DK-S128 evaluation board and we are working on UART.With USB to serial converter We are able to print normal "Hello World" as below

 uint8_t message[] = "Hello World!\n\r";
g_uart0.p_api->write(g_uart0.p_ctrl, message,sizeof(message));

But we need to write the floating or integer value to UART like

int vr_res=10;
vr_res=vr_res/100;
g_uart0.p_api->write(g_uart0.p_ctrl, &vr_res,sizeof(vr_res));

With this we are not getting expected data on serial console.

Is this the correct way to print the integer value over serial console? 

If not what is the correct way to print the integer or float on serial console?

 

 

  • You will need to covert the integer or float value to a string to print it to the terminal :-

    #include "stdio.h"

        uint8_t buffer[16];

        int i = 100;

        float f = 2.0f;

        snprintf((char *)buffer, sizeof(buffer), "%i %2.2f\r\n", i, f);

     

    and then write the  string in the buffer using the UART write() API :-

    g_uart0.p_api->write(g_uart0.p_ctrl, &buffer, strlen(buffer));

     

     

    Also, you will have to enable float support for printf family of API with newlib-nano :-

  • Hello Abhi,

    Did you try the solution suggested by Jeremy? Did it help you?

    Kind regards,