Controlling Arduino By Text Input

Hello arduiners…

I need some help I’m kind of stuck
I can find code on parts of this but am still having trouble putting everything together

What I’m trying to do is control the arduino from PC
What I want to do is send a command down the serial link which is then received by the arduino and a reply is sent back up the serial to the PC

in the code below I want to send “help?”…

I have no problem doing all of this…(!!! 99% of the time !!!)
My problem is that when I send a command to the arduino I can’t be sure that the board has consumed the entire command so I need to send an end of line charatcer and keep reading the serial until I get that character. This much I know…

sometimes at the arduino I may just get “he” as the “lp?” hasn’t arrived yet
so the command is missed

I use the following code below to do the comparison see also the function afterwards…

So My question is…
How can I implement an end of line character, in the comp function, in this code?

void loop()
{
if (Comp(“help?”)==0)
{
Serial.write(" Diagnostic Commands\n");
Serial.write(" 1. blade1U - Puts Blade 1 Up\n");
Serial.write(" 2. blade1D - Puts Blade 1 Down\n");
Serial.write(" 3. blade2U - Not Implemented Yet\n");
Serial.write("\n");
Serial.write(“Enter Command…\n”);
}
delay(5);

}

char Comp(char* This)
{
char inData[20]; // Allocate some space for the string
char inChar=-1; // Where to store the character read
byte index = 0; // Index into array; where to store the character

while (Serial.available() > 0) // Don’t read unless
// there you know there is data
{
if(index < 19) // One less than the size of the array
{
inChar = Serial.read(); // Read a character
inData[index] = inChar; // Store it
index++; // Increment where to write next
inData[index] = ‘\0’; // Null terminate the string
}
}

if (strcmp(inData,This) == 0) {
for (int i=0;i<19;i++) {
inData*=0;*

  • }*
  • index=0;*
  • return(0);*
  • }*
  • else {*
  • return(1);*
  • }*
    }

You could try something like below:

//zoomkat 3-5-12 simple delimited ',' string parce 
//from serial port input (via serial monitor)
//and print result out serial port
// CR/LF could also be a delimiter

String readString;

void setup() {
  Serial.begin(9600);
  Serial.println("serial delimit test 1.0"); // so I can keep track of what is loaded
}

void loop() {

  //expect a string like wer,qwe rty,123 456,hyre kjhg,
  //or like hello world,who are you?,bye!,

  if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == ',') {
      if (readString.length() >1) {
        Serial.println(readString); //prints string to serial port out
        //do stuff with the captured readString 
        readString=""; //clears variable for new input
      }
    }  
    else {     
      readString += c; //makes the string readString
    }
  }
}

There is nothing in your code that stops Comp() from reading only some serial data (all that has currently arrived), assuming that that was all the data, doing the compare on partial data, and returning.

My problem is that when I send a command to the arduino I can’t be sure that the board has consumed the entire command so I need to send an end of line charatcer and keep reading the serial until I get that character. This much I know…

Then it should be obvious that Comp() needs to wait for this end-of-packet marker, and NOT perform the comparison until the end-of-packet marker arrives.

So My question is…
How can I implement an end of line character, in the comp function, in this code?

Add a while loop, to Comp(), that keeps looping until the end of packet marker has not been seen.

boolean ended = false;
while(!ended)
{
    while (Serial.available() > 0) // Don't read unless
                                   // there you know there is data
    {
        if(index < 19) // One less than the size of the array
        {
          inChar = Serial.read(); // Read a character
          inData[index] = inChar; // Store it
          index++; // Increment where to write next
          inData[index] = '\0'; // Null terminate the string
          if(aChar == EOP)
          {
             ended = true;
             break;
          }
        }
    }
}

Use a #define statement to assign a value to EOP (whatever your end of packet marker is).

OP, there's a really good reason we ask people to post their code inside code tags. Can you see what that reason is?