CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Big issue with STRTOK with GPS string

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
rald



Joined: 04 Sep 2013
Posts: 25

View user's profile Send private message

Big issue with STRTOK with GPS string
PostPosted: Tue Mar 07, 2017 3:53 pm     Reply with quote

hello everyone,

I have a big issue, I have like 3 days fighting with this but I can't find the correct answer.

I am receiving string from GPS chip via RS232. I can get and read the string but when I try to split it using STRTOK using this code
Code:
prt = strtok(gps, sep);
while(prt != 0){
fprintf(bluetooth, "$s\n",prt);
prt = strtok(0, sep);
}


this example code works fine, show me the list of strings. However when I try to get prt inside an array everything gets wrong and show me everything except the correct string, for example:

That code show me $GPGGA with the first prt, inside an array show the rest of sentence except $GPGGA.

Does anybody have an idea?

Let me know if you need more information.

thanks.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Mar 07, 2017 10:32 pm     Reply with quote

rald wrote:
prt = strtok(gps, sep);
while(prt != 0){
fprintf(bluetooth, "$s\n",prt);
prt = strtok(0, sep);
}
this example code works fine, show me the list of strings. However
when I try to get prt inside an array everything gets wrong and
show me everything except the correct string, for example:

That code show me $GPGGA with the first prt, inside an array show the
rest of sentence except $GPGGA.

You showed us the code that works fine. Now post the code that fails.
Post the code that shows the problem listed below:
rald wrote:
when I try to get prt inside an array everything gets wrong

Show us all details of your code that writes to an array. Show all
variable declarations, too.
rald



Joined: 04 Sep 2013
Posts: 25

View user's profile Send private message

PostPosted: Tue Mar 07, 2017 11:09 pm     Reply with quote

Hi,

Code:
void spliting_string(){
   char sep[2],*original, *prt, array[20];
   int  i=0, x;
   fgets(data, GPS);
   strcpy(sep, ",");
   strcpy(original, data);
   prt = strtok(original, sep);
   while(prt != null){
      array[x] = prt;
      prt = strtok(null, sep);
      x++;
   }
   fprintf(bluetooth, "%s\n\r", array[0];
}


this code should be showing me a "$GPGGA" but instead I am getting 0000.0000,n,0000,000 etc....
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Mar 07, 2017 11:28 pm     Reply with quote

Here, you are copying bytes from one string array to another:
Quote:
strcpy(original, data);

But your destination parameter is wrong. In the line below, you have
declared 'original' as an un-initialized char pointer.
Quote:
char sep[2], *original, *prt, array[20];

You should have declared it as an array. Example:
Code:
char original[20];

Also, an array size of 20 is too small to hold a normal GGA string.
I suggest that you start with a size of 100 for the 'data' and 'original' arrays. Example:
Code:

char data[100];  // fgets() puts the GGA string here
char original[100];
 


Here is what the CCS manual says about strcpy. It says the destination
must be an array, not just a pointer. An array has storage (RAM)
allocated to it. It can hold bytes. That's what you need for strcpy() to work.
Quote:

strcpy( )

Syntax:
strcpy (dest, src)

Parameters:
dest is a pointer to a RAM array of characters.
src may be either a pointer to a RAM array of characters or it may be a
constant string.
rald



Joined: 04 Sep 2013
Posts: 25

View user's profile Send private message

PostPosted: Wed Mar 08, 2017 2:08 pm     Reply with quote

hi,

I made your suggestion but I am getting this data:

A
235959.076
000000.076

The code is this:
Code:

void spliting_string(){
   char dato[100], sep[2], *prt, array[20], original[100];
   int x = 0;
   on(ledstatus);
   fgets(dato, GPS);
   strcpy(sep, ",");
   strcpy(original, dato);
   array[0] = prt = strtok(original, sep);
   while(prt != null){
      array[x] = prt = strtok(null, sep);
      x++;
   }
   fprintf(bluetooth, "%s\n\r", array[0]);
   off(ledstatus);
   /*for(int i = 0;i<=sizeof(array);i++){
      array[i] = "";
   }*/
}

Let me know any other detail you can find.

Thanks a lot.
temtronic



Joined: 01 Jul 2010
Posts: 9226
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed Mar 08, 2017 2:38 pm     Reply with quote

just a thought...

this line...

fgets(dato, GPS);
...
may get you into trouble.

If the GPS fails to send the 'end of string' marker (00), the PIC will stay there forever.
You should add code to abort the fgets() after say xx seconds.

Pretty sure CCS has this in their FAQ section of the manual....

others will know for sure..
Jay
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Mar 08, 2017 3:09 pm     Reply with quote

Post your PIC and your CCS compiler version so I can be running the
same thing as you're running. The compiler version is given at the
top of the .LST file. Example of version numbers: 4.141, 5.057, 5.070
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group