cancel
Showing results for 
Search instead for 
Did you mean: 

How to send float values through serial com?

sali.1
Associate
 
3 REPLIES 3
php444
Associate

you should use sprintf

char v[4];

sprintf(v,"%.2f",your_var);

then send var v on serial

Likely going to want more than 4 bytes to hold the string and NUL

Do you want to send the data in an ASCII form, or the form it is stored in memory?

For strings also fota() and dtoa() functions.

https://www.geeksforgeeks.org/convert-floating-point-number-string/

https://chromium.googlesource.com/native_client/nacl-newlib/+/master/newlib/libc/stdlib/dtoa.c

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
sali.1
Associate

#include <stdio.h>

char buffer[200];

int i, j;

double fp;

char *s = "baltimore";

char c;

int main(void)

{

  c = 'l';

  i = 35;

  fp = 1.7320508;

  /* Format and print various data */

  j = sprintf(buffer, "%s\n", s);

  j += sprintf(buffer+j, "%c\n", c);

  j += sprintf(buffer+j, "%d\n", i);

  j += sprintf(buffer+j, "%f\n", fp);

  printf("string:\n%s\ncharacter count = %d\n", buffer, j);

}

/********************* Output should be similar to: *************

string:

baltimore

l

35

1.732051

character count = 24      */