user72368: I'm trying to build a GUI that sends the data from processing to arduino via serial. With the help of a couple of guys on here, I have come up with a basic framework that sends a variable ID, variable value and termination character (\n). So far so good!
However, the [arduino reference][1] advises to only use char for storing characters. How would I send a byte or int over serial, while still being able to use a termination character to ensure everything is working as expected?
edit:
Using the arduino code from Edgar's answer below and the following processing code, I get the "could not parse value" error.
import processing.serial.*;

SerialConnection serialConnection;

boolean data_processed = false;
char[] var_array = new char[31];
char var_id;
char var_val;
char space = ' ';
char termchar = '\n';

void setup() {
  serialConnection = new SerialConnection(this, 9600);
}

void draw() {
  serialConnection.startSerialCommunication();

  if (serialConnection.isReady) {
    var_id = 'a';
    var_val = 127;
    serialConnection.serialPort.write(var_id);
    serialConnection.serialPort.write(space);
    serialConnection.serialPort.write(var_val);
    serialConnection.serialPort.write(termchar);

    delay(50);
    while (serialConnection.serialPort.available() > 0) {
      for (int i = 0; i < 30; i++) {
        char inchar = serialConnection.serialPort.readChar();
        var_array[i] = inchar;
        print(var_array[i]);
      }
    }
  }
}


user7508: If you want to send numeric values to your Arduino, the simplest option
is to send them formatted as ASCII text. Thus you end up simply sending
characters.
Let's assume for example a simple text-based protocol of the form
foo 42
bar 5678
baz -12

Each message is made of a variable name followed by its numeric value.
The name and the value a separated by a space. Each message is
terminated by a line feed ('\n').
In order to parse a message, you first have to split it at the space
character. The strsep() function comes handy for this. Then,
you have to parse the second part of the message (the value) as an
integer. I would use strtol() for this job. Putting all
together:
void process(char *message) {
    char *name = strsep(&message, " ");  // split at the space
    if (!message) {
        Serial.println("Error: no value given");
        return;
    }
    char *endp;  // end of the numeric value
    long value = strtol(message, &endp, 0);
    if (endp == message) {
        Serial.println("Error: could not parse value");
        return;
    }
    /* Successfully parsed. */
    Serial.print(name);
    Serial.print(" received value ");
    Serial.println(value);
}

Note there are some sanity checks in order to make sure the message does
match the expected format. Obviously, you have to replace the last three
lines by something more meaningful to your application.
Example sketch using this:
void setup() {
    Serial.begin(9600);
}

void loop() {
    static char buffer[40];
    static size_t buffer_pos = 0;
    while (Serial.available()) {
        char c = Serial.read();
        if (c == '\n') {  // end of message
            buffer[buffer_pos] = '\0';  // terminate the string
            process(buffer);
            buffer_pos = 0;  // reset for next message
        } else if (buffer_pos < sizeof buffer - 1) {
            buffer[buffer_pos++] = c;  // buffer the character
        }
    }
}


72368: <p>I'm trying to build a GUI that sends the data from processing to arduino via serial. With the help of a couple of guys on here, I have come up with a basic framework that sends a variable ID, variable value and termination character (\n). So far so good!</p>
<p>However, the [arduino reference][1] advises to only use <code>char</code> for storing characters. How would I send a <code>byte</code> or <code>int</code> over serial, while still being able to use a termination character to ensure everything is working as expected?</p>
<p><strong>edit:</strong>
Using the arduino code from Edgar's answer below and the following processing code, I get the &quot;could not parse value&quot; error.</p>
<pre><code>import processing.serial.*;

SerialConnection serialConnection;

boolean data_processed = false;
char[] var_array = new char[31];
char var_id;
char var_val;
char space = ' ';
char termchar = '\n';

void setup() {
  serialConnection = new SerialConnection(this, 9600);
}

void draw() {
  serialConnection.startSerialCommunication();

  if (serialConnection.isReady) {
    var_id = 'a';
    var_val = 127;
    serialConnection.serialPort.write(var_id);
    serialConnection.serialPort.write(space);
    serialConnection.serialPort.write(var_val);
    serialConnection.serialPort.write(termchar);

    delay(50);
    while (serialConnection.serialPort.available() &gt; 0) {
      for (int i = 0; i &lt; 30; i++) {
        char inchar = serialConnection.serialPort.readChar();
        var_array[i] = inchar;
        print(var_array[i]);
      }
    }
  }
}
</code></pre>
 7508: <p>If you want to send numeric values to your Arduino, the simplest option
is to send them formatted as ASCII text. Thus you end up simply sending
characters.</p>
<p>Let's assume for example a simple text-based protocol of the form</p>
<pre><code>foo 42
bar 5678
baz -12
</code></pre>
<p>Each message is made of a variable name followed by its numeric value.
The name and the value a separated by a space. Each message is
terminated by a line feed (<code>'\n'</code>).</p>
<p>In order to parse a message, you first have to split it at the space
character. The <a href="https://man7.org/linux/man-pages/man3/strsep.3.html" rel="nofollow noreferrer"><code>strsep()</code></a> function comes handy for this. Then,
you have to parse the second part of the message (the value) as an
integer. I would use <a href="https://man7.org/linux/man-pages/man3/strtol.3.html" rel="nofollow noreferrer"><code>strtol()</code></a> for this job. Putting all
together:</p>
<pre class="lang-cpp prettyprint-override"><code>void process(char *message) {
    char *name = strsep(&amp;message, &quot; &quot;);  // split at the space
    if (!message) {
        Serial.println(&quot;Error: no value given&quot;);
        return;
    }
    char *endp;  // end of the numeric value
    long value = strtol(message, &amp;endp, 0);
    if (endp == message) {
        Serial.println(&quot;Error: could not parse value&quot;);
        return;
    }
    /* Successfully parsed. */
    Serial.print(name);
    Serial.print(&quot; received value &quot;);
    Serial.println(value);
}
</code></pre>
<p>Note there are some sanity checks in order to make sure the message does
match the expected format. Obviously, you have to replace the last three
lines by something more meaningful to your application.</p>
<p>Example sketch using this:</p>
<pre class="lang-cpp prettyprint-override"><code>void setup() {
    Serial.begin(9600);
}

void loop() {
    static char buffer[40];
    static size_t buffer_pos = 0;
    while (Serial.available()) {
        char c = Serial.read();
        if (c == '\n') {  // end of message
            buffer[buffer_pos] = '\0';  // terminate the string
            process(buffer);
            buffer_pos = 0;  // reset for next message
        } else if (buffer_pos &lt; sizeof buffer - 1) {
            buffer[buffer_pos++] = c;  // buffer the character
        }
    }
}
</code></pre>
