What I am missing is a method which tells a "new line" is available in the serial RX buffer.
I want to parse commands coming in as a string with \n at the end. So I am looking for a way how to, for example, create a method which will return a number of chars (including \n) if a new line could be find in the rx buffer, or 0 if none \n find.
if (Serial.NLavailable()) {
my_parse(...);
}
My understanding is the serial rx interrupt service routine may include a piece of code monitoring each char coming in, when \n detected it shall set a variable (returned by the method) with the number of chars from beginning of the buffer to the first \n inclusive. Based on that above simple test I can read the new line with something like Serial.readLine() and parse..
Is there any experience with that, is that feasible, or maybe does it exist somewhere?
Roll your own version or copy many of the examples in this forum.
Read each character from serial and put it into a char array. Add a '\0' to the next array position each time. When you read a '\n' the array will contain the string that you want.
Yea, that is a traditional approach - reading chars from serial. I am looking for a service coming from Stream class (so I must not deal with reading chars in the main, but do it only after I get a signal - "a new line is here").
pito:
I am looking for a service coming from Stream class (so I must not deal with reading chars in the main, but do it only after I get a signal - "a new line is here").
In order to implement this it would be necessary to read the incoming characters as they become available, which implies that they also need to be buffered. This would probably be necessary in any case since the buffer in HardwareSerial is not very big and could not be relied on to hold a complete input line. The code to receive incoming bytes, buffer them, and process the buffer when a new line is received is very simple and has been posted here many times - it does not feel to me like a problem that needs to have a solution embedded in a stream class, but you always have the option of implementing your own line-oriented stream input class if you want to.
pito:
Yea, that is a traditional approach - reading chars from serial. I am looking for a service coming from Stream class (so I must not deal with reading chars in the main, but do it only after I get a signal - "a new line is here").
It would have been good to know what you really wanted.