String manipulation

Hey, I would like to figure out how to build a transmit string before transmitting it. My goal is to build the string... compute a checksum for the string. Then transmit the string. However, right now the string just builds and sends. Where is the intermediate stored?

Thanks. Below is a copy of my code.

Serial.print( "$GPACC," ); Serial.print( x_val, DEC ); Serial.print( "," ); Serial.print( y_val, DEC ); Serial.print( "," ); Serial.print( z_val, DEC ); Serial.print( "," ); Serial.print( timer, DEC ); Serial.println( "*" );

I would prefer to build this all in an string first then transmit the string. Those are integer values being transmitted.

You could use sprintf although it consumes lots of program memory:

#include <stdio.h>

void buildString(char * header, int x, int y, int z, int timer){
char buffer[32];  // ensure this is big enough
   sprintf(buffer,"%s,%d,%d,%d,%d*", header,x,y,z,timer); 
   // buffer now contains the string, you can do your checksum and send here or have buffer visible to the calling function 
}

That's awesome but I cannot seem to find the stdio.h in the Arduino SDK. Where do I get it?

Thanks!

its one of the files included with the compiler. I think its in: hardware\tools\avr\avr\include

but the compiler should bring it in automatically if your sketch has:
#include <stdio.h>

My first line of code is:

#include <stdio.h> and the compiler says it cannot find the file. I checked and stdio.h is included under “hardware\tools\avr\avr\include” so I’m a little confused…

My SDK is 0011 Alpha

Is there someway I have to setup the path to all the libraries. It seems to find the wire library w/o any issue.

Installing the arduino environment should set the paths up for you. Post your OS, Arduino version and the error message and perhaps someone can help.

I am in a similar situation here I want to build a string, but I get an error:

In function 'void loop()': error: too few arguments to function 'void buildString(char*, int, int, char*)'

void buildString(char * header, int targetPosition, int panSpeed,char * terminator){
char buffer[32];  // ensure this is big enough
   sprintf(buffer,"%s %d %d %s", header, targetPosition, panSpeed, terminator);
   // buffer now contains the string, you can do your checksum and send here or have buffer visible to the calling function
}

I don't know C so I am just a cut and paste hacker. My string should end up: header targetPosition panSpeed terminator = "G1 25000 4000 /r"

You need to post the code from “loop” where you call “buildString”, but whatever you do with buildString won’t work, because “buffer” goes out of scope when the function exits.

OK I'm at work now so I don't have the code here. I'll add a statement like: cmd = buffer; in the function loop.

As I remember the loop reads the input from the serial port 0 then calls buildString() and prints it to serial port 1 for the servo controller.

I’ll add a statement like:
cmd = buffer;

You can’t do that with the function you showed; in “loop”, “buffer” is not in scope.

Thanks I'll look at other methods, I know I can build the string character by character instead. This just looked like a clean little piece of code.

The code is fine. It's just that you can't use a local static buffer. You need to use either a global buffer or dynamically allocate a buffer (using malloc).

It's just that you can't use a local static buffer

Slightly OT here, but "buffer" isn't static, it's automatic.