Serial input question

Hi,
I have found some code here in the forums which looked useful to me, but i seem unable to use it at all ><

What i want is simply to get a string from the serial input and send it into a function (which then sends it via Virtual Wire to a 2nd Arduino) but the problem is that getting the string is the hard part in the first place.

void get_keyboard_commands() {
  char input[0];
  int incount = 0;
  if ( Serial.available()) {
    
    // read in a string byte by byte
    while (Serial.available()) {
       input[incount++] = Serial.read();      
    }
    input[incount] = '\0';  // puts an end on the string

 }
 if(input[0] != '\0'){
    Serial.println(input);         
    sendCommand(input);
    input[0]= '\0';
 }
  
}

is what i am using now…
It sort of works, i get my string and is being forwarded to Arduino Nr2 but in a way i dont want it.
First of all it splits stuff up like:
“Test” becomes “T\nest” (judging from the output.
It always sends the first character followed by the rest and secondly it repeats the last send message as long as there is not a new one coming from the Serial input.

Can someone show me what i have to do to do that kind of stuff the right way? Sending just one message at a time which is the complete String from the Input?

Your statement, char input[0]; reserves zero bytes for an input buffer. Not even enough room for the ‘\0’ byte terminator. The first time you write to any index like input[n], even input[0], you’re damaging memory outside the buffer.

Even if it was more than zero bytes, it’s gone when you leave the function, so you never build up more than what’s immediately available-but-not-yet-read in the serial input queue.

#define LONGEST_COMMAND 10

char input[LONGEST_COMMAND+1];
int incount = 0;

void get_keyboard_commands() {
  char ch = '\0';
  // read in a string byte by byte
  while (incount < sizeof(input)-1 && Serial.available()) {
    ch = Serial.read();
    input[incount++] = ch;
    input[incount] = '\0';  // always terminate the string
    if (ch == '\n') { // commands end with newline
      Serial.print(input);
      sendCommand(input); // use the command
      incount = 0;
      input[incount] = '\0'; // empty the string for next command
    }
  }
}

Just a thought:

void get_keyboard_commands() {
  char input[10] = {0}; //change to appropriate value
  int incount = 0;
  if ( Serial.available()) {
    //delay(100); //allow the whole string to get buffered
    // read in a string byte by byte
    while (Serial.available()) {
       input[incount++] = Serial.read();
    }
    input[incount] = '\0';  // puts an end on the string

 }
 if(input[0] != '\0'){
    Serial.println(input);
    sendCommand(input);
    input[0]= '\0';
 }

}

[edit]halley has posted a good solution above :)[/edit]

AlphaBeta, the adding of a delay(100) to wait for more of the command to arrive is probably not going to help here. No matter what you choose for a delay value, it might be too short for a whole command to arrive, or so long that the Serial internal buffer will overflow.

Sorry, I added my version of a command-routine in my post above while you were responding.

I know that the delay is not a good practice. I've experienced that while creating a proof of concept sketch, the delay is a quick hack that have helped me sometimes. When I've not bothered to code a failsafe serial recieve logic.

It's commented out because it enabled the OP to easily try it. In the original sketch, he does not check to see that more then one char has been buffered. The logical explanation to why 'it always sends the first character followed by the rest' could be; the first character enables avalable() but read()s before the rest has been recieved, thus disableing avalable() and causing a \n'

If there are six characters available-but-not-yet-read, then Serial.available() returns 6 (a true value). If you call Serial.read() once, then Serial.available() will return 5, not zero (the false value).

edit Hmm.. it seems it doesnt work though. I dont get even a local serial.print of what i send to the console 0o

My routine above collects input until a '\n' newline, which the Arduino IDE doesn't send (for reasons unknown to me). Change that tiny bit to a ' ' (space) and try it with println() instead of print(). You should get a line every time you send a space.

damn, i should have been thinking of that shaking head but i assumed the IDE would send \n.. well... thanks again.