Uint8_t array different after write

Hi Everyone,

I've been stuck on this problem for a while and couldn't figure anything out. My setup is that I have an Arduino Nano with an ESP01 onboard that reads from a socket hosted by my computer. The socket sends headers that have a constant length of 64 bytes. The issue here, is that after I read the data from the socket and save it to a buffer, and when I write it to Serial with Serial.write(), it's fine and it's the message that got sent through the socket.

However, I noticed, that immediately afterwards if you read it again to Serial, it's total garbage data, even though it should still be in scope.

Here's the relevant section

while (client.connected()) {
      Serial.println("Waiting for message...");
      int i = 0;
      while (client.available() > 0) {
        if (i == HEADER_SIZE) {
          i = 0;
        }
        header_buffer[i] = client.read();
        Serial.write(header_buffer[i]);
        received_message = true;
      }
      if (received_message) {
        Serial.println("");
        received_message = false;
        for (int j = 0; j < HEADER_SIZE; ++j) {
            Serial.write(header_buffer[j]);
          }
          Serial.println("");
      }
    }


note that header and received_message are initialized at the start of void loop()

uint8_t header_buffer[HEADER_SIZE];
bool received_message = false;

First it prints out 0123456789abcdefghijklmopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@^, which is exactly the contents of the packet I sent,

but then 5E2AC101839FD8F7EF1FFE7ED5FB77EDBE7B9CFECFACFEFDEF5FD7FBD597F7FFFFF745FB8DDFFFF7DF94FFBFDFFFAFFFFFD7FBFD78FFFFF771DEE3FF79DF31 afterwards. (I did put it in hex for the forum), which is even the wrong length (63).

I'm at a total loss of what I'm doing wrong, and help is much appreciated.

i don't see a i++

move..

to just below i=0;
after it reaches header size..

~q

you're so right. It works now, thank you so much!

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.