Best way to recieve UART.

Hi, I am working on project that require communication over internet(Wi-Fi).
I am using Arduino mega 2560 and for internet communication ESP8266 in server mode(active receive mode).
My program basically check every loop cycle with

if(Serial1.available())

read message received from ESP8266 and send answer to message. Program also measure and set some digital pins and also communicates with display over I2C.
My problem: I would like to decrease response time for message so I am thinking of rising baud rate from 9600 to 115200. Received messages are quite small(they usually fit to 64 byte buffer) but response message can have up to 3kB(according to my calculation a could cut time spent on sending message from 2.13 to 0.17 second on 2kB message).
My question: what is better way to handle receiving message? I can think of 2 options
1.) the way I do it currently, using Serial1.available()
2.) setting ESP to passive recv mode and polling message every loop cycle
The reason I am considering switching to second option is because what I found on forum is that UART received signal is stored inside 64 byte buffer and with faster baud rate the overflow could happen before one cycle elapses(correct me if I am wrong).

The serial input basics tutorial may have information that you can use.

The reason I am considering switching to second option is because what I found on forum is that UART received signal is stored inside 64 byte buffer and with faster baud rate the overflow could happen before one cycle elapses(correct me if I am wrong).

he method in the tutorial receives the data into a user defined buffer so the size of the hardware serial receive buffer is not relevant. The user specifies the size of the receive buffer.

As always, we have to wonder if you are using an ESP8266, why you would want a Mega 2560 as the ESP will generally be able to handle all the necessary processing and the serial communication between the two makes thing much more difficult.

If it is about multiple I/O, ADC or PWM, there are expansion modules which make it even easier than using a Mega 2560. :grinning:

On an esp32 or esp8266 with the OS freeRTOS, I’d use a task to receive serial like so:
This code if from a ESP32 but will work with some modification to the str of not using PSRAM; which the 8266 does not have.

void fReceiveSerial_LIDAR( void * parameters  )
{
  bool BeginSentence = false;
  char OneChar;
  char *str;
  str = (char *)ps_calloc(300, sizeof(char) ); // put str buffer into PSRAM
  // log_i("Free PSRAM before String: %d", ESP.getFreePsram());
  for ( ;; )
  {
    EventBits_t xbit = xEventGroupWaitBits (eg, evtReceiveSerial_LIDAR, pdTRUE, pdTRUE, portMAX_DELAY);
    if ( LIDARSerial.available() >= 1 )
    {
      while ( LIDARSerial.available() )
      {
        OneChar = LIDARSerial.read();
        if ( BeginSentence )
        {
          if ( OneChar == '>')
          {
            if ( xSemaphoreTake( sema_ParseLIDAR_ReceivedSerial, xSemaphoreTicksToWait10 ) == pdTRUE )
            {
               xQueueOverwrite( xQ_LIDAR_Display_INFO, ( void * ) &str );
              xEventGroupSetBits( eg, evtParseLIDAR_ReceivedSerial );
              //
            }
            BeginSentence = false;
            break;
          }
          strncat( str, &OneChar, 1 );
        }
        else
        {
          if ( OneChar == '<' )
          {
            strcpy( str, ""); // clear string buffer
            BeginSentence = true; // found beginning of sentence
          }
        }
      } //  while ( LIDARSerial.available() )
    } //if ( LIDARSerial.available() >= 1 )
    xSemaphoreGive( sema_ReceiveSerial_LIDAR );
    //        log_i( "fReceiveSerial_LIDAR " );
    //        log_i(uxTaskGetStackHighWaterMark( NULL ));
  }
  free(str);
  vTaskDelete( NULL );
} //void fParseSerial( void * parameters  )

A sentence is sent to the ESP with the format of <#,data,data,data,data,data,data,data,data>. The above task will only start to receive with a begin sentence marker, extracts Serial data as it comes in, and places that data into a separate buffer until the >, end of sentence, is received. Works for long or short messages. Whiles no data is being receive the CPU can run other tasks. The task is triggered once a mS by the event evtReceiveSerial_LIDAR, if there is data in the serial buffer the task works to extract it, the task stays with the receiving from the buffer has hit a end of sentence marker. At the end of sentence the task stuffs the received info into a queue and sends it off to be parsed by another task. Serial coms set to 115200.

GroundFungus the tutorial is only high level abstraction, but on processor level there is only predefined buffer somewhere in arduino libraries to 64 bytes. Try running this code and during delay send message longer than 64 bytes.

void setup() {
  Serial.begin(115200);
}

void loop() {
  Serial.print("Received:");
  while(Serial.available()){
    Serial.write(Serial.read());
  }
  Serial.println();
  delay(5000);
}

You will see that only first 63 bytes are echoed back(I don't know why not 64).
And I have read the guidelines.
Paul__B do you think that ESP-01s with extension boards can handle 2 UARTs, I2C, 50 analog pins of which couple are interrupt? If yes, thanks for advice I will use it in my next projects.
Idahowalker thanks for help, but I doesn't really solve my trouble.

ketchp:
GroundFungus the tutorial is only high level abstraction, but on processor level there is only predefined buffer somewhere in arduino libraries to 64 bytes. Try running this code and during delay send message longer than 64 bytes.

The answer to that is very simple. Don't ever have a delay() in a receiving program.

...R

I do not write code with delay(). Code that I write does not block for over 99% of the time. If you write non-blocking code like in the tutorial, the size of the serial receive buffer is irrelevant.

For non-blocking Serial Text I/O see Arduino Serial I/O for the Real World
That covers non-blocking buffered read and write to Uarts. Works for both Mega and ESP32
Includes simple code to extract commands from the text stream.

The code was developed in part for a commercial project running on an ESP32, but which may need to be split into a Mega / ESP32 with I/O like yours