Splitting of Arduino Serial input into multiple strings

634 views Asked by At

Hello and thank you in advance for any help. I have an application which reads speed, rpm and gear values from BeamNG Drive and Serial prints them to a COM Port. It works in a loop, separating the values using end bytes.

RPM_END_BYTE = '+'; 
SPEED_END_BYTE = '='; 
GEAR_END_BYTE = '!'; 

Using these end bytes, I'm wondering how to, in arduino, split the input into three strings. I can either have them directly into an integer or use string.toInt(); to convert it. This is what one loop of the program's serial output looks like :

02500+120=6!

I have set up a virtual COM port on my PC to check that the software works and it does, however I can't seem to figure out how to split the input up.

I have also used the following code, which works when i input numbers through the Serial Monitor and end it with '+', but it does not work with my software in the same way.

const byte numChars = 32;
char receivedChars[numChars];   // an array to store the received data
#include <LiquidCrystal.h>
boolean newData = false;
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
void setup() {
    Serial.begin(9600);

    lcd.begin(16, 2);
}

void loop() {
    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;
        }
    }
}

void showNewData() {
    if (newData == true) {
        lcd.setCursor(0, 1);
        lcd.clear();
        lcd.print(receivedChars);
        newData = false;
    }

Thanks so much to anyone who can help. I apologize if a similar question has been asked already, but I cannot find one.

1

There are 1 answers

4
Frenchy On BEST ANSWER

an example of solution: i have adapted the program which is okay with '+' with the 3 endMarkers

char endMarker[3] = {'+', '=', '!'};

while (Serial.available() > 0 && newData == false) {
    rc = Serial.read();
    int returnvalue = testifendMarker(rc);
    if (returnvalue < 0 {
        receivedChars[ndx] = rc;
        ndx++;
        if (ndx >= numChars) {
            ndx = numChars - 1;
        }
    }
    else {
            if (returnvalue == 0){
                 // terminate the string with + do your stuff
                 // maybe use lcd.setCursor(Col, Row) to set the cursor position
                 receivedChars[ndx] = '+';
            }else if (returnvalue == 1){
                 // terminate the string with = do your stuff
                 // maybe use lcd.setCursor(Col, Row) to set the cursor 
                 receivedChars[ndx] = '=';
            }else {
                 // terminate the string with ! do your stuff
                 // maybe use lcd.setCursor(Col, Row) to set the cursor 
                 receivedChars[ndx] = '!';
            }
            //display the result on lcd
            lcd.print(receivedChars);// you just display 
            ndx = 0;
            // newdata = true; put this boolean to true terminate the loop while
    }
}   

int testifendMarker(char c) {
    for (int i=0; i < 3; i++){
            if (endMarker[i] == c){
                return i;
            }
    }

    return -1;
}