Do I need a special library for strcpy and strcat

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.

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?

no it wrong, my script crash and restart

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.

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.

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.

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

#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
 
}

What do you think this does?

Serial.println(long);

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

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 $allvar = $var1.','.$var2.','.$var2;

Can't we do something easy? :o)

Thank a lot

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.

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”?

pierrot10: 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?

Ha sorry!

the value are something like this (number with .)

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

etc

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=

From your earlier post

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.

void serialhwread(){
  i=0;

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

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

int i=0;

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)

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;

:)

Are those 'examples' actual values that you encountered when the problem occurred, or just values that you think are typical or noticed when the problem did not occur?

Sorry to harp on about this, but the symptoms suggest you may have an unterminated string and/or array overflow so it is quite important to know the actual precise values that cause the problem.

In your code

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

what types are the parameters lon,lat,alt,time,vel? Char, float, char*, int or what?

nid69ita: Try using snprintf

That would have the same effect as the existing code, and suffer from the same problems is any of the buffers overflow or are not correctly terminated.

pierrot10: 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)

char coords[x];
char lat[15];
char lon[15]
char alt[15];
char vel[15];
char time[20];

The complicated and time-consuming thing here is getting you to post your code. Obviously that won't compile. What is "x"?

You are time-wasting here if you don't post your complete sketch.

Read this before posting a programming question

Thank again all for helping @PeterH Those value are the excat value, provided by gps.getPar(lon,lat,alt,time,vel); @acboother Those value (lon, lat, alt, time, vel) are char @Gammon x is an value that I am not sure, but it should be aroud 114 I wrote this:

(x=a value that I may ignore..., may be it's the reason of my issue)

I posted the major code here http://forum.arduino.cc/index.php?topic=177206.msg1315045#msg1315045

My code works fine excepted for my need to concatenate char variable, as I wrote before.

I also tried something like this Keep in mind, lon, lat, alt, etc have 15 caraceter maximum

  char fix[144] = {"long=" + String(lon) + "&lat=" + String(lat) + "&alti=" + String(alt) + "&vel=" + String(vel) + "&time=" + String(time) + "\0"};

But I got this error message, because I miss the step to convert String to char. Can we do it simply?

Can not covert 'StringSumHelper' to 'char' in initlization