Go Down

Topic: Do I need a special library for strcpy and strcat (Read 16910 times) previous topic - next topic

pierrot10

Jul 13, 2013, 09:05 am Last Edit: Jul 13, 2013, 09:11 am by pierrot10 Reason: 1
Hello,

I do not understand something.
Previously this code did not make trouble, but now I create a new file and when I comment de following code, my terminal display the correct message, but when I uncomment it, the terminal starts displaying a lot of caracter and notinh work.


Code: [Select]

char coords[100]; // I also tryed with 110
char lon[15];
char lat[15];
char alt[15];
char time[20];
char vel[15];
char msg1[5];
char msg2[5];

strcpy (coords,"lat=");
        strcat (coords,lat);
     
        strcat (coords,"&long=");
        strcat (coords,lon);
     
        strcat (coords,"&alt=");
        strcat (coords,alt);
     
        strcat (coords,"&vel=");
        strcat (coords,vel);
     
        strcat (coords,"&time=");
        strcat (coords,time);


As I am creating  a new "clean" file, I asked my self if I miss something like a library?
Il ne suffit pas de tout savoir, la persévérance, c'est déjà presque tout!
You can not know everything, perseverance, it is almost everything!

pierrot10

Il ne suffit pas de tout savoir, la persévérance, c'est déjà presque tout!
You can not know everything, perseverance, it is almost everything!

AWOL

You're probably out of RAM or stomping on some RAM you don't own, but it is impossible to tell because YOU DIDN'T POST YOUR CODE.

UKHeliBob

In your code snippet (it would be better to see the whole program) none of the char arrays are initialised, so they won't have a null terminator which strcat needs to find the end of the string, so all bets are off as to what happens.

You are right to consider the size of the array, particularly if you ever include the msg labels an variables.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

PeterH

The code you posted is obviously not your actual code, and is not even a snippet of your actual code; you need to post your actual code if you want people to understand what it's doing. If your sketch is large and contains lots of code not relevant to the problem then create a simpler sketch which demonstrates the problem in the simplest way you can. Quite often, the mere act of creating this will enable you to spot some false assumption and solve the problem for yourself, but in any case it saves all of us from wading through lots of irrelevant  code. Before posting the sketch please check that it does actually run and demonstrate the problem, include any input of output necessary for us to understand what happens when the problem occurs, and use tools/auto format to tidy the code up.

pierrot10

#5
Jul 13, 2013, 08:28 pm Last Edit: Jul 14, 2013, 08:44 pm by pierrot10 Reason: 1
Dear All,

Sorry if I did not provide enough code.
I hope it will better help you to help me

All of the code works excepted the function getGPSCoords(); As I stated, if the strcopy and strcat are commented, it works. If they are uncomment, the program crash...

Would it be another way to concatenate char variable

Thank a lot for all of your

Code: [Select]

#include "SIM900.h"
#include <SoftwareSerial.h>
#include "inetGSM.h"
#include "sms.h"
//#include "call.h"
#include "gps.h"

InetGSM inet;
//CallGSM call;
SMSGSM sms;
GPSGSM gps;

char coords[110];
char lon[15];
char lat[15];
char alt[15];
char time[20];
char vel[15];
char msg1[5];
char msg2[5];

void loop()
{
 
  */
  //Read for new byte on serial hardware,
  //and write them on NewSoftSerial.
  serialhwread();
  //Read for new byte on NewSoftSerial.
  serialswread();
};

void serialhwread(){
  i=0;
  if (Serial.available() > 0){           
    while (Serial.available() > 0) {
      inSerial[i]=(Serial.read());
      delay(10);
      i++;     
    }

   inSerial[i]='\0';

    // GET GPS COORDS
    if(!strcmp(inSerial,"gps")){
      #ifdef DEBUG
        Serial.println(F("GETTING GPS COORDS"));
        Serial.println(F("--------------------------------"));
        Serial.println(F("In progress, wait..."));
      #endif
      getGPSCoords();
     
    }
  }
}
void getGPSCoords(){
  #ifdef DEBUG
    stat=gps.getStat();
    if(stat==1){
      Serial.println("NOT FIXED");
    }else if(stat==0){
      Serial.println("GPS OFF");
    }else if(stat==2){
      Serial.println("2D FIXED");
    }else if(stat==3){
      Serial.println("3D FIXED");
    }
    delay(5000);
  #endif   
 
  //Get data from GPS
 
  gps.getPar(lon,lat,alt,time,vel);

Serial.println(lat);
Serial.println(long);
Serial.println(alt);
Serial.println(time);
Serial.println(vel);
 
  strcpy (coords,"lat=");
  strcat (coords,lat);
     
  strcat (coords,"&long=");
  strcat (coords,lon);
     
  strcat (coords,"&alt=");
  strcat (coords,alt);
     
  strcat (coords,"&vel=");
  strcat (coords,vel);
     
  strcat (coords,"&time=");
  strcat (coords,time);
     
  #ifdef DEBUG
    Serial.print(F("Coords : "));                               
    Serial.println(coords);
  #endif

}


Il ne suffit pas de tout savoir, la persévérance, c'est déjà presque tout!
You can not know everything, perseverance, it is almost everything!

PeterH

#6
Jul 13, 2013, 08:41 pm Last Edit: Jul 13, 2013, 08:43 pm by PeterH Reason: 1
What do you think this does?

Code: [Select]

Serial.println(long);


What do the variables lat, lon, alt,  vel, time contain?

pierrot10

#7
Jul 13, 2013, 08:53 pm Last Edit: Jul 13, 2013, 09:37 pm by pierrot10 Reason: 1
Hello Peter,

Thank for replying

lat contain latitude, lon contains longitude, alt contains altitude, time contains time and vel contains speed.

