Would it be possible to send data as comma separated values without redundant text? For example, have Processing split string to positionArray[3] ; To reinforce what has already been stated there is a lot of redundancy in
char pcOutBuffer[] = "Motorboard:Present_Posistion 1 20093774343;Present_Posistion 2 20093774343;Present_Posistion 3 20093774343;"; //typical payload
and could be shortened with comma separated values and sent as a much shorter string, eg
value1,value2,value3 followed by a line feed(accomplished by println())
The following is example Processing code which will run on an Arduino Due R3:
import processing.serial.*;
Serial myPort; // The serial port
String inStr; // Input string from serial port
int lf = 10; // ASCII linefeed
String[] dataStr = new String[2];
int[] data = new int[2];
void setup() {
surface.setVisible(false); // Output is in console at bottom of editor
// List all the available serial ports:
printArray(Serial.list());
// Enter device number for your system
myPort = new Serial(this, Serial.list()[1], 115200);
myPort.bufferUntil(lf);
}
void draw() {
}
void serialEvent( Serial myPort) {
//'\n' (line feed) is delimiter indicating end of packet
inStr = myPort.readStringUntil('\n');
//make sure our data isn't empty before continuing
if (inStr != null) {
//trim whitespace and formatting characters (like carriage return and/or line feed)
inStr = trim(inStr); //Data is sent as a string
println("inStr =", inStr);
String[] myData = split(inStr, ','); // String is split into array at comma
println("array:");
printArray(myData);
for (int i = 0; i < myData.length; i++) {
try {
int parsedInt = Integer.parseInt(myData[i]);
data[i] = parsedInt;
}
catch (NumberFormatException e) {
println("Failed to parse the string as an integer.");
}
}
}
}
Arduino code:
int num[2];
void setup() {
Serial.begin(115200);
SerialUSB.begin(115200);
}
void loop() {
num[0] = random(255);
num[1] = random(300, 900);
SerialUSB.print(num[0]);
SerialUSB.print(","); // Comma separated values
SerialUSB.println(num[1]); // Has a line feed.
//Wait for a bit to keep serial data from saturating
delay(10);
}
Note that there is no SerialUSB.write().
By the way, your code does run if you declare a baud rate on the Arduino side, eg
SerialUSB.begin(115200);
Output in my console log (macos):
And it didn’t hang; went to 59 and then reset to zero.