Serial send and receive data help

Hi all!
This may be a simple answer from many in the forum, but for me its been a struggle.

I'm able to send a request in the format of serial of 8bytes data. hence query to a RTU device.
in return the device responds via serial with 13bytes.
I can sort of read the bytes via:

if (Serial.available() > 0) {

incomingByte = Serial.read();

lcd.setCursor(1, 1); ( external I2C screen)

lcd.print(incomingByte);
}

The Question is, How can I save the 13 bytes received in order to be used later.
this is the request:
11,03,00,54,00,04,07,49

this is the expected response:
11,03,08,00,64,00,3E,00,0D,00,55,9C,E8

               if (Serial.available() > 12) { // got 13 bytes?
                   // read them into an array
                   for (counter = 0; counter <13; counter = counter +1){
                   incomingByte[0] = Serial.read();
                    lcd.setCursor(1, 1);     // change one of these counter ( external I2C screen)
                   lcd.print(incomingByte[counter]);
                   }
              }

May I suggest using a library? Perhaps one of my own used for serial data transfer - serial transfer library.

Wouldn't incomingByte[counter] = Serial.read(); work better?
@P B
Using libraries being tested and ready is of course favorable for lots of members but doing the hard work is very educating for those being interested in the back stage work

If the sending device always concludes its message with an end-marker byte (for example a line-feed character) then have a look at the second example in Serial Input Basics

...R

Railroader:
Wouldn't incomingByte[counter] = Serial.read(); work better?
@P B
Using libraries being tested and ready is of course favorable for lots of members but doing the hard work is very educating for those being interested in the back stage work

Noted, but don't you think that's OP's decision? :slight_smile:

Ehh?
I made a remark to the suggestion made by @CrossRoads.

My opinion about libs or not? Of cource, OP descides.

Another thought. Should members like You and me support the questioning member or run private discussions above the head of OP? I admit, it's easy to fall in that trap.

Why not just use the library for the protocol you’re using? It’s already been written and tested.

PS: which Arduino are you using? Software or hardware based serial?

Hi all!
Thanks for the replies!
After putting kids,pets,and wife to sleep, FINALLY got some time to test !!

I do not know how to code per say but learning as we go along.
After couple of changes to the First reply got some results!
and thanks to you guys I can print the byte received all 13, and have the array to work with!
here is a Video:

and the modified code:

if (Serial.available() > 12) { // got 13 bytes?
for (int counter = 0; counter <13; counter = counter +1){
incomingByte[counter] = Serial.read();

lcd.init();
lcd.setCursor(3, 0);
lcd.print("Data received: ");
lcd.setCursor(1, 1);
lcd.print(incomingByte[counter]);
}
}

I did used and tried couple Libraries, but most had timing issues at sending request or receiving it, maybe because I'm using a NodeMcu ESP8266. and most did not have all (0-16) function registers working.

anyways I love to learn step at a time and understand how libraries work in the background.

THANK YOU GUYS!!!!

@CrossRoads

Why is if(Serial.available()>12) instead of if(Serial.available() == 13)? Is there any advantage of the former or the both are the same?

GolamMostafa:
Why is if(Serial.available()>12) instead of if(Serial.available() == 13)? Is there any advantage of the former or the both are the same?

Which of them works if there are 14 bytes?

...R

Robin2:
Which of them works if there are 14 bytes?

OP has said that he has exactly 13 bytes to receive.

GolamMostafa:
OP has said that he has exactly 13 bytes to receive.

Things go wrong sometimes. Always try to allow for that.

If you test for exactly 13 bytes and 14 happen to be in the buffer the program will be completely stuck.

...R

After the 13th byte is received can the buffer be flushed?
By : Serial.flush();
Or it’s not a good practice?

Robin2:
Things go wrong sometimes. Always try to allow for that.

So much satisfying!! K+.

xtech007:
After the 13th byte is received can the buffer be flushed?
By : Serial.flush();
Or it’s not a good practice?

Serial.flush() is for the serial OUTPUT buffer - it causes your program to wait until all the data in the buffer has been sent.

You can dump the data in the input buffer like this

while(Serial.available() > 0) {
   byte dump = Serial.read();
}

...R