I'm writing a democode for showing how ESP-NOW on ESP8266 / ESP32 works.
usually a typedef struct is used for "packing the data"
Now of course the user can change the typedef struct
Is there a way to automatically parse the typedef struct which datatypes are defined so that a Serial.print can always print the right way?
Perhaps when the struct is one byte long. Once it is two or more bytes long, you cannot tell if it stores two bytes, an int, or a union that permits both. Good Luck !
StefanL38:
Is there a way to automatically parse the typedef struct which datatypes are defined so that a Serial.print can always print the right way?
Only if the struct is defined identically in the sketches of both MCUs. You can do something like this (transfer structs) using this lib - you may be interested in browsing the source code for inspiration.
A 'struct' is very much like a 'class' (object definition) so you could require that one of its members be a function named 'print' that take a Stream to print to:
I believe the information that would be needed to parse a struct won't be available in the compiled program. A struct is just a convenience for the programmer.
The example in Reply #3 does not make the types of the elements available to the running program.
Take a look at the printable class. It doesn't do the automagical parsing that you're looking for but it does allow you to arrange things so that you can use serial.print on your custom objects.