can any kind of typedef struct be parsed automatically which datatypes are in?

Hi everybody,

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?

best regards Stefan

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:

struct foo {
   int one;
   float two;
   void print(Stream s)
    {
    s.println(one);
    s.println(two);
    }
};

Then you can use:

  foo structure;
  structure.print(Serial);

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.

...R

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.

Yes, have your struct (aka class) inherit from the 'Printable' class. Then, implement the virtual 'printTo()' method.

Any object that inherits from the Print class knows how to print an object that inherits from the Printable class.

See: Printable classes - Programming Questions - Arduino Forum