Problem with parsing big packets on Serial port Arduino Nano

If the Serial buffer is full then you have screwed up. The simple way to do that would be to put delay(100) in your program.

I actually do that in the setup() of some of my important projects. There are delays initializing the LCD screen and other peripherals. So I clear the Serial buffer at the end of setup() like this...

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

MorganS: If the Serial buffer is full then you have screwed up. The simple way to do that would be to put delay(100) in your program.

I actually do that in the setup() of some of my important projects. There are delays initializing the LCD screen and other peripherals. So I clear the Serial buffer at the end of setup() like this...

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

Sooner or later, any buffer can be filled with data. It is not always possible to do the parsing often enough. Maybe just the CPU performance is not enough. If I understand correctly, then the buffer is filled with data by an interrupt from the UART RX. Therefore, it is necessary to make room for new data before starting the parsing in LOOP ().There should be a clear buffer command. After all, this is just zeroing the variable of the buffer filling counter. Can Serial.End () and Serial.began () apply and then enable parsing? Then the parsing will checknew data like in first packet, as after resetting the CPU. Of course, I will try also your buffer cleaning solution. Thanks!

Gennady: Can Serial.End () and Serial.began () apply and then enable parsing? Then the parsing will checknew data like in first packet, as after resetting the CPU.

Yes it does, also you can set the pointer to the last byte = pointer to the first byte (Serial.begin() sets both to 0, from the (ring)buffer offset. I think, i read it somewhere but i haven't checked inside hwSerial.cpp, or maybe i did, but haven't just now to verify my memory. You should, you could also add the function to clear the buffer to hwSerial as you think it suits you most. If the GPS is constantly broadcasting, there should be a break of a certain length in between broadcasts (or at least one would hope so) and you can look for that before you start listening. Since the main part is raw data, it may be hard to look for a header.

Sooner or later, any buffer can be filled with data. It is not always possible to do the parsing often enough. Maybe just the CPU performance is not enough.

in some way yes, though with you baudrate that should not really be an issue. Either way check out this way of receiving DMX on an Arduino, with having the ISR vectors redefined and the ISR's in the Sketch. There is a more modern way using the Conceptinetics DMX library, you can get some more details out of that.

Gennady:
Sooner or later, any buffer can be filled with data. It is not always possible to do the parsing often enough. Maybe just the CPU performance is not enough.

No. Serial is really slow. Even the slowest Arduino can perform thousands of operations between each Serial character arriving.

If you are burning up those thousands of instructions doing floating-point calculations then that doesn’t mean Serial is broken. It means you or your library are doing something wrong.

If I understand correctly, then the buffer is filled with data by an interrupt from the UART RX. Therefore, it is necessary to make room for new data before starting the parsing in LOOP ().

Nope. Parsing by using Serial.read() is the only thing that should be taking characters out of that buffer. If parsing is working fast enough then it will empty the buffer before the next packet finishes transmission.

There should be a clear buffer command. After all, this is just zeroing the variable of the buffer filling counter. Can Serial.End () and Serial.began () apply and then enable parsing? Then the parsing will checknew data like in first packet, as after resetting the CPU.

Double nope! If you erase the buffer then you would have to wait at least 100 milliseconds for the next packet to start. Then wait for that packet to finish. That is a long time for the Arduino to waste when it already had the data buffered and waiting for the parser.

Of course, I will try also your buffer cleaning solution. Thanks!

No. Don’t use that code. Just verify that the parser always completes its job before the next packet is complete in the Serial buffer.

Print out Serial.available() after getting that first packet. It should be a small number like 0 or maybe 100.

Thank! I will use your advice and conduct experiments.