Chat freely about anything...

User avatar
By Trey
#52518 I am listening to packets and copying the bytes into a larger byte array with a destination start index.
Directly after copying I can see by printing to the serial monitor that the larger array got the payload.
When I go try to get the data from the larger array in a different method it's empty. Am I doing something wrong.

Listen prints Data as expected. The next method prints 0's. Am I missing something?

Code: Select allbyte Data[9999];

void listen()
{
         int noBytes = Udp.parsePacket();
         if(noBytes > 0)
         {
                 int idx = packetBuffer[0] | packetBuffer[1] << 8;
                 std::copy(packetBuffer + 2, packetBuffer + 1472, Data + idx);
                 for(int i = 2; i < 1472; i++ )
                 {
                              Serial.println(Data[i]);
                 }
     

         }
}


void next()
{
   for(int i = 0; i < (the number of bytes I want from Data); i++ )
                 {
                              Serial.println(Data[i]);
                 }
}



User avatar
By martinayotte
#52548 Why are you writing the copy at "Data + idx" destination and then printing content from "Data[0]" ?
Big value of "idx" can also cause an "out of bound" exception if not within your "Data[9999]" declaration.
User avatar
By Trey
#52549 I am not sure what you mean, I built the project over the course of a year, the sketch worked perfectly until I lost it and had to start from scratch. Above is pseudo code. Iv'e been frantically rewriting since yesterday.

PACKET 1gives me info about.
Packet 2 idx = 0 here so it should be copied to data starting at 0
Packet 3 When we get here idx should last packet size
Packet 4 When we get here idx should last packet size * 2
User avatar
By martinayotte
#52550 Your code explicitly provide destination address calculated from "idx" received from the packet :

Code: Select allint idx = packetBuffer[0] | packetBuffer[1] << 8;
std::copy(packetBuffer + 2, packetBuffer + 1472, Data + idx);


Since packetBuffer[0] and packetBuffer[1] are even unknown, depending of the what sender provide, if for example those are 0xFF and 0xFF respectively, you will get an "out of bound" exception because idx will have 65535 value.

If you wish to copy the packet at start 0, simply do (no need for "idx") :
Code: Select allstd::copy(packetBuffer + 2, packetBuffer + 1472, Data);