Alternative to while(Serial.available()){Serial.read();}

Hello,

I recently updated my old code I used to control my CNC machine using Arduino Uno, the new code includes no Strings and non blocking serial and everything is (finally! :sunglasses: ) working as expected, I am using a new library for SD card reading and I wanted to replace the original Serial that comes with the IDE with another lightweight library called PetitSerial. the problem comes when I want to use:

while(Serial.available()){Serial.read();}

to clear the incoming buffer, and I get: class PetitSerial' has no member named 'available'
kindda frustrating because I only need to clear once in order to get everything working.Do you guys know an alternative way to clear?
The documentation says that .available returns the number of elements in the buffer so I tried this instead:

while (Serial.read()>=0){
Serial.read;
if (Serial.read() ==0){
break;}}

sometimes it works but sometimes the CNC freeze.I googled it a lot here in the forum and in C/C++ forums but I only could find something called the tcflush function but those examples there are beyond my understanding :slightly_frowning_face:

I mean how people did clear the serial buffer in avr devices before arduino Serial.available appears?

Thanks in advance!

Maybe post a link to the source code for 'PetitSerial'?

If this is the PetitSerial library

the function read() is unbuffered, aka doesn't use a buffer, so you don't need to empty a non-existing buffer.

read() should return -1 if there is no data in the buffer

Mark

If, instead of "lightweight" you mean "brain damaged", then go ahead.

You really cannot do better than the Arduino Serial for general-purpose serial. Especially if you want to use functions like available().

The op and the designer of this light weight "Serial" also need to remember that functions which are not used are dropped by the linker!.

Note - functions and class members are not the same thing .......... available() is not a function!

Mark

gfvalvo:
Maybe post a link to the source code for 'PetitSerial'?

vlc0617:
If this is the PetitSerial library

PetitFS/src/PetitSerial.h at master ยท greiman/PetitFS ยท GitHub

the function read() is unbuffered, aka doesn't use a buffer, so you don't need to empty a non-existing buffer.

wow! I hit reality and it hurts! yes that is, as soon as I read your reply I ran to my PC like:it is not the library, it is not the library! and it turned out to be a pair of unattended variables making a mess while passing from a case to another and then going back to the original state!. I fixed that and the arduino is not going to run into the woods anymore.

holmes4:
read() should return -1 if there is no data in the buffer

So I need to rethink all the serial stuff then, if I keep PetitFS , do you think is still a good approach to use a state machine with RecvEndMarkers? All that I need is to do is to send a char '?' and the machine will respond with a string like this: <Idle|MPos:0.000,0.000,0.000|Bf:15,127|FS:0,0|WCO:0.000,0.000,0.000>
so I am using the last character '>' as endmarker and storing the whole stuff into a char array. do you think this make sense even if Serial.read() is unbuffered or I am missing the point?

MorganS:
If, instead of "lightweight" you mean "brain damaged", then go ahead.

You really cannot do better than the Arduino Serial for general-purpose serial. Especially if you want to use functions like available().

lightweight just in terms of dynamic memory, I would like to add more features to the machine, that was original reason to remove all the String stuff, to make room.

so back to the lab!

to answer the title:

while (Serial.read() != -1);

Serial uses no dynamic memory. It does use a chunk of static memory for its input and output buffers but you can modify the sizes of those if it is really important to you.

@holmes4 How can something that looks like a duck and acts like a duck not be a duck when it is riding on a classboat?

Sometimes I call them "methods" even though I know that is a Java word, not a C++ word.

Juraj:
to answer the title:

while (Serial.read() != -1);

That solved my problem! reading the entire string using petitSerial.Now both version works like a charm, Thank you!

MorganS:
Serial uses no dynamic memory. It does use a chunk of static memory for its input and output buffers but you can modify the sizes of those if it is really important to you.

Now I am confused, when I use petitSerial instead of Serial, I go from 27% to only 19% of dynamic memory! how can I modify the buffers of Serial? according to GRBL wiki, they use a buffer of 127 instead of 64

Just be more confusing, the dynamic memory in an AVR Arduino is SRAM, which stands for Static RAM. It does not need to be continuously refreshed like DRAM but it also does not retain the memory when the power is off.

Big-S strings use the SRAM dynamically. Each new String or manipulation of a String must allocate a new piece of heap memory to work with. Under the wrong conditions, that can consume the 80% of memory that the compiler reported as available.

If you avoid dynamic memory hogs like Strings then that compiler report can use 90% or more of the SRAM before you start looking at off-label libraries that save a few bytes.

so it was all about removing Strings and not really changing libraries, well now I know a little more than yesterday :slight_smile: thank you guys I will go back to the lab to try my bland new code!