Constructing my own serial command syntax to control arduino
Expanding on my comment... I wrote a simple command line interpreter
that you can use as a base, or an inspiration, for your own interpreter.
In order to not make a link-only answer, I will put here an annotated
and simplified version.

The incoming characters are handled by loop(). They are stored into a
static buffer until a carriage return (CR = '\r') is received. When
the CR is received:


the buffer is NUL-terminated in order to make it a proper C string
it is handed to the exec() function for interpretation
the variable storing the command length is reset to zero for the next
command


Here is the code:

while (Serial.available()) {
    static char buffer[BUF_LENGTH];
    static int length = 0;  // length of line received so far
    int data = Serial.read();
    if (data == '\r') {
        buffer[length] = '\0';     // properly terminate the string
        if (length) exec(buffer);  // give to interpreter
        length = 0;                // reset for next command
    } else if (length &lt; BUF_LENGTH - 1) {
        buffer[length++] = data;   // buffer the incoming byte
    }
}


The exec() interpreter function uses strsep() to split the
command line into a command verb and subsequent parameters. It then
compares the command verb with all supported commands in a long if...
else if sequence, like this:

static void exec(char *cmdline)
{
    char *command = strsep(&amp;cmdline, " ");

    if (strcmp(command, "mode") == 0) {
        int pin = atoi(strsep(&amp;cmdline, " "));
        int mode = atoi(cmdline);
        pinMode(pin, mode);
    } else if (strcmp(command, "read") == 0) {
        int pin = atoi(cmdline);
        Serial.println(digitalRead(pin));
    } else if (strcmp(command, "write") == 0) {
        int pin = atoi(strsep(&amp;cmdline, " "));
        int value = atoi(cmdline);
        digitalWrite(pin, value);
    } else {
        Serial.print(F("Error: Unknown command: "));
        Serial.println(command);
    }
}


This one supports the three commands:


mode &lt;pin&gt; &lt;mode&gt;, meaning pinMode()
read &lt;pin&gt;, meaning digitalRead()
write &lt;pin&gt; &lt;value&gt;, meaning digitalWrite()


The full version is here: Simple Arduino command line
interpreter. Compared to the version in this answer, the
main differences are:


it echoes the characters it reads, and does so in a way which is
friendly to terminal emulators:


it echoes CR as CRLF
it handles BS (backspace) and echoes it as BS + space + BS

it uses strcmp_P() instead of strcmp(), with the PSTR() macro,
in order to keep the constant strings in Flash only
it supports more commands, including "help".