Post a new topicPost a reply Page 1 of 1   [ 6 posts ]
Author Message
 Post subject: Possible bug after using sprintf function
PostPosted: Tue Dec 20, 2011 1:51 am 

Joined: Mon Jul 25, 2011 4:15 pm
Posts: 64
Hi,

Code:
// network variables
// destination is to all nodes
char aux[0];
int counterProblem = 0;

void setup()
{
  RTC.ON();
  // 2011, dec, 19, sunday, 6 pm, 00 min, 00 sec
  RTC.setTime( 2011, 12, 19, 2, 18, 0, 0 );
  ACC.ON();
  USB.begin();
 
  counterProblem = 0;
}

void loop()
{
  USB.print("counterProblem=");
  USB.println(counterProblem);
  sprintf(aux,"%d,%d,%d,temp:%d,bat:%d%c%c%c",
          ACC.getX(), ACC.getY(), ACC.getZ(),
          RTC.getTemperature(), PWR.getBatteryLevel(),'%','\r','\n');
  USB.print("counterProblem=");
  USB.println(counterProblem);
  counterProblem++;
  if (counterProblem > 10)
  {
    USB.println("counterProblem is > 10");
    counterProblem = 0;
  }
  delay(1000);
}


The above code will print out the integer counterProblem at the beginning of the loop and after the sprintf function. The values printed will be 0 and 14000 respectively. Why is this happening?


Top
 Profile  
 
 Post subject: Re: Possible bug after using sprintf function
PostPosted: Tue Dec 20, 2011 2:23 am 

Joined: Mon Jul 25, 2011 4:15 pm
Posts: 64
I could call the counterProblem integer anything and it will somehow be increased by 14000 by the sprintf function. I tried using just sprintf without the RTC, ACC, and the PWR and the same result occurs except there is less of an increase to my integer...


Top
 Profile  
 
 Post subject: Re: Possible bug after using sprintf function
PostPosted: Tue Dec 20, 2011 10:12 am 

Joined: Mon Sep 28, 2009 1:06 pm
Posts: 7479
Dear rbhatia,

Looking at your code we see two issues:

1 - why you define char aux[0]? You should define it as longer as your frame is, or using calloc as follows:

Quote:
  char * aux;
  aux = (char) calloc(length,sizeof(char));

(...)

free = aux;
aux=NULL;




2 - I noticed that as you say, there is some trouble using sprintf in your way, try to directly insert the end characters as follows and it will works.

Quote:
  sprintf(aux,"%d,%d,%d,temp:%d,bat:%d%\r\n",ACC.getX(), ACC.getY(),ACC.getZ(),RTC.getTemperature(), PWR.getBatteryLevel());




Kind regards.


Top
 Profile  
 
 Post subject: Re: Possible bug after using sprintf function
PostPosted: Wed Dec 21, 2011 1:13 am 

Joined: Mon Jul 25, 2011 4:15 pm
Posts: 64
Hi libelium-dev,

The spaces between the commas in the sprintf function was the root of the problem. I find this very strange... Anyway, at least it's fixed. I changed the statement for the aux variable to "char* aux" like you said but the other statement:

aux = (char) calloc(length,sizeof(char));

Doesn't work because of length is a missing function.. unless I was supposed to replace length with the length of my aux. It works fine without that statement either.

Thanks,
Ronak


Top
 Profile  
 
 Post subject: Re: Possible bug after using sprintf function
PostPosted: Wed Dec 21, 2011 2:39 am 

Joined: Mon Jul 25, 2011 4:15 pm
Posts: 64
Oh I just figured it out.

Code:
char* aux;
..
aux = (char*) calloc(25,sizeof(char*));

This works when I use:

Code:
aux = get_file();

Code:
*char get_file()


Top
 Profile  
 
 Post subject: Re: Possible bug after using sprintf function
PostPosted: Tue Jan 03, 2012 7:13 pm 

Joined: Mon Sep 28, 2009 1:06 pm
Posts: 7479
Hi,

Yes, that's right. Sorry about the typo. It is correct this way:

Quote:
 char * aux;
  aux = (char*) calloc(length,sizeof(char));

(...)

free = aux;
aux=NULL;



Bets regards


Top
 Profile  
 
Display posts from previous:  Sort by  
Post a new topicPost a reply Page 1 of 1   [ 6 posts ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:


Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
Libelium theme based on 610nm Style by Daniel St. Jules of http://www.gamexe.net


© Libelium Comunicaciones Distribuidas S.L. | Terms of use