Arduino readUntilString() clarification/help

Thread Starter

iceburnhex

Joined Jun 14, 2022
19
Hi all,

I am not really a programmer by trade, and trying to get some stuff that I think is considered basic, but not really sure what to search for or how to go about, so bare with me if it's not super clear.

I'm using an Arduino Uno, just using their IDE for programming a few small functions.
One item that I'm having trouble with is the Serial.readStringUntil('\n') command.
It works as intended (read a string, put it into a variable, end at new line). But I'm having difficulties reading a specific value inside of the string.

I will get some lines of code, such as "Value1", "Value2", and "Generated + X" where the X is randomized value that keeps changing.
The readStringUntil('\n') works great for the first 2, as it calls the appropriate functions once it reads them, but once i get the "Generated " line, it doesn't always work because if the X value changes, it no longer sees the string as something specific.

So is there a way to read the entire line, but then parse through it for a specific string (like once it reaches the end of Generated" it knows the line is what I'm looking for?
 
Last edited by a moderator:

djsfantasi

Joined Apr 11, 2010
9,129
I am not really a programmer by trade, and trying to get some stuff that I think is considered basic, but not really sure what to search for or how to go about, so bare with me if it's not super clear.

I'm using an Arduino Uno, just using their IDE for programming a few small functions.
One item that I'm having trouble with is the Serial.readStringUntil('\n') command.
It works as intended (read a string, put it into a variable, end at new line). But I'm having difficulties reading a specific value inside of the string.

I will get some lines of code, such as "Value1", "Value2", and "Generated + X" where the X is randomized value that keeps changing.
The readStringUntil('\n') works great for the first 2, as it calls the appropriate functions once it reads them, but once i get the "Generated " line, it doesn't always work because if the X value changes, it no longer sees the string as something specific.

So is there a way to read the entire line, but then parse through it for a specific string (like once it reaches the end of Generated" it knows the line is what I'm looking for?
So yes, you can read an entire line and parse through it. But you need to learn how to use the Arduino String functions (click on link).

Things may have changed, but using strings in Arduino code wasn’t very reliable and often caused the code to crash (YMMV). I use strings sparingly, such as only for constants.

The other method is to use arrays of char variable type to manipulate strings of characters.

Your description of the lines you will read is not specific enough for me. Can you provide several examples?
 

Thread Starter

iceburnhex

Joined Jun 14, 2022
19
So yes, you can read an entire line and parse through it. But you need to learn how to use the Arduino String functions (click on link).

Things may have changed, but using strings in Arduino code wasn’t very reliable and often caused the code to crash (YMMV). I use strings sparingly, such as only for constants.

The other method is to use arrays of char variable type to manipulate strings of characters.

Your description of the lines you will read is not specific enough for me. Can you provide several examples?
I'm reading Serial Data in from a different device that I have no control over. Specifically, it uses the phrase "MotorLimitDebug XYZ" which is what I'm looking for. The XYZ will change, but overall its based on what the mechanism is doing and hence why I need to see "MotorLimitDebug" to be able start a counter and eventually complete my indications I'm building on the arduino.

I'll definitely go through that link today and see if I can make sense of it.

As far as code context:



while (Serial.available()) {
RX_CMD = Serial.readStringUntil('\n');
}
if (RX_CMD.indexOf("MotorLimitDebug") > 0) {
counter = counter + 1;
sendln(counter);
}
if (counter == 4) {
sendln("RecallFunction0");
}
if (RX_CMD == ("SeqComplete 0")) {
digitalWrite(Red, LOW);
digitalWrite(Green, HIGH);
complete = true;
}



This is the part of the main loop that isn't currently working for me. If I send the SeqComplete 0 line, it reads it just fine. It's just I need a way to increase the counter by interpreting the MotorLimitDebug lines with variable XYZ afterwards. My sendln commands are just Serial.print(__VA_ARGS__);
 
Top