|
|
View previous topic :: View next topic |
Author |
Message |
rald
Joined: 04 Sep 2013 Posts: 25
|
Big issue with STRTOK with GPS string |
Posted: Tue Mar 07, 2017 3:53 pm |
|
|
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
|
|
Posted: Tue Mar 07, 2017 10:32 pm |
|
|
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
|
|
Posted: Tue Mar 07, 2017 11:09 pm |
|
|
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
|
|
Posted: Tue Mar 07, 2017 11:28 pm |
|
|
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:
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
|
|
Posted: Wed Mar 08, 2017 2:08 pm |
|
|
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
|
|
Posted: Wed Mar 08, 2017 2:38 pm |
|
|
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
|
|
Posted: Wed Mar 08, 2017 3:09 pm |
|
|
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 |
|
|
|
|
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
|