2

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.

Wolfgang Fahl
  • 15,016
  • 11
  • 93
  • 186

1 Answers1

0

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;
}      
Frenchy
  • 16,386
  • 3
  • 16
  • 39
  • I’ll be able to test this soon, thanks so much for this! – Creagh Duggan Dec 15 '18 at 20:09
  • I have used this code and terminated the strings using "receivedChars[ndx] = '=';" with +,=,! where your indents are, and then printed receivedChars to the lcd screen, however; once one result (rpm) has been printed, the program stops. My code is here https://pastebin.com/MFaTUgy3 Would you mind telling me what I'm doing wrong? Thank you. – Creagh Duggan Dec 15 '18 at 21:39
  • its normal you have modified the logic of your program, i have adapted to your program. i modify the solution , i put comment on line newdata = true – Frenchy Dec 16 '18 at 06:23
  • Thank you, your program works perfectly for its purpose, when the serial monitor is used to input numbers, however; it did not work when I was using the BeamNG details sender. This leads me to believe it's a problem with my program, not the arduino code. Thanks very much for your contribution! I'll post any updates and working code if I get it all working. – Creagh Duggan Dec 16 '18 at 13:36