To receive and read multiple bytes from a Serial device

Hello everyone, I'm very new to Arduino.

I have a serial device which I send an array of bytes to and the device will return 20 bytes.

I managed to send out the array of bytes to the device, however, I am having difficulty to read these 20 bytes returned from my serial device. From what I read on the forum it seems like Arduino is only able to read 1 byte......

You advice will be much appreciated!

Hello and welcome,

There is a 64-chars input buffer in which the received characters are stored until they are read. This is working as a FIFO queue.

Serial.read() will dequeue the first character. So, consecutive calls to Serial.read() will read all the received characters.

You must read the received characters (emptying the buffer) as fast as possible, or you will lose data if the buffer is full, because it is shifted to make room for the newly received character newly received characters will then be discarded.

Maybe reading serial input basics might get you on the right track.

ophirian:
I managed to send out the array of bytes to the device, however, I am having difficulty to read these 20 bytes returned from my serial device. From what I read on the forum it seems like Arduino is only able to read 1 byte......

You advice will be much appreciated!

Serial is all 1 byte at a time, the serial byte has a start bit, 8 data bits and a stop bit, all 1 at a time.
When you write or read serial, none of it "has to go together" and every byte arrives over time ... your controller is insanely faster than Serial, why you check for serial available before reading it.

Your code can read 1 byte at a time and assemble the data then or copy the data into an array and do thew same thing after the data is completely sent. The latter method will need more RAM and always finishes later, but it's what beginners and IT dimwits are usually taught to do.

If you want an intro to read and interpret as bytes come in, the second address in my sig space below is to Nick Gammon's blog on reading serial without blocking, the State Machine section will give you a power tool for real time coding. If you don't know what 'blocking' is, the first address has a lesson on that and how to make many things work together smoothly.

Seriously, for small-memory microcontrollers these are "how it's done" since there were microcontrollers. Use PC approaches when you want to limit what your device can achieve.