Sending a string over Serial

Is it possible to send a string over Serial to the Arduino?

I have the Arduino hooked up to a Fon router's serial connection, but can't send commands without hardcoding them into the sketch. I'd like to be able to just send commands through the Arduino IDE or maybe some other term program on my mac.

I'm currently thinking my alternative is to build a menu which accepts a single character and send the corresponding string to the router. That's not real appealing though due to lack of flexibility.

Is it possible to send a string over Serial to the Arduino?

Of course, just bear in mind the Arduino's limited memory.

Could you provide some code? All I've seen and been able to do has been dealing with individual characters at a time.

All I've seen and been able to do has been dealing with individual characters at a time

That's all a string is - a sequence of individual characters.

Give this a try:
[UNTESTED CODE]

const byte MAXIMUM_INPUT_LENGTH = 10;
char input[MAXIMUM_INPUT_LENGTH+1] = {’\0’};
byte currentIndex = 0;

void setup() {
Serial.begin(9600);
}

void loop() {
if (Serial.available()){
if (currentIndex>=MAXIMUM_INPUT_LENGTH){
for (byte i=0; i<=MAXIMUM_INPUT_LENGTH; i++){
input = ‘\0’; //clear input

  • }*

  • currentIndex = 0; //start over*

  • }*

  • input[currentIndex++] = Serial.read(); //append to input*

  • Serial.print("input is: ");*

  • Serial.println(input);*

  • }*
    } [/quote]

Thanks AlphaBeta.

So strictly speaking it doesn't receive strings. I can't just run Serial.read(var, STRING) or something like that, I need 20+ lines of code to receives many characters and put them together into a string.

I was hoping for something shorter, but I guess this falls under the saving space argument?

I need 20+ lines

Probably less than half that.

AWOL, I welcome your example. That would add value to the conversation. AlphaBeta's example was about 20 lines and I consider it valuable because it both answered my question and showed me how to get around my confusion.

flickerfly - my example was AlphaBeta's.
The input of a string takes about 10 lines - the rest is declarations, initialisation and debug.
You could probably lose the "for" loop too - simply put a '\0' in the first character to terminate the string.
Shunt it off into a subroutine, and forget about it.