For a RF project I need to split payload for a max predefined chunk size.
For that I created a RFmsg
struct.
The unexplained part I need help in is why when I create a local instance of RFmsg
, its contents are remain in tact after function is recalled? is it a struct "thing"? or something else I'm missing ?
To solve that I know that wiping struct content after initiating will solve that, but I'm want to be sure.
for that I share a minimal code that:
void newMSG(char *msg)
simulates the splitting part of a longmsg
input.void printStrcut_content(RFmsg &_payload)
prints struct's content.- output show that only after boot, its content are gibberish, but after that, it shows the content of last struct update.
CODE:
#include <Arduino.h>
#define MSG_LEN 10
#define DEVNAME_LEN 8
char *devName = "TEST";
struct RFmsg
{
int msg_num;
int tot_msgs;
int tot_len;
char payload[MSG_LEN];
char dev_name[DEVNAME_LEN];
};
void newMSG(char *msg)
{
RFmsg _payload;
printStrcut_content(_payload);
_payload.tot_len = strlen(msg);
strcpy(_payload.dev_name, devName);
_payload.tot_msgs = (int)(_payload.tot_len / MSG_LEN);
if (_payload.tot_len % MSG_LEN > 0)
{
_payload.tot_msgs++;
}
for (uint8_t i = 0; i < _payload.tot_msgs; i++)
{
strcpy(_payload.payload, "");
for (uint8_t n = 0; n < MSG_LEN; n++)
{
_payload.payload[n] = (char)msg[n + i * MSG_LEN];
}
_payload.payload[MSG_LEN] = '\0';
Serial.print("chunk #");
Serial.print(i);
Serial.print(": ");
Serial.println(_payload.payload);
}
}
void printStrcut_content(RFmsg &_payload)
{
Serial.print("LEN: ");
Serial.println(_payload.tot_len);
Serial.print("PAyLOAD: ");
Serial.println(_payload.payload);
Serial.print("name: ");
Serial.println(_payload.dev_name);
Serial.print("tot_msgs: ");
Serial.println(_payload.tot_msgs);
}
void setup()
{
Serial.begin(115200);
while (!Serial)
;
Serial.println("\nStart");
}
void loop()
{
newMSG("1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ");
delay(5000);
}
OUTPUT:
Start
LEN: 36
PAyLOAD: UVWXYZ
name:
tot_msgs: 4
chunk #0: 1234567890
chunk #1: ABCDEFGHIJ
chunk #2: KLMNOPQRST
chunk #3: UVWXYZ
LEN: 36
PAyLOAD: UVWXYZ
name:
tot_msgs: 4
chunk #0: 1234567890
chunk #1: ABCDEFGHIJ
chunk #2: KLMNOPQRST
chunk #3: UVWXYZ
LEN: 36
PAyLOAD: UVWXYZ
name:
tot_msgs: 4
chunk #0: 1234567890
chunk #1: ABCDEFGHIJ
chunk #2: KLMNOPQRST
chunk #3: UVWXYZ