Sending data through serial port formated with sprintf

Hello,

Sorry if this is a bit silly question, but I’m a bit confused.

I’m sending data throught serial port with this :

char serialSend[10];
sprintf(serialSend, "%d=%d", j, _analogValue[j]);
Serial.println(serialSend);

( obviously there is more code but these are the lines I don’t know how they work and I guess there is no need for more info of the code )

The code works ok. I receive messages on the device connected to the arduino like these :

1=177+carrieagereturn
2=234+carrieagereturn
3=1024+carrieagereturn

But, as long as I think, I have some doubts. Given that I have a char array[10], when I format with sprintf the data and it is stored in the char array, what happens to the values that are not filled by sprintf?

if I had { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } after the sprintf I would have something like { 49, 61, 49, 55, 55, x, x, x, x, x }

Wich values are introduced by the sprintf in the remaining positions of the array which are not needed ( the x )?

How println knows that the remaining values are not requiered and only sends the first five? I guess, sprintf will fill with a 0X00 or 0XFF value in the remaining values. But, what happens if I what to send a 0x00 or 0xFF value?

This comes to me as I want to add a CRC8 byte. So, I was wondering what would happen if the value given by the CRC is the one wich is not shown by sprintf. Would it be sent with Serial.println or should I use other code?

sprintf() will put the values listed in the function into the specified buffer formatted in the way specified by the formatting string. So, you tell it to include the value of j and that it is an integer, followed by an equals sign and then the value of element j of array _analogValue, also as an integer. When you print the buffer using Serial.println() a newline character is printed after the buffer because you told it to by using println()

Given that I have a char array[10], when I format with sprintf the data and it is stored in the char array, what happens to the values that are not filled by sprintf?

Nothing unless you explicitly use them in the sprintf()

I was wondering what would happen if the value given by the CRC is the one wich is not shown by sprintf. Would it be sent with Serial.println or should I use other code?

You could either use sprinf() to put it in a buffer then print that or you could print it explicitly using say Serial.print() or Serial.println()

Serial.print will print characters until it finds a '\0' (the null terminating character for c strings), which is added at the end of the string by sprintf

For example

char serialSend[10]; // declaration without initialization

sprintf(serialSend, "%d=%d", 1, 2); // serialSend now contains 4 characters "1=2\0" followed by 6 bytes of garbage (because you didn't initialize the array)

Serial.println(serialSend); // will print until it finds '\0', so "1=2"

Thanks, it's clearer now.

Your program will probably crash if sprintf writes past the array end. Better to use snprintf()

char serialSend[10];

Or you could just wrap it in a SafeString (my lib)

#include <SafeString.h> 
// available from the library manager
..
cSFA(sfStr,serialSend); // create a SafeString from a char array
sfStr.clear(); 
sfStr.print(1); sfStr.print('='); sfStr.println(2);
Serial.print(sfStr);

SafeString won’t let you overrun the length of your serialSend char array
By the way, why not just print directly to the Serial?

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.