Pages: 1 [2]   Go Down
Author Topic: How Can I merge char variable  (Read 2200 times)
0 Members and 1 Guest are viewing this topic.
Leeds, UK
Offline Offline
Edison Member
*
Karma: 80
Posts: 1729
Once the magic blue smoke is released, it won't go back in!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You could try something like this:

Code:
strcat (coords,"&yr=");
strncat(coords,time,4); //first 4 chars are year
strcat(coords,"&mon=");
strncat(coords,time+4,2); //next 2 are month
strcat(coords,"&day=");
strncat(coords,time+6,2); //next 2 are day
strcat(coords,"&hr=");
strncat(coords,time+8,2); //next 2 are hour
strcat(coords,"&min=");
strncat(coords,time+10,2); //next 2 are minute
strcat(coords,"&sec=");
strcat(coords,time+12); //remainder is seconds

But there ought to be a better way of doing this. Perhaps if you actually posted your code, then one could be suggested.


Part of the trouble you are having is that your coords[100] buffer is not large enough to store the entire concatenated string. Count how long the string is, trust me, its more than 100 characters.
« Last Edit: June 16, 2013, 05:00:58 am by Tom Carpenter » Logged

~Tom~

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 640
Posts: 50335
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
time diaply this : 20130609062400.097

how can I have it in a readable format?
How can you have what in a readable format on what?

You might have better luck with your snippets at http://snippets-r-use.com. Here, we expect (and need) to see ALL of your code. It is possible that you are running out of memory.
Logged

Offline Offline
Sr. Member
****
Karma: 0
Posts: 456
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Dear Tom Carpenter and Paul

Thank for your answer. Tom I am going to try your proposition and I count the amout of caracters but apparently it should be fine, because here are declare the char variable
Code:
char coords[100];
char lon[15];
char lat[15];
char alt[15];
char time[20];
char vel[15];
char msg1[5];
char msg2[5];

@Paul
When I publish that post, I have not niticed the here that number 20130609062400.097
there is year, month etc.
So at the final, I would like to have this in that formatz
Quote
2013-06-09 06:24:00

So to be sincerly, I do not know what it's exacetly this 062400.097. I supposed it's the time, but I do not think that at 6am, I was working....  smiley-confuse

but in order to make sure, I ma going to increate coords to 150
Logged

Il ne suffit pas de tout savoir, la persévérance, c'est déjà presque tout!

New Jersey
Offline Offline
Faraday Member
**
Karma: 70
Posts: 3734
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
So to be sincerly, I do not know what it's exacetly this 062400.097. I supposed it's the time, but I do not think that at 6am, I was working....
It looks like the time as you surmised, with a decimal component to the seconds. As to it not being the time of day you expect, does it make sense if it were Zulu time (GMT) ?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 640
Posts: 50335
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Still with the snippets, eh? When you post ALL of your code, we can help.
Logged

Offline Offline
Sr. Member
****
Karma: 0
Posts: 456
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Dear Paul, It a bit hard becaue it would be really a lot of reading.

The date is generated by a GPS library.

I declare my variabel like this:
Code:
#include "gps.h"
GPSGSM gps;

// GPS
char coords[150];
char lon[15];
char lat[15];
char alt[15];
char time[20];
char vel[15];

and here is a function to get the coords
Code:
void getGPSfix(){
       Serial.println(F(""));
      Serial.println(F("GPS FIX"));
      Serial.println(F("--------------"));
     
        delay(5000);
//Get data from GPS

gps.getPar(lon,lat,alt,time,vel);

       
Serial.print(F("Long :"));
        Serial.println(lon);
        Serial.print(F("Lat :"));
Serial.println(lat);
        Serial.print(F("Alt :"));
Serial.println(alt);
        Serial.print(F("Time :"));
Serial.println(time); // This retunr me something like 20130617062400.345
        Serial.print(F("Vel :"));
Serial.println(vel);

      Serial.println(F(""));
}

Apparently we have to manager the string, but I do not know exactely how to delimeter the time

@Paul,
Is enoguth for you?

