Go Down

Topic: Concatenate two char arrays (for udp client) (Read 3 times) previous topic - next topic

marcus barnet

I'm using Arduino as a UDP client to send information about values read from sensors.

I'd like to send packets like:

"string1=value1"

where value1 is an integer read from Arduino analog pins.
The problem is that udp.write() only takes as parameter char arrays, so i can't sum a string to an integer and then pass the new string to udp.write().

I have to concatenate a char array to an integer value and then pass the array to the udp.write() function.

At the moment, i'm using
Code: [Select]
dtostrf(FrontSonar_Reading,1,2,temp);
in order to save the integer value in the "temp" array char, but i do not know how to concatenate this result to the array char.

Actually, I have something like:

Code: [Select]
char string1[] = "string1=";

void loop(){
  //...
  dtostrf(FrontSonar_Reading,1,2,temp);
  Udp.beginPacket(remoteIp, remotePort);

   Udp.write(string1);
   Udp.write(temp);

   Udp.endPacket();
}


it works, but it sends two packets; i'd like to send just one packet with the string and the integer at the same time.

Is it possible?

Jantje

I'm not sure this is the best way to do it when you use UDP but sprintf works in all cases.
Below is a sketch that concatenates a string "=" a value.
Code: [Select]
void setup() {
  Serial.begin(115200);
}

void loop() {

    char myConcatenation[80];
    char myCharArray[16]="A variable name";
    int myInt=5;
    sprintf(myConcatenation,"%s = %i",myCharArray,myInt);
    Serial.println(myConcatenation);
    delay(100);
}

look at this link for more info on sprintf http://www.cplusplus.com/reference/clibrary/cstdio/sprintf/
I hope this helps
Best regards
Jantje
Do not PM me a question unless you are prepared to pay for consultancy.
Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -

marcus barnet

Thanks a lot for your suggestion!

Is sprintf() safe? Can it have memory lacks or problems?

I'd like to build a roboust application and i'm trying to avoid memory problems.

What do you think about this?

Thanks again.

marcus barnet

Can this function be more safer than sprintf()?

Code: [Select]
char * BufferInsertStringValue(char * s,int startingPosition, char * command) {
  int i = 0;
  int stringLength=strlen(s)-1;
  for ( i = 0;i <= stringLength;i++) {
    command[startingPosition + i - 1] = s[i];
     }
  command[startingPosition + i] = '\0';
  return command;
}


where s is the temp array char and comando is the original text string which i want to concatenate with the integer value.
I call this function with:
Code: [Select]

char * vett;
vett = BufferInsertStringValue(temp, 14, sonar_center);


Do you think it's safer than sprintf function?

Jantje

The only risk you have with sprintf is that you will outrun your target buffer space.
As you are using a char buffer containing the variable name and you are adding a int you can 100% guarantee your buffer is big enough.
Unless you go for runtime memory allocation (like String class does) you will always have this problem.
Best regards
Jantje
Do not PM me a question unless you are prepared to pay for consultancy.
Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -

Lavan

Hello Friends,  I am exploring the below sketch to concatenate  arrays..  If I want to add "%" symbol to the end of the integer value to myConcatenation[80] array, how do I do it?  basically, when I print myConcatenation, I would like to see the output as "A variable name = 5 %.

Any guidance is appreciated.

Code: [Select]
void setup() {
  Serial.begin(115200);
}

void loop() {

    char myConcatenation[80];
    char myCharArray[16]="A variable name";
    int myInt=5;
    sprintf(myConcatenation,"%s = %i",myCharArray,myInt);
    Serial.println(myConcatenation);
    delay(100);
}

Lavan

Ok.  Figured it out myself with below code.  Is there any other better way to achieve it?

Code: [Select]
void setup() {
  Serial.begin(9600);
}

void loop() {

    char myConcatenation[80];
    char myCharArray[]="A variable name";
    int myInt=5;
    char myStr[] = "%";
    sprintf(myConcatenation,"%s = %i %s",myCharArray,myInt,myStr);
    Serial.println(myConcatenation);
    delay(100);
}

Jantje


Is there any other better way to achieve it?

Reading the sprintf documentation?  ]:D
You probably need to add a \ before the % but I'm sure google can point you to documentation of the printf family that tells you how to do it.
Do not PM me a question unless you are prepared to pay for consultancy.
Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -

Lavan

Thanks for your direction.  Got the answer from the printf statement guide.  Below one does the trick.

Code: [Select]
sprintf(myConcatenation,"%s = %i%%",myCharArray,myInt)

Jantje

great :-)
So % is the escape character and not \. Makes sense.
Best regards
Jantje
Do not PM me a question unless you are prepared to pay for consultancy.
Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -

Go Up