How to send float values through serial com?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2022-04-24 3:18 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2022-04-24 6:13 AM
you should use sprintf
char v[4];
sprintf(v,"%.2f",your_var);
then send var v on serial
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2022-04-24 10:13 AM
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
Up vote any posts that you find helpful, it shows what's working..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2022-04-25 6:06 AM
#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 */
