Questions regarding the capture of serial data

I'm trying to wrap my head around some concepts related to serial communications.

For example, if I'm communicating with a WiFi adapter (an ESP01) and sending "AT" commands to it,
I know that I need to define a char array to hold the bytes that I receive from it via commands such as "ESP01.read()".

Question 1: How do I know when that device is done sending data back to me?

Question 2: Do I need to incorporate a delay to make sure I don't empty the buffer before the response is completely sent?

Question 3: How do I know in advance how large to make my char array? Is there some way to make the array grow dynamically as more space is needed?

There is will usually be an end marker in this case; e.g. a newline or a carriage return. You can print the received bytes as hex and see what the end marker is (probaby 0x0A or 0x0D).

You should hardly ever have to incorporate delays anywhere in your code; in this case it might break more than it solves :wink: Have a look at Robin's updated Serial Input Basics thread to get ideas.

//Edit
3)
That's the tricky part :wink: Analyse the reply to every command that you send. Either by trial and error or by looking at the protocol / datasheet.

  1. whenever you receive a char, reset a timestamp (e.g. msecLst). if you haven't received a char after some amount of time since the last received char (e.g. millis() - msecLst > Timeout) assume you've received the complete message

  2. it's common to allocate a string array more than large enough for the longest message. assume you've received a message if you reached the end of the buffer.