system
June 29, 2014, 3:04pm
1
Hi.
I'm using Serial.print/ln commands to debug some code.
So here is part of my code:
void timeSyncNode(byte node, char* timeCommand)
{
Serial.print("command1:"); Serial.println(timeCommand);
Serial.print("command2:"); Serial.println(timeCommand);
...
Output is OK:
command1:T1234567897
command2:T1234567897
But now if I add another print line between those two outputs (type is byte), then last command's output is wrong:
void timeSyncNode(byte node, char* timeCommand)
{
Serial.print("command1:"); Serial.println(timeCommand);
Serial.print("node:"); Serial.println(node);
Serial.print("command2:"); Serial.println(timeCommand);
Output of 3rd line is wrong:
command1:T1234567897
node:2
command2:T?
Could you guys explain it to me why is this happening ? "command2" output should be same as "command1" ?
can you post the complete code ?
(or minimize the code to an example that shows the problem)
I suspect one of the parameters is not right or you are out of memory.
system
June 29, 2014, 3:32pm
3
I suspect that your string is NOT null-terminated. Therefore, the code is finding a NULL somewhere else, and using that to terminate your string. Pass a properly NULL terminated array to the function.
system
June 29, 2014, 4:08pm
4
PaulS:
Therefore, the code is finding a NULL somewhere else, and using that to terminate your string.
This is me being picky but the '\0' string terminator character is called NUL and not NULL. NULL is something else.
system
June 29, 2014, 4:17pm
5
Thank you all for your replies.
Ok here is now I call timeSyncNode function:
char* timeSyncMessage()
{
time_t t = now();
char buf[88];
snprintf(buf, 16, "%c%lu",TIMEHEADER, t); //TIMEHEADER IS "T". I should get T + 10 digits here
return buf;
}
#include <RFM69.h>
#include <Time.h>
RFM69 radio;
...
byte nodeId = radio.SENDERID;
char* time = timeSyncMessage();
timeSyncNode(nodeId,time);
system
June 29, 2014, 4:54pm
6
Why is buf allocated on the stack if you're going to return its address?
system
June 29, 2014, 4:58pm
7
bwat:
Why is buf allocated on the stack if you're going to return its address?
Now we know why your code doesn't work. You can NOT do this.
system
June 29, 2014, 6:22pm
8
Sorry, I'm little rusty with C.
Could you guys please tell me what's the right way to do it ?
What I want is to return:
"T" + current timestamp, which I get with now() function (time_t type).
Example: T1234567890
Something like:
char* timeSyncMessage()
{
time_t t = now();
char *buf;
snprintf(buf, 16, "%c%lu",TIMEHEADER, t);
return buf;
}
system
June 29, 2014, 8:17pm
10
Thanks, that works fine.
Is there any better way to concatenate that "T" char and current time in sec. (time_t type) ?
like this?
char* timeSyncMessage()
{
static char buf[16];
snprintf(buf, 16, "T%lu", now());
return buf;
}
system
June 29, 2014, 8:38pm
12
I mean if usage of "snprintf" is the right way to do it ? Otherwise I should probably use strcpy/strcat ?
Thanks for your help.
what does now() returns? an unsigned long?
make a loop and add one digit at the time, probably fastest.
system
June 29, 2014, 8:50pm
14
Function now() (from library Arduino Playground - Time ) returns time_t , which is probably int or long. I'll try to do it with loop.
Thanks again for your help.