Read and parse input string

HI,

I'm a very (very, VERY) newbie to C.

I have copied a program (read a string - output the string) and this works fine. But That is not what I need.

I want to read the a command (including its parameters), parse it and execute that command with its parameters.

Ihave the follwing commands: (all motors are stepper-motors)

IMA (Initialise ALL motors)

IM1 (Intialise motor 1 )
IM2 (Intialise motor 2 )
IM3 (Intialise motor 3 )
IM4 (Intialise motor 4 )

IMx command "resets" motor n to its "middle / center" position.

RM1 (Read motor 1 position )
RM2 (Read motor 2 position )
RM3 (Read motor 3 position)
RM4 (Read motor 4 position)

(All stepper-motors are mechanically connected to a pot-meter again connected to a ANALOG-port).
Value will be 0 - 1023 )

SM1(value) (set motor 1 position according to value - compared to ANALOGport reading)
SM2(value) (set motor 2 position - - - - - )
SM3(value) (set motor 3 position - - - - - )
SM4(value) (set motor 4 position - - - - - )

But I do not know how to do this.

Kris

Do you have anything done until now?

String cmd;
String value;

void loop() {
  cmd="";
  if(Serial.available() > 0)
  {
      cmd = Serial.readStringUntil('\n');

      if(cmd.equals("IM1"))
        callYourIMFunction1();
      else if(cmd.equals("IM2"))
        callYourIMFunction2();
      else if(cmd.equals("IM3"))
        callYourIMFunction3();
      else if(cmd.equals("IM4"))
        callYourIMFunction4();
        
      else if(cmd.equals("SM1")){
        value="";
        while(value.length()==0){
          value = Serial.readStringUntil('\n');
        }
        if(value.length()>0)
	  callYourSMFunction1(value.toInt());
      }
  }
}

You can try like this. You have to sent commands with a newline character. For SM commands like SM4(100), you will have to send them like "SM4\n100\n"

Well, not much.

But I have studied the code that reads a char from the PC-keyb. saves it into a buffer - repeats until inData = CR (or LF) and then writes the line to the PC-console. Its part of the ARD- library.

(I'am sitting at my LABTOP now, I dont have the code her (on this pC) - sorry !)

BUt You gave me an idea, I could use an Index-pointer (char(1) could go to buffer[1] and so on ) and then read index and test for correct char... (including digits where appropriate )...

Kris

Have a look at the examples in serial input basics. There is also a parse example.

...R