'Serial between Arduino and ESP32 sending unwanted (possibly buffered?) characters

I am sending a String from an ESP32 to Arduino. The transfer itself is successful.

But every second time, the ESP appends characters from the beginning of the String to the beginning of the String itself (ik it is confusing). Ending the serial connection and recreating it every time before the transfer makes those characters unprintable (as if their contents were erased, but the chars themselves were still there?)

What I get on my serial monitor: (https://imgur.com/A8HLPV4)

You can see that the beginning of the message is a datetime. Every second time, it cuts out the same part from the datetime and appends it in front of the datetime itself, making it nonsense.

Here is the sender code:

Serial2.begin(9600, SERIAL_8N1, 27, 26);
String datetime = "2022-04-21-13-52-22 Vincent Cerowski ARRIVAL aaa";
Serial2.println(datetime);
Serial2.flush();

.flush() does not seem to help.

Here is the receiver code:

const byte numChars = 255;
char receivedChars[numChars]; // an array to store the received data

boolean newData = false;

static byte ndx = 0;
char endMarker = '\n';
char rc;

while (Serial.available() > 0 && newData == false) {

rc = Serial.read();
if (rc != endMarker) {receivedChars[ndx] = rc; ndx++; if (ndx >= numChars) {ndx = numChars - 1;}}
else {receivedChars[ndx] = '\0'; ndx = 0; newData = true;}
}

if (newData == true) {
   Serial.println(receivedChars);
newData = false;}

FULL CODE OF RECEIVER PROGRAM:

// Receive a NDEF message from a Peer
      // Requires SPI. Tested with Seeed Studio NFC Shield v2
      
      #include "SPI.h"
      #include "PN532_SPI.h"
      #include "snep.h"
      #include "NdefMessage.h"
      
      PN532_SPI pn532spi(SPI, 10); //10
      SNEP nfc(pn532spi);
      uint8_t ndefBuf[128];
      
      const int numChars = 200;
      char receivedChars[numChars]; // an array to store the received data
      
      boolean newData = false;
      
      int msgSize;
      
      void setup() {
          Serial.begin(9600);
      
      }
      
      void loop() {
      
      
              msgSize = nfc.read(ndefBuf, sizeof(ndefBuf));
              NdefMessage msg  = NdefMessage(ndefBuf, msgSize);
              //msg.print();
              //Serial.print(" ");
              
              // Get the first record
              NdefRecord record = msg.getRecord(0);
              // Get the payload size
              byte length = record.getPayloadLength();
              // Create byte array big enough for payload
              byte payload[length];
              // Get payload to byte array
              record.getPayload(payload);
              // Convert byte Array to string
              String message = String((char *)payload);
              //remove trailing nonprintable chars
              int stringlength = message.length();
              message.remove(stringlength-2);
              
              Serial.println (message); //this sends the message payload through Serial to the ESP32
              //Serial.flush();
      
          if (msgSize > 0){
              recvWithEndMarker(); //this is a function to receive a response over Serial from the ESP32
              showNewData(); //this is a function to display the received response and send it through the NFC adapter}
      }
      
      void recvWithEndMarker() {
       memset(receivedChars, 0, sizeof(receivedChars));
       int ndx = 0;
       char endMarker = '\n';
       char rc;
       
       while (Serial.available() > 0 && newData == false) {
        
          rc = Serial.read();
          if (rc != endMarker) {receivedChars[ndx] = rc; ndx++; if (ndx >= numChars) {return;}}
          else {receivedChars[ndx] = '\0'; ndx = 0; newData = true;}
        }
      }
      
      void showNewData() {
             if (newData == true) {
             msgSize = 0;
             Serial.println(receivedChars);
             newData = false;
      
              NdefMessage message = NdefMessage();
              
              message.addMimeMediaRecord("text/plain", receivedChars);
              
              int messageSize = message.getEncodedSize();
              message.encode(ndefBuf);
              if (0 >= nfc.write(ndefBuf, messageSize)) {Serial.println("Failed");} else {Serial.println("Success");}
             
             }
       }

FULL CODE OF SENDER PROGRAM:

 uint8_t ndefBuf[128];
      
      const int numChars = 200;
      char receivedChars[numChars]; // an array to store the received data
      
      boolean newData = false;

void setup(){Serial.begin(9600); Serial2.begin(9600, SERIAL_8N1, 27, 26);}

void loop(){recvWithEndMarker(); showNewData();}

void recvWithEndMarker() {//this function waits for the message payload sent over Serial from Arduino
       int ndx = 0;
       char endMarker = '\n';
       char rc;
       
       while (Serial.available() > 0 && newData == false) {
        
          rc = Serial.read();
          if (rc != endMarker) {receivedChars[ndx] = rc; ndx++; if (ndx >= numChars) {ndx = numChars - 1;}}
          else {receivedChars[ndx] = '\0'; ndx = 0; newData = true;}
        }
      }
void showNewData() {//this displays the received payload and calls the respond function
 if (newData == true) {
 Serial.println(receivedChars);
newData = false;
    respond();
 }
}

void respond(){//this sends back a response through Serial to Arduino, this is the part where the bug is
  
    String datetime = "2022-04-21-13-52-22 Vincent Cerowski ARRIVAL aaa";
    Serial2.println(datetime);
    Serial2.flush();
  
  }

Anybody knows what the cause is and how I can solve it?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source