In the gps.cpp file, there is that fonction
Code:
char GPSGSM::getPar(char *str_long, char *str_lat, char *str_alt, char *str_time, char *str_speed)
{
char ret_val=0;
char *p_char;
char *p_char1;
gsm.SimpleWriteln("AT+CGPSINF=0");
gsm.WaitResp(5000, 100, "OK");
if(gsm.IsStringReceived("OK"))
ret_val=1;

//longitude
p_char = strchr((char *)(gsm.comm_buf),',');
p_char1 = p_char+1;  //we are on the first char of longitude
p_char = strchr((char *)(p_char1), ',');
if (p_char != NULL) {
          *p_char = 0;
    }
strcpy(str_long, (char *)(p_char1));

// latitude
p_char++;
p_char1 = strchr((char *)(p_char), ',');
if (p_char1 != NULL) {
          *p_char1 = 0;
    }
strcpy(str_lat, (char *)(p_char));

// altitude
p_char1++;
p_char = strchr((char *)(p_char1), ',');
if (p_char != NULL) {
          *p_char = 0;
    }
strcpy(str_alt, (char *)(p_char1));

// UTC time
p_char++;
p_char1 = strchr((char *)(p_char), ',');
if (p_char1 != NULL) {
          *p_char1 = 0;
    }
strcpy(str_time, (char *)(p_char));

// TTFF
p_char1++;
p_char = strchr((char *)(p_char1), ',');
if (p_char != NULL) {
          *p_char = 0;
    }

// num
p_char++;
p_char1 = strchr((char *)(p_char), ',');
if (p_char1 != NULL) {
          *p_char1 = 0;
    }

// speed
p_char1++;
p_char = strchr((char *)(p_char1), ',');
if (p_char != NULL) {
          *p_char = 0;
    }
strcpy(str_speed, (char *)(p_char1));

return ret_val;
}
There is this part, as well.
Code:
// UTC time
p_char++;
p_char1 = strchr((char *)(p_char), ',');
if (p_char1 != NULL) {
          *p_char1 = 0;
    }
strcpy(str_time, (char *)(p_char));
I hope, I provided you enouhg information
« Last Edit: June 17, 2013, 10:52:10 am by pierrot10 » Logged

Il ne suffit pas de tout savoir, la persévérance, c'est déjà presque tout!

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 640
Posts: 50335
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Apparently we have to manager the string, but I do not know exactely how to delimeter the time
You mean that you don't know how to parse the time. You should start with printing the data as obtained from the GPS. Modify the library if needed.

If the date/time stamp in the GPS data looks like 20130617062400.345, then taking 4 characters for the year, 2 for the month, 2 for the day, 2 for the hour (GMT), 2 for the minute, and two for the second seems reasonable. The millisecond portion follows the decimal point, but may be more than you need.
Logged

Mid-Atlantic, USA
Offline Offline
God Member
*****
Karma: 30
Posts: 515
"Remember kids, the only difference between Science and screwing around is writing it down." - Adam Savage
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Apparently we have to manager the string, but I do not know exactely how to delimeter the time
You mean that you don't know how to parse the time. You should start with printing the data as obtained from the GPS. Modify the library if needed.

If the date/time stamp in the GPS data looks like 20130617062400.345, then taking 4 characters for the year, 2 for the month, 2 for the day, 2 for the hour (GMT), 2 for the minute, and two for the second seems reasonable. The millisecond portion follows the decimal point, but may be more than you need.

And, because this is a GPS generating the time, it would be UTC not your local time (unless you happen to be in GMT and either don't use summer time, or it is winter). Because timezones are defined politically, not necessarily geographically, it would take a huge database (probably larger than an UNO could hold) to be able to accurately figure out the timezone correction based on the Lat/Lon/date for every point on the Earth...
Logged


Offline Offline
Sr. Member
****
Karma: 0
Posts: 456
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

by the bay, what it's the limite of a buffer

Code:
coords[100]

I change it to 300 and nothing was working...
Logged

Il ne suffit pas de tout savoir, la persévérance, c'est déjà presque tout!

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 640
Posts: 50335
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
what it's the limite of a buffer
The limit is when the heap and the stack overlap. Since both are dynamically modified at run time, there is no fixed limit.

Quote
I change it to 300
Why? The 90 characters you were storing fit in the 100 element array.
Logged

Leeds, UK
Offline Offline
Edison Member
*
Karma: 80
Posts: 1729
Once the magic blue smoke is released, it won't go back in!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Why? The 90 characters you were storing fit in the 100 element array.

But on top of those 90 characters are the extra 20 characters of text such as "&long=", so 100 characters is possibly not quite enough. Maybe 110 or so would be better.
Logged

~Tom~

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 640
Posts: 50335
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
But on top of those 90 characters are the extra 20 characters of text such as "&long=", so 100 characters is possibly not quite enough. Maybe 110 or so would be better.
What is not clear is why the data needs to be copied from existing char arrays into one huge array. There are methods in the most output libraries to output data in smaller chunks. The system on the other end won't be able (generally) to tell the difference between 110 output statements and one output statement.
Logged

Johannesburg, South Africa
Offline Offline
Full Member
***
Karma: 0
Posts: 180
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset



time diaply this : 20130609062400.097

how can I have it in a readable format?

many thank

That is a readable format. Look again: 2013 (year) 06 (month) 09 (day) 06 (hour) 24 (minute) 00.097
Logged

Pages: 1 [2]   Go Up
Jump to: