 |
 |
| View previous topic :: View next topic |
| Author |
Message |
bkamen
Joined: 07 Jan 2004 Posts: 1619 Location: Central Illinois, USA
|
|
Posted: Mon Oct 26, 2009 7:16 am |
|
|
Again,
You didn't study the code I sent you.
This routine: void gps_get_timedate ( unsigned char *s, unsigned int which ) {
Does that for you and saves time and date for you in a format I think you want. _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
 |
hayee
Joined: 05 Sep 2007 Posts: 252
|
|
Posted: Tue Oct 27, 2009 3:10 am |
|
|
Not again.
I studied the code and didn't understand the routine how it is working.
Could you explain what is going on in that routine, specially what is the function of strncpy and memset?
Thanks |
|
 |
bkamen
Joined: 07 Jan 2004 Posts: 1619 Location: Central Illinois, USA
|
|
Posted: Tue Oct 27, 2009 10:30 pm |
|
|
| hayee wrote: | Not again.
I studied the code and didn't understand the routine how it is working.
Could you explain what is going on in that routine, specially what is the function of strncpy and memset?
|
That routine is called by the parsing routine that looks at the sentences for the various keywords.
When it has something it likes, it uses strncpy to copy it to a temp string and then passes that to the routine mentioned in the last post. That routine takes the string and saves the time or the date accordingly.
study study and study that code some more.
Good luck,
-Ben _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
 |
hayee
Joined: 05 Sep 2007 Posts: 252
|
|
Posted: Wed Oct 28, 2009 3:54 am |
|
|
I have read the code and understand the code.
But Problem is that i am unable to convert the string into int by using atoi command.
for example
I have an array in which data is store.Now i am going to seperate hours,mins and secs from it.
| Code: |
unsigned char gps_string[28];//contains data recived from gps.
unsigned char hour,min,sec;
unsigned int hour1,min1,sec1;
while(1)
{
...
for (i=0;i<2;i++)
{
hour[i]=gps_string[7+i];//save hour characters
}
hour1=atoi(hour);//conversion into int
for (i=0;i<2;i++)
{
min[i]=gps_string[9+i];//save mins charaters
}
min1=atoi(min);//conversion into int
for (i=0;i<2;i++)
{
sec[i]=gps_string[11+i];//save secs characters
}
sec1=atoi(sec);//conversion into int
}
|
ints values are not right.
why it is? |
|
 |
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Wed Oct 28, 2009 4:09 am |
|
|
Your code isn't right.
How can you access unsigned char hour,min,sec; as array, e.g. min[i]?
Furthermore, you don't put a delimiting 0 to the strings.
You can write 0 string delimiter and apply atoi() to the gps_string, without copying it previously. But must work right to left, because gps_string is overwritten in part. |
|
 |
hayee
Joined: 05 Sep 2007 Posts: 252
|
|
Posted: Wed Oct 28, 2009 4:49 am |
|
|
FvM.
I am not getting your point,Plz explain. |
|
 |
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Wed Oct 28, 2009 5:14 am |
|
|
O.K. I'll stay with the elementary stuff.
This code doesn't work:
| Code: | unsigned char hour,min,sec;
unsigned int hour1,min1,sec1;
while(1)
{
...
for (i=0;i<2;i++)
{
hour[i]=gps_string[7+i];//save hour characters
}
hour1=atoi(hour);//conversion into int |
Change it as below:
| Code: | unsigned char hour[3]; // must be defined as an array of sufficient size
unsigned int hour1,min1,sec1;
while(1)
{
...
for (i=0;i<2;i++)
{
hour[i]=gps_string[7+i];//save hour characters
}
hour[2]=0; // A string needs a 0 delimiter
hour1=atoi(hour);//conversion into int |
|
|
 |
hayee
Joined: 05 Sep 2007 Posts: 252
|
|
Posted: Wed Oct 28, 2009 5:27 am |
|
|
Thanks FvM its now working.
But what is the reason behind the delimiter why we need to use it.
e.g
hour[2]=0; // A string needs a 0 delimiter |
|
 |
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Wed Oct 28, 2009 5:32 am |
|
|
Without a delimiter, a string function can't know where the string ends. It doesn't work by telepathy, as far as I know . |
|
 |
bkamen
Joined: 07 Jan 2004 Posts: 1619 Location: Central Illinois, USA
|
|
Posted: Wed Oct 28, 2009 11:31 am |
|
|
FvM: Thanks for jumping in, I just turned on my computer. :D
Hayee: The code I originally gave you parses this information automatically, properly terminates and converts into integers for you. I don't understand why you are still trying to duplicate code when you say you understand how the code I gave you works.
So let's look at your code for a sec and expand on what FvM is saying:
| Code: |
for (i=0;i<2;i++)
{
hour[i]=gps_string[7+i];//save hour characters
}
hour1=atoi(hour);//conversion into int
|
You have a loop that takes gps_string starting at location 7 + i and adds it to your hour var. It doesn't guarantee a terminating null which is needed by the standard string functions. So that will break.
Now let's look at the code I sent you:
| Code: |
void gps_get_timedate ( unsigned char *s, unsigned int which ) {
unsigned char work[4];
unsigned int x,y,z;
strncpy(work, s, 2);
x = atoi(work);
memset(work, 0, sizeof(work));
strncpy(work, s+2, 2);
y = atoi(work);
memset(work, 0, sizeof(work));
strncpy(work, s+4, 2);
z = atoi(work);
switch (which) {
case 0:
gps_info.d = x;
gps_info.m = y;
gps_info.y = z;
break;
case 1:
gps_info.hh = x;
gps_info.mm = y;
gps_info.ss = z;
break;
}
}
===============================================
void gps_process_packet ( void ) {
char string[84]="", header[7]="", separator[2]="", work[20]="";
unsigned int ptr, index = 0, which = 0;
strcpy(string, gps_string); // make a copy of the current string
gps_have_packet = 0; // reset the flag since we can move on without.
strcpy( header, "$PGRMF"); // The string we want to parse
strcpy( separator, ","); // the separators in the sentence is a comma
if ( strncmp(header, string, 6) == 0 ) { // is the beginning of our string what we want? If yes, then process
if ( strlen(string) > 70 ) { // this is here because a valid GPS signal gives us longer sentences which puts our info in different places.
ptr = strtok(string, separator); // now use the string to token routine and then process each token below.
while (ptr!=0) {
switch (index) {
// based on token position as delimited by the comma
// do something with the information we have
case 4: which = 1; // Time
case 3: // Date
gps_get_timedate(ptr, which);
break;
case 6: which = 1; // LAT ddmm.mmmm
case 8: // LON
gps_get_latlon(ptr, which);
break;
case 7: which = 1; // LAT N/S
case 9: // LON E/W
gps_get_nsew(ptr, which);
break;
case 11: // Fix Type 0=None, 1=2D, 2=3D
gps_info.fix = atoi(ptr);
break;
case 12: // Speed
gps_get_speed(ptr);
break;
}
which = 0;
memset(work, 0, sizeof(work) );
ptr = strtok(0, separator);
index++;
}
} else { // we don't have a lock, but we still get valid time and date from the GPS, so because it's in a different token position, use this set of indexes.
ptr = strtok(string, separator);
while (ptr!=0) {
switch (index) {
case 1: which = 1; // Time
case 2: // Date
gps_get_timedate(ptr, which);
break;
case 8: // Fix Type 0=None, 1=2D, 2=3D
gps_info.fix = atoi(ptr);
break;
}
which = 0;
memset(work, 0, sizeof(work) );
ptr = strtok(0, separator);
index++;
}
}
}
}
|
ptr is a pointer to the string which has been filled with nulls to terminate each "token" as defined by the comma. So whenever the time/date or lat/lon routine is called, that routine is given a pointer to what information needs to be pulled from the gps string. Because I used strncpy (copy n chars from one string to another) it automatically places the final null in the destination string for me.
Doing what you're doing explicitly copies one to the next char exactly as you want. Missing the final '\0' (null) that atoi() needs to figure out where the string ends.
I've used this same code on 2 different GPS's now with the obvious needed changes for configuring and controlling the GPS, but the ISR and parsing routines all stayed the same after adjusting the index locations of the tokens in the strings I wanted..
-Ben _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
 |
|
|
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
|