Read whole data stored in serial buffer

Hello,

I'm trying to communicate with a aftermarket car ECU via RS-232.
I can send a request packet to my ECU, and it replies with the requested data.
The amount of data differs from what I requested.
It can be about 30 Bytes of data sent by the ECU to my DUE.

So far so good...

But, how can I read all the data stored in the Serial Buffer? (It can store up to 64 Bytes, right?)
With the available method, I can only count the amount of data received. I found no method to read the whole Buffer.

Best regards
Bastian

Data over serial drops in one at the time.
You must build up the returned data piece by piece. Often there is a or at the end of an response. So read until the terminator and then you can analyze the response.

What about something like this

numBytes = Serial.available();
for (n = 0; n < numBytes; n++) {
    myInputBuffer[n] = Serial.read();
{

But as @Robtillart has said you need to have some means to know when all the data has been received in the Serial buffer.

Look at the Arduino code in this demo for a more reliable solution.

It assumes the incoming data will be surrounded by < and > but you can easily change it to any other characters.

…R

Hi,
thanks for your answers.
Which datatype should the array myInputBuffer[n] have?

Which datatype should the array myInputBuffer[n] have?

The same kind you are using now to read the data!

Serial.read() returns a byte so that is what the array needs to hold.

...R

Serial.read() returns a byte

Actually, it returns an int. The data is in the low order byte. The error, if there was one, is in the high order byte.

If you KNOW that there will be no error, because you have used Serial.available() to make sure that there is something to read, then you can use byte or char, depending on whether you are receiving ASCII data or binary data.

PaulS:
Actually, it returns an int.

What I like about this Forum is that you learn something every day.

I have always put what I read into a byte or char so I was not aware there was other info available.

...R