Skip to main content
Attempt to implement answer, cannot resolve the issue.
Source Link

However, the arduino reference[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?

Currentedit: Using the arduino code from Edgar's answer below. Should I be separating a byte/integer into several characters and then concatenating? Apologies if I'm missing something obviousthe following processing code, I get the "could not parse value" error.

Arduino:

char c = ' ';
int length = 2;
char buffer [3];
char termChar = '\n';
byte index = 0;
boolean haveNewData = false;

/**************************************************************************/
void setup(){
  Serial.begin(9600);
}

/**************************************************************************/
void loop(){
  readSerial();
  if (haveNewData) {
    processNewData();
  }
}

/**************************************************************************/
void readSerial(){
  if (Serial.available())
  {
    c = Serial.read();
    if (c != termChar)
    {
      if (index <= length)
      {
        buffer[index] = c;
        index += 1;
      }
    }
    else
    {
      buffer[index] = ' ';
      index = 0;
      haveNewData = true;
    }
  }
}

/**************************************************************************/
void processNewData(){
  Serial.write(buffer[1]);
  // Serial.print(var);
  haveNewData = false;
}

Processing:

import processing.serial.*;

SerialConnection serialConnection;

boolean data_processed = false;
// byte[]char[] var_array = new byte[2];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 = 1;'a';
    var_val = 127;
    serialConnection.serialPort.write(var_id);
    serialConnection.serialPort.write(var_valspace);
    serialConnection.serialPort.write(termcharvar_val);
  }  serialConnection.serialPort.write(termchar);

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

However, the arduino reference 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?

Current code below. Should I be separating a byte/integer into several characters and then concatenating? Apologies if I'm missing something obvious.

Arduino:

char c = ' ';
int length = 2;
char buffer [3];
char termChar = '\n';
byte index = 0;
boolean haveNewData = false;

/**************************************************************************/
void setup(){
  Serial.begin(9600);
}

/**************************************************************************/
void loop(){
  readSerial();
  if (haveNewData) {
    processNewData();
  }
}

/**************************************************************************/
void readSerial(){
  if (Serial.available())
  {
    c = Serial.read();
    if (c != termChar)
    {
      if (index <= length)
      {
        buffer[index] = c;
        index += 1;
      }
    }
    else
    {
      buffer[index] = ' ';
      index = 0;
      haveNewData = true;
    }
  }
}

/**************************************************************************/
void processNewData(){
  Serial.write(buffer[1]);
  // Serial.print(var);
  haveNewData = false;
}

Processing:

import processing.serial.*;

SerialConnection serialConnection;

boolean data_processed = false;
// byte[] var_array = new byte[2];
char var_id;
char var_val;
char termchar = '\n';

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

void draw() {
  serialConnection.startSerialCommunication();

  if (serialConnection.isReady) {
    var_id = 1;
    var_val = 127;
    serialConnection.serialPort.write(var_id);
    serialConnection.serialPort.write(var_val);
    serialConnection.serialPort.write(termchar);
  }

  delay(50);
  while (serialConnection.serialPort.available() > 0) {
    int inByte = serialConnection.serialPort.read();
    println(inByte);
  }
}

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]);
      }
    }
  }
}
Source Link

How to send non char variables over serial

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 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?

Current code below. Should I be separating a byte/integer into several characters and then concatenating? Apologies if I'm missing something obvious.

Arduino:

char c = ' ';
int length = 2;
char buffer [3];
char termChar = '\n';
byte index = 0;
boolean haveNewData = false;

/**************************************************************************/
void setup(){
  Serial.begin(9600);
}

/**************************************************************************/
void loop(){
  readSerial();
  if (haveNewData) {
    processNewData();
  }
}

/**************************************************************************/
void readSerial(){
  if (Serial.available())
  {
    c = Serial.read();
    if (c != termChar)
    {
      if (index <= length)
      {
        buffer[index] = c;
        index += 1;
      }
    }
    else
    {
      buffer[index] = ' ';
      index = 0;
      haveNewData = true;
    }
  }
}

/**************************************************************************/
void processNewData(){
  Serial.write(buffer[1]);
  // Serial.print(var);
  haveNewData = false;
}

Processing:

import processing.serial.*;

SerialConnection serialConnection;

boolean data_processed = false;
// byte[] var_array = new byte[2];
char var_id;
char var_val;
char termchar = '\n';

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

void draw() {
  serialConnection.startSerialCommunication();

  if (serialConnection.isReady) {
    var_id = 1;
    var_val = 127;
    serialConnection.serialPort.write(var_id);
    serialConnection.serialPort.write(var_val);
    serialConnection.serialPort.write(termchar);
  }

  delay(50);
  while (serialConnection.serialPort.available() > 0) {
    int inByte = serialConnection.serialPort.read();
    println(inByte);
  }
}