Hello,
I am quite a C++ newbie and arrived at a point of desperation...
First of all I receive Data from the serial interface, 6 different data sets which are sent from an external device. I split those data sets and put them into classes, furthermore I designed one class "Data" which contains one instance of each of those 6 classes
Then I've written a class Buf, which is an circular buffer (FIFO). I want to store 50 objects of the class Data. I fill a Object of the class Data, and when it's full, I want to append it to my Buf. That works, but when the object of the class Data is filled again with the next package of data sets, the actual data set overwrites the already appended object of my Buf - I gain every time the same data in my Buf.
I'm sure that I made supposably just a little mistake, which has this great consequence, but by now I'm quite mixed and haven't got any idea left what to do.
I'm really greatful for any kind of help and posted some of my code - hope that somebody out there could find my fault...
Thanks a lot for your thoughts,
linden99
//class Buf (circular buffer)
# define BUFSIZE 50
class Buf
{
public:
Buf();
virtual ~Buf();
bool Append(void* newthing);
void *First(void);
void *Last(void);
int Length(void) const;
int Full(void) const;
int Empty(void) const;
private:
void *data[BUFSIZE];
int bufapp;
int bufget;
int bufcount;
};
inline int Buf::Length(void) const { return bufcount; }
inline int Buf::Full(void) const { return bufcount == BUFSIZE; }
inline int Buf::Empty(void) const { return bufcount == 0; }
bool Buf::Append(void *newthing)
{
if (bufcount {
printf("%d", bufapp);
data[bufapp] = newthing;
bufapp++;
if(bufapp == BUFSIZE)
bufapp = 0;
bufcount++;
return (true);
}
else
return (false);
}
void *Buf::First(void)
{
void* temp = data[bufget];
bufget++;
if(bufget == BUFSIZE)
bufget = 0;
bufcount--;
return temp;
}
void *Buf::Last(void)
{
void *temp=NULL;
if (bufapp>0)
{
temp = data[bufapp-1];
return temp;
}
else
return temp;
}
///////////////////////////////////
// part of main()
////////////////////////////////
Buf buffer;
Data *newitem=NULL;
Data *lastitem=NULL;
Data *appitem=NULL;
int lasttime;
int newtime;
char lastmin[5];
char newmin[5];
//if all data sets are completely filled
if (bgga==true && bgsa==true && bgsv==true && brmc==true && brme==true && brmm==true)
{
appitem=newitem;
int len=buffer.Length();
if (len>0)
{
lastitem=new Data();
lastitem=(Data*) buffer.Last();
midstr(lastmin, lastitem->gga.getggautc(), 2, 2);//cuts the minutes out of the string
midstr(newmin, newitem->gga.getggautc(), 2, 2);
newitem=NULL;
newitem=new Data();
lasttime=atoi(lastmin);
newtime=atoi(newmin);
switch(lasttime)
{
case(59): //just in case one hour was transgressed
if (newtime==0)
{
if(buffer.Append(appitem))
printf("\n\n\ok!\n\n\n");
else
printf("\n\n\not ok!\n\n\n");
}
default:
if (newtime==lasttime+1)
{
if(buffer.Append(appitem))
printf("\n\n\ok!\n\n\n");
else
printf("\n\n\not ok!\n\n\n");
}
} // end switch
}// End if len>0
else //if there hasn*t been any data inside the circular buffer yet, it has to be appended
{
if(buffer.Append(appitem))
printf("\n\n\od!\n\n\n");
else
printf("\n\n\n not ok!\n\n\n");
appitem=NULL;
newitem=NULL;
newitem=new Data();
}
bgga=false;
bgsa=false;
bgsv=false;
brmc=false;
brme=false;
brmm=false;
}
I am quite a C++ newbie and arrived at a point of desperation...
First of all I receive Data from the serial interface, 6 different data sets which are sent from an external device. I split those data sets and put them into classes, furthermore I designed one class "Data" which contains one instance of each of those 6 classes
Then I've written a class Buf, which is an circular buffer (FIFO). I want to store 50 objects of the class Data. I fill a Object of the class Data, and when it's full, I want to append it to my Buf. That works, but when the object of the class Data is filled again with the next package of data sets, the actual data set overwrites the already appended object of my Buf - I gain every time the same data in my Buf.
I'm sure that I made supposably just a little mistake, which has this great consequence, but by now I'm quite mixed and haven't got any idea left what to do.
I'm really greatful for any kind of help and posted some of my code - hope that somebody out there could find my fault...
Thanks a lot for your thoughts,
linden99
//class Buf (circular buffer)
# define BUFSIZE 50
class Buf
{
public:
Buf();
virtual ~Buf();
bool Append(void* newthing);
void *First(void);
void *Last(void);
int Length(void) const;
int Full(void) const;
int Empty(void) const;
private:
void *data[BUFSIZE];
int bufapp;
int bufget;
int bufcount;
};
inline int Buf::Length(void) const { return bufcount; }
inline int Buf::Full(void) const { return bufcount == BUFSIZE; }
inline int Buf::Empty(void) const { return bufcount == 0; }
bool Buf::Append(void *newthing)
{
if (bufcount {
printf("%d", bufapp);
data[bufapp] = newthing;
bufapp++;
if(bufapp == BUFSIZE)
bufapp = 0;
bufcount++;
return (true);
}
else
return (false);
}
void *Buf::First(void)
{
void* temp = data[bufget];
bufget++;
if(bufget == BUFSIZE)
bufget = 0;
bufcount--;
return temp;
}
void *Buf::Last(void)
{
void *temp=NULL;
if (bufapp>0)
{
temp = data[bufapp-1];
return temp;
}
else
return temp;
}
///////////////////////////////////
// part of main()
////////////////////////////////
Buf buffer;
Data *newitem=NULL;
Data *lastitem=NULL;
Data *appitem=NULL;
int lasttime;
int newtime;
char lastmin[5];
char newmin[5];
//if all data sets are completely filled
if (bgga==true && bgsa==true && bgsv==true && brmc==true && brme==true && brmm==true)
{
appitem=newitem;
int len=buffer.Length();
if (len>0)
{
lastitem=new Data();
lastitem=(Data*) buffer.Last();
midstr(lastmin, lastitem->gga.getggautc(), 2, 2);//cuts the minutes out of the string
midstr(newmin, newitem->gga.getggautc(), 2, 2);
newitem=NULL;
newitem=new Data();
lasttime=atoi(lastmin);
newtime=atoi(newmin);
switch(lasttime)
{
case(59): //just in case one hour was transgressed
if (newtime==0)
{
if(buffer.Append(appitem))
printf("\n\n\ok!\n\n\n");
else
printf("\n\n\not ok!\n\n\n");
}
default:
if (newtime==lasttime+1)
{
if(buffer.Append(appitem))
printf("\n\n\ok!\n\n\n");
else
printf("\n\n\not ok!\n\n\n");
}
} // end switch
}// End if len>0
else //if there hasn*t been any data inside the circular buffer yet, it has to be appended
{
if(buffer.Append(appitem))
printf("\n\n\od!\n\n\n");
else
printf("\n\n\n not ok!\n\n\n");
appitem=NULL;
newitem=NULL;
newitem=new Data();
}
bgga=false;
bgsa=false;
bgsv=false;
brmc=false;
brme=false;
brmm=false;
}