Modifying a serialport reading

Hi there,

The below code is not working. Where am I wrong?

String sat = String(Serial.readString());
if (sat.substring(3,9) == "IrmsLimit") //If there is "IrmsLimit" in substring sat
    {
      IrmsLimit = sat.substring(0,12); // "0.0IrmsLimit" is the substring
      IrmsLimit.remove(3,9); // Delete "IrmsLimit"
      Serial.print(IrmsLimit); //Write string IrmsLimit (0.0)to serialport
      Serial.println("is the Irms Limit");
    }
String sat = String(Serial.readString());

The readString() method returns a String. There is no reason to create ANOTHER String instance, wasting even more memory, no matter how temporarily.

String sat = Serial.readString();
      IrmsLimit = sat.substring(0,12); // "0.0IrmsLimit" is the substring

You should NOT be using substring that way, without KNOWING that there are at least 12 characters in the String.

      IrmsLimit.remove(3,9); // Delete "IrmsLimit"

It's silly to copy the 9 characters into the IrmsLimit string, in the first place, when you just want to make them go away.

The below code is not working. Where am I wrong?

You have not defined what the code ACTUALLY does, or what you expect it to do. It IS working, so it must be your expectations that are wrong.

Thanks for your comments. How could i define sat as serial read? As below?

sat = Serial.readString();

Although i changed it to following, the result doesnot change, it doesnot send the data.

sat.substring(0,2);

I have tried to send the data as "0.0IrmsLimit" from VS and wrote this code as feedback to VS again from Arduino.. But I couldnot see the parsed result in VS :frowning:

Although i changed it to following, the result doesnot change, it doesnot send the data.

Changing how you deal with data you have already read does NOTHING with how anything sends any data anywhere.

What is sending the data that you are trying to receive, using readString()?

I have tried to send the data as "0.0IrmsLimit" from VS and wrote this code as feedback to VS again from Arduino.. But I couldnot see the parsed result in VS

Visual Studio does not send and/or receive serial data. It is an application that is used to build applications that might send and/or receive serial data.

If you use VS to build an application using C# as the programming language, it is not all that difficult to have that application read serial data. Not trivial, but not all that hard. If the end result is that you don't care what the Arduino says about the parsing process, because it just works, debug the parsing process using the Serial Monitor application to send/receive data.

PaulS, you made me laugh. Thank you very much for you philosophic answer:)
Sending "0.0IrmsLimit" recieving "0.0is the IrmsLimit"..
By the way, my serial monitor doesnot work. So that i use two more softwares for displaying serialport or i use debugging at VS. As i understand from your answer this code seems like working. Hmm. While i m getting no errors but can not see at serial monitor, it seems interesting.

Thank you for sharing time for this problem!

Hello jentzsch, I think the solution to your problem will be to start thinking in a new and different way, don't let this deter you in the end it will be for the best.

The first thing to do is get familiar with C strings, a C string is an array of characters terminated with null ('\0'), the following link gives a good description.

https://www.tutorialspoint.com/cprogramming/c_strings.htm

Next read through the examples at the following link in particular example #3. Copy example #3 and watch how it receives and displays C strings using the serial monitor.

Don't worry about parsing substrings or communicating with a Visual Studio app. all that will come easy if you understand the previous two points

sumguy thank you very much for your help. I will be trying 3rd example with modifying to my problem. I will be hopefully informing here if i suceed:)

Robin2’s code has proven to be very reliable so I am sure you will succeed. The next step for you would be to transmit a string from your PC app to your Arduino in the format that he suggests, something like . Ideally your VS program will contain a text window of some kind so that you can echo the string back to the PC as proof of operation. I am pretty sure you could also get a pointer or two on the Visual Studio side of things.

C strings are very flexible and there is a library of methods that allow you to manipulate them in many ways, here is a link to a tutorial.

https://www.tutorialspoint.com/arduino/arduino_strings.htm