Reading and Parsing String from Raspberry Pi on an Arduino

We are trying to receive a string that will give values like 1473,1492,1100,0,1; from a raspberry pi and take that data and assign the first number (1473) to heave, the second to surge, etc. Attached is the code we have so far, suggestions please!

const byte numChars = 20;
char receivedChars[numChars]; // an array to store the received data
int heave;
int sway;
int surge;
int pan_left;
int pan_right;

boolean newData = false;

void setup() {
Serial.begin(9600);

}

void loop() {
//This code will continuously send ‘’ python.
//Serial.println("");
delay(400);// must be added.
recvWithEndMarker();
showNewData();
}

void recvWithEndMarker() {
static byte ndx = 0;
char endMarker = ‘;’;
char rc;

while (Serial.available() > 0 && newData == false) {
rc = Serial.read();

if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = ‘;’; // terminate the string
ndx = 0;
newData = true;
}
heave = receivedChars[1];
sway = receivedChars[2];
surge = receivedChars[3];
pan_left = receivedChars[4];
pan_right = receivedChars[5];
}
}

void showNewData() {
if (newData == true) {
Serial.println("This just in … ");
Serial.println(receivedChars);
Serial.println("Surge = ");
Serial.println(surge);
Serial.println("Heave = ");
Serial.println(heave);
Serial.println("Sway = ");
Serial.println(sway);
newData = false;
}
}

Suggest you tell us if your code works or not! If not, what do you get?

Paul

Parsing should be done in showNewData() which is when you already received all the data.

You have your level shifters working, have you completed a loop back test on the RPi side?

Have you come up with a data format?

Such as beginning, ending, and data separators?

I send something like <@,129875,3,5,7,9>. The <> represents sentence begin and end, I use ! or @ or # or $ or % or ^ and so on and so forth as data identifiers, and the comma is a separators.

First you got to have a successful RPi loop back test, then you need to add in level shifters before connecting the Arduino to the RPI, unless you got a 3.3 Arduino.

@kmarshall, you seem to be using code from my Serial Input Basics. That tutorial includes a parse example that illustrates how to do what you want.

Don't add code for other features into the recvWithEndMarker() function - put that extra code into its own function. It is much easier to develop and test programs if each function has a single clearly defined purpose.

...R