How do I know that the data received from serial is valid?

I understand people says using if (Serial.available()) to determine if serial is transmitting.

There is a problem which is when I received data only in if (Serial.available()) and start to work on my code, because of the time delay, I receive the data in incomplete pieces.

For example I send "Hello world", it might be received in "el wrd" with missing letters in between.

So in order to get a complete sets of char, I use a loop to collect all data. problem is when is use that while data are not sending, arduino still chunk out �. How do I differential this? What exactly is �?

Sounds like data overrun. What baud rate? What is your end of line char? Are you discarding "00"s?

1 Like

I would suggest to study Serial Input Basics

1 Like

Maybe example 2 and up in Serial Input Basics - updated will give an idea.

If you use delay() in your program, you can easily get buffer overruns as mentioned above.

Post your code and we might be able to advise further. What is the sending side; any protocol involved?

1 Like

One of the big challenges in programming is that you have limited amounts of processing time.

To handle real-time data of any sort, you need to buffer the incoming data as well as processing it when the computer has time to - not when you’re ready to. :scream_cat:

These are harsh rules, but they’re the only rules.

That’s why getting rid of delay(), and other techniques are so important

1 Like

that's not a correct statement. Serial.available() indicates that serial data has been received, not transmitted

consider using Serial.readBytesUntil() to capture a complete line (terminated be a specified character) before processing it

+1 for the Serial input basics tutorial. It shows how to read the entire line into a buffer for later parsing. Use start and end markers to synchronize the sender and receiver and simple error detection.

  • Implement data structure checks, error checking codes, and start/end markers to validate the received data.
  • Apply data range or value constraints and perform consistency checks to ensure data integrity.
  • Use timeouts and synchronization techniques for handling delays and ensuring proper data reception.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.