Serial comm last mistery :)

Hi,
I am nearly finished with my command analyzer, but with the array size I still have one problem I hope it will be solved here.

This is the current code:

#define MAXCMDLEN  11

void loop()
{
 if( Serial.available() > 0 && Serial.read()=='@') 
 {  
   delay(10);
   char cmdbuf[MAXCMDLEN];
   char c;
   int i = 0;
   while( Serial.available() && c != '\n' && i<MAXCMDLEN ) 
   {
     c = Serial.read();
     cmdbuf[i++] = c;
   }
   if(c!='\n')  
     cmdbuf[i] = '\n';
   analyzeCommand(cmdbuf, i);
 }  
}

void analyzeCommand(char* cmd, int bufCounter)
{
   for(int j=0; j < bufCounter; j++)
     Serial.print(cmd[j]);  
}

it works, but when I type, say, @0099887766 and I expect 0099887766, I surprisingly get 009988776 without the last character. If I print bufCounter of my analyzeCommand(), it is always 9, even if I increase the size of MAXCMDLEN. It is probably another trivial mistake here, but to me it is a mystery how i of the loop() and bufCounter of the analyzer always are stopped at 9, even if I type a 10-char string!

Do you see the problem there pleas? :slight_smile:

The problem is with delay(10), but the problem with this problem is that, if I just delete it, then I don't get a command completely as things work faster than needed (even if I slow down to 57200 baud), but if I make it delay(12) it works!

Well, what is the right way of implementing this, so that all of my 10 char to be integrated once and in one variable, and then to print them?

The problem is with delay(10)

No, the problem is that you're not accumulating a whole line of text into the buffer and processing it.

Change the print() in analyzeCommand() to a println(), and you'll see what's happening: the runtime calls loop(), it finds one character available, it reads that character, then calls analyzeCommand() and discards cmdbuf.

You need to change the while loop so it doesn't break until you get the line termination.

Thanks!

You need to change the while loop so it doesn't break until you get the line termination.

Do you mean to test for '\n' right?

Another issue is the difference between an array of characters and a string. The Serial.print function is expecting a string, but you are passing it an array of characters.

The difference between an array of characters and a string is that a string is an array of characters that is NULL terminated.

You are not NULL terminating your array of characters.

PaulS,
well I am actually not passing an array of characters to the Serial.print()

for(int j=0; j < bufCounter; j++)
     Serial.print(cmd[j]);

as you see, I am passing "one char" each time, using "for" loop. I can use Serial.println() to print a '\n' at the end. Now do you see any issue with passing char array one by one?

I'm pretty sure that Ran's reply explains what your problem is.

I always print something before and after printing strings, just to make sure I am printing the whole string, and nothing but the string.

Serial.print("My string contains [");
// Print the string (all at once, or character by character)
Serial.print("]");

This way, if you send the Arduino 1234567890, and you see

My string contains [12]
My string contains [34567]
My string contains [890]

in the output window, the problem is pretty obvious.

I recommend that you do the same.

Thank you!