All of them are char.

My workaround is to concatenate it into one variable , to be used in a function.

I previously worked around that issue and I solved my problem with strcpy and strcat, but I really do not know why it does not work now. May I miss a library?

So my need is to concatenate the lon, lat, alt, time into coords or wathever.

In PHP we can do
Code: [Select]
$allvar = $var1.','.$var2.','.$var2;

Can't we do something easy? :o)

Thank a lot
Il ne suffit pas de tout savoir, la persévérance, c'est déjà presque tout!
You can not know everything, perseverance, it is almost everything!

pierrot10

#8
Jul 13, 2013, 09:54 pm Last Edit: Jul 13, 2013, 09:58 pm by pierrot10 Reason: 1
I discover an intersting think,

I tryed step by step by commenting all strcat command. Then I uncomment he two first strcat for lat, the the next for lon and it was working until I uncomment the strcat for the variable cat:

Be aware, I change the variable
char coords[100]; to char coord[]="";
and I added a define COORDSSIZ 15.

Here is a exemple/part of my above code.
Code: [Select]

char coord[]="";$
define COORDSSIZ 15; //in order to make sure to not have more than 15 caracter.

void getGPSCoords(){
  #ifdef DEBUG
    stat=gps.getStat();
    if(stat==1){
      Serial.println("NOT FIXED");
    }else if(stat==0){
      Serial.println("GPS OFF");
    }else if(stat==2){
      Serial.println("2D FIXED");
    }else if(stat==3){
      Serial.println("3D FIXED");
    }
    delay(5000);
  #endif   
 
  //Get data from GPS
 
  gps.getPar(lon,lat,alt,time,vel);
 

  strcat (coords,"lat=");
  strncat (coords,lat,COORDSSIZ); //In my case lat has 11 caracter but can have  15 in max
  Serial.print(lat);
  Serial.println(",");
  strcat (coords,"&long=");
  strncat (coords,lon,COORDSSIZ); //In my case lon has 10 caracter but can have  15 in max
  Serial.print(lon);
  Serial.println(",");

  strcat (coords,"&alt=");
 
  // Until now coords has 35 caracters.
// IT CRASH/RESET SINCE I UNCOMMENT THNE BELOW LIGN

  strncat (coords,alt,COORDSSIZ); //In my case alt has 10 caracter but can have 15 in max
 
  Serial.print(alt);
  Serial.println(",");   
  /*
  strcat (coords,"&vel=");
  strncat (coords,vel,COORDSSIZ);
   
  strcat (coords,"&time=");
  strncat (coords,time,20);
  */
  #ifdef DEBUG
    Serial.print(F("Coords : "));                               
    Serial.println(coords);
  #endif

}

Does coords become out of "space"?
Il ne suffit pas de tout savoir, la persévérance, c'est déjà presque tout!
You can not know everything, perseverance, it is almost everything!

PeterH


lat contain latitude, lon contains longitude, alt contains altitude, time contains time and vel contains speed.

All of them are char.

Yeah, that's what you intend them to contain, but what specific values do they actually contain when the problem occurs?

pierrot10

Ha sorry!

the value are something like this (number with .)

for exemple:
lon: 12.344566
lat: 3457.3456
alt: 34.6788

etc
Il ne suffit pas de tout savoir, la persévérance, c'est déjà presque tout!
You can not know everything, perseverance, it is almost everything!

pierrot10

here are the exact value :

lon: 712.585044
lat: 4705.587531
alti: 505.911530
time: 20130713210510.000
vel: 0.000000


Coords : long=712.585044&lat=4705.587531&alti=
Il ne suffit pas de tout savoir, la persévérance, c'est déjà presque tout!
You can not know everything, perseverance, it is almost everything!

AWOL

#12
Jul 13, 2013, 11:07 pm Last Edit: Jul 13, 2013, 11:09 pm by AWOL Reason: 1
From your earlier post
Code: [Select]
har coords[110];
char lon[15];
char lat[15];
char alt[15];
char time[20];
char vel[15];
char msg1[5];
char msg2[5];

void loop()
{
 
 */
I don't know what "har" is, and I don't like that tail-end of a comment.

Code: [Select]
void serialhwread(){
  i=0;

Here, I'm not sure I see an "i" in scope.

pierrot10

oups sorry,

har is char. the c was not copied but it is correct in my script

Same for i.

It's well declared on the top of my script
Code: [Select]
int i=0;
Il ne suffit pas de tout savoir, la persévérance, c'est déjà presque tout!
You can not know everything, perseverance, it is almost everything!

pierrot10

#14
Jul 13, 2013, 11:37 pm Last Edit: Jul 13, 2013, 11:40 pm by pierrot10 Reason: 1
Thank for all for your help,
but I do not undertand why it's so complicate to concatenate char variable. (I am a beginner as well :o)).

In resume , I have :
(x=a value that I may ignore..., may be it's the reason of my issue)
Code: [Select]
char coords[x];
char lat[15];
char lon[15]
char alt[15];
char vel[15];
char time[20];


my function
void getGPSCoords()
collect the coords and provide the value to teh corresponding char variable.
The values are numbers with dots, for exemple:

lon: 712.585044
lat: 4705.587531
alti: 505.911530
time: 20130713210510.000
vel: 0.000000

I wish to concatenate lat,lon,alti,vel and time into coords and adding the string (or har) between some of them
lon=, &lat=, &alti, &vel=, &time=

As we can do in PHP:

$coords="lon=".$lon."&lat=".$lat."&alti=".$alt."&vel=".$vel."&time=".$time;

:)




Il ne suffit pas de tout savoir, la persévérance, c'est déjà presque tout!
You can not know everything, perseverance, it is almost everything!

Go Up