problem with receiving variables after parsing

Hi all,

I have a problem with this function... The dumpingData() function is repeated several times and the SerialPrint gives me back the variables i parsed the string into.
Now I have a problem, every Time the Serial Out gives me back one further letter...

for example:
I send:
$GPSDAT,210102,V,50.773305,N,06.091255,E,000.0,000.0,00.0,02,*

it gives me back
$
210102
V
blablabla... which is correct

and the next time i receive
G //why is this a G???? I want the $ back
210102
V
blablabla

and then
P //why is this a P???? I want the $ back
210102
V
blablabla

Any idea how to fix that? when i pass the , i just receive rubbish..

best regards,
hendrik

char buf[65];

void dumpingData()
{ 
   while (stringComplete == false) 
  {
    if (Serial.available() >0)
    {
      char c = Serial.read();
      buf[idx++] = c;
      buf[idx] = 0;  // terminator...
      stringComplete = (c == '*');
    }
  }

char *ptr=NULL;
ptr=strtok(buf,",");
  
if (NULL!=ptr)
{
  gpsdatin=*ptr; // ptr points to $
  Serial.print(gpsdatin); 
  Serial.println();
}
if ( NULL != (ptr=strtok(NULL,",")) )
{
  timein=atof(ptr);
  Serial.print(timein); //ptr points to timein
  Serial.println();
}
if ( NULL != (ptr=strtok(NULL,",")) )
{
 validin=*ptr;
 Serial.print(validin); 
 Serial.println();// ptr points to validin or Not validin
}
if ( NULL != (ptr=strtok(NULL,",")) )
{
 latin = atof(ptr);
 Serial.print(latin,6); // ptr points to latin
 Serial.println();
}
if ( NULL != (ptr=strtok(NULL,",")) )
{
 NSin = *ptr;
 Serial.print(NSin); // ptr points to N/S
 Serial.println();
}
if ( NULL != (ptr=strtok(NULL,",")) )
{
 lonin = atof(ptr);
 Serial.print(lonin,6); // ptr points to lonin
 Serial.println();
}
if ( NULL != (ptr=strtok(NULL,",")) )
{
 EWin = *ptr;
 Serial.print(EWin); // ptr points to EWin
 Serial.println();
}
if ( NULL != (ptr=strtok(NULL,",")) )
{
 speedin = atof(ptr);
 Serial.print(speedin); // ptr points to speedin
 Serial.println();
}
if ( NULL != (ptr=strtok(NULL,",")) )
{
 headingin = atof(ptr);
 Serial.print(headingin); // ptr points to headingin
 Serial.println();
}
if ( NULL != (ptr=strtok(NULL,",")) )
{
 altitudein = atof(ptr);
 Serial.print(altitudein); // ptr points to altitudein
 Serial.println();
}
if ( NULL != (ptr=strtok(NULL,",")) )
{
 satin = atof(ptr);
 Serial.print(satin); // ptr points to #satin
 Serial.println();
}
if ( NULL != (ptr=strtok(NULL,",")) )
{
 endstring=*ptr; // ptr points to *
  Serial.print(endstring); 
  Serial.println();
}
*ptr=NULL;
stringComplete = false;
buf[0] = '\0';
}
  gpsdatin=*ptr; // ptr points to $

You are assigning the address of the pointer to gpsdatin, not the contents pointed to. Get rid of the *.

hmm.. that doesn't work... I also tried to comment the whole gpsdatin=*ptr; out... but still the same problem. If i delete the * it gives me an invalid conversion error. In fact the parsing into the variable works quite well... the problem is that the pointer always points to the next character in the buffer everytime i resend a string

*ptr=NULL;

This is wrong, too. The value NULL should be assigned to ptr, not written to where ptr currently points.

Writing data to the serial monitor, with no identifying information makes it very hard to understand what value corresponds to what variable, so it's hard to say what is going wrong.

Instead of:

  Serial.print(gpsdatin); 
  Serial.println();

use:

Serial.print("gpsdatin: [");
Serial.print(gpsdatin); 
Serial.println("]");

That way, you'll know right away which value is printing incorrectly.

Also, try printing, with identifier, the string before parsing it.

Finally, it would be necessary to know all the variable types you are using to see where things are going wrong.

For instance, is gpsdatin is a pointer, then gpsdatin = ptr; should work. If gpsdatin is a char array, then strcpy(gpsdatin, ptr); might be necessary. It's hard to say without knowing gpsdatin's type.

PaulS:

  gpsdatin=*ptr; // ptr points to $

You are assigning the address of the pointer to gpsdatin, not the contents pointed to. Get rid of the *.

This is nonsense. ptr is the address, *ptr is the char pointed to. Removing the * is an error of the hopefully-doesn't-compile variety.

Removing the * is an error of the hopefully-doesn't-compile variety.

No it isn't. It's a way of assigning a known, recognized as not-pointing-to-anything-valid, value to a pointer. Not only is it not an error, it is a recommended practice, and a darn good idea.

char buf[65];

void dumpingData()
{
  while (stringComplete == false)
  {
    if (Serial.available() >0)
    {
      char c = Serial.read();
      buf[idx++] = c;
      buf[idx] = 0;  // terminator...
      stringComplete = (c == '*');
    }
  }

Let's hope you don't get more than 65 characters, eh?

Where is idx defined? How is it defined? When is it set to zero?

 if (NULL!=ptr)

{
    gpsdatin=*ptr; // ptr points to $
    Serial.print(gpsdatin);
    Serial.println();
  }

How and were is gpsdatin defined?

You are a tiny bit optimistic saying "ptr points to $".

*ptr=NULL;

I agree with PaulS here. You are setting the location that ptr is pointing to, to zero.

 buf[0] = '\0';

Why are you doing this?

PaulS:

  gpsdatin=*ptr; // ptr points to $

You are assigning the address of the pointer to gpsdatin, not the contents pointed to. Get rid of the *.

No, Paul. *ptr dereferences the pointer. However I would like to see the definition for gpsdatin.

Jabberwox:
and then
P //why is this a P???? I want the $ back

Answer my question about idx and I think you will see.

Thanks Nick,
I'm sorry, I was off during the weekend..

//following variables for incoming data
char buf[65];
int idx = 0;
boolean stringComplete;

char gpsdatin;
char endstring;
long timein;
char validin;
float latin;
char NSin;
float lonin;
char EWin;
float speedin;
float headingin;
float altitudein;
int satin;

These are global variables. I just want to have the $-Symbol in the gpsdatin... thanks so far, hendrik

Jabberwox:

...

int idx = 0;
...

Yes, and it works the first time, right? How about the second time? When do you set it to zero the second time?

ahh, damnit, thx :wink: