ESP32 SerialRead timing problems and duplications

Im currently reading from STM32 which is sending Tx data (a buffer size of 10).

On my ESP32 I declared some variables as:
HardwareSerial MySerial(2);
const size_t BUFFERSIZE = 10;

so the reading of that would be:

void loop() {
  char buffer[BUFFERSIZE];
  while(MySerial.available() > 0){
    size_t bytesRead = MySerial.readBytes(buffer,BUFFERSIZE);
    Serial.printf("Received(%d): ", bytesRead);
    Serial.println(buffer);

Im having troubles when the readings are seconded with empty read line. The output looks like that:

18:17:45.847 -> Received(10): 13;77;
18:17:45.847 -> Received(10):
18:17:45.958 -> Received(10): 13;77;
18:17:45.958 -> Received(10):

I also wonder how often can I send such data from STM32 so the ESP32 would be able to catch up? Should I delay sending data or delay at receiving data?

your buffer is 10 bytes long and you attempt to read 10 bytes from Serial2 and then use the buffer as a cstring ➜ do you send a trailing null char as part of the 10 bytes?

Serial.readBytes() will terminate if the 10 bytes have been received, or if it times out. Do you have a constant flow of incoming data?

PS %zu is the proper format to use in printf with size_t data type.

Thanks for the reply.

On STM32 at declaration of buffer[10] it gets filled with '\0' so the trailing null does exist.
The first few bytes of buffer are used for my data, it's formatted "key:value;", where the key is a value from 10-99 and value is uint32_t. In my opinioin the buffer should never exceed size of 10, so the trailing null char should always be a part of my data.

As of constant flow, im still dealig with that. I figured when i debug on STM32, for each Transmit i've put a breakpoint. Manually stepping through did send the data over, but when I've let it run I didnt recieve anything.

Thanks for the tip. I struggle a little with conversions, where readString returns String, I cant handle formatting such type. Is using .c_str() a good practice when reading from Serial? -> How should I receive data if I'm transmiting a string which I convert to buffer of 10?
Example: ["1", "0", ":"1", "0", "0", "0", ";", "\0", "\0"]

To my knowledge, In C++, local arrays without an explicit initialization are not guaranteed to be initialized to zero. It's best practice to initialize them explicitly if you want them to start with zeros. You can do this by using the initializer syntax like char buffer[BUFFERSIZE] = {0};

The best practice with an asynchronous communication link is to do think asynchronously. I would suggest to study Serial Input Basics to handle this or write a small state machine parsing on the fly if you are sure it’s always the same format