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