[solved] nRF24L01+ sending/receiving and struct data is messed up

Hi,

I want to use a struct to exchange data between two µC using nRF24L01+, not sure if this matters. I've defined the same struct on both µC which uses 11 bytes

struct dataStruct{
  uint8_t nrfid;
  uint8_t id;
  uint8_t proto;
  uint16_t val;
  uint16_t sendrequest; 
  uint32_t counter;
}myData;

For testing I initialize as follows

void setup() {
  myData.nrfid =1;
  myData.id    = 2;
  myData.proto = 3;
  myData.val   = 4;
  myData.sendrequest = 5;
  myData.counter     = 6;
...
 }

I receive the following data (for-loop printing each byte of the struct):0x1 0x2 0x3 0x4 0 0x5 0 0x6 0 0 0As you can see "0x4 0", "0x5 0" and "0x6 0 0 0" are myData.val, myData.sendrequest and myData.counter, respectively, but with bytes in wrong order. What am I missing here? Maybe it has something to do with the nRF24L01+ modules' sending routine, maybe it's simply my lack of experience using C?

Thanks & best!

terraduino:
, but with bytes in wrong order.
What am I missing here?

Your expectations are wrong. ATmegas use little-endian (low byte first).

Thanks, that is great news :slight_smile: Ok, ATTiny13A uses little-endian but Raspbian uses little-endian too, as far as my search revealed. I also ran a test that states my Raspbian uses little-endian.
Now I'm lost.
Why is the order still wrong when I use?

     printf("Data [%u", dataReceived.nrfid);
        printf("/%u",dataReceived.id);
        printf("/%u",dataReceived.proto);
        printf("/%" PRIu16 "",dataReceived.val);
        printf(" | %" PRIu16 "",dataReceived.sendrequest);
        printf("/%" PRIu32 "",dataReceived.counter);

Despite that there is another problem. I tried to switch the bytes but I'm selecting the wrong :o

0x1 0x2 0x3 0x4 0xa 0x5 0xb 0x6 0xc 0 0

printf("\n%#x %#x\n", ((dataReceived.val & 0xff)),((dataReceived.val & 0xff00)>>8));

0xa 0x5

Shouldn't dataReceived.val "find" the correct position in the struct itself?

Thanks & best

Maybe there is a packing problem/difference on the raspi side?

A strange question on the Arduino forum.

Thanks for the link, I'll read it next.

Strange question in what regard? Well, sorry, I thought the forum "Programming Questions - Understanding the language, error messages, etc." would be suitable. First time using structs with Arduino IDE.

The data was transmitted correctly, as can be seen in the for-loop output. Only accessing the struct data seems to be a problem.

Best

terraduino:
Strange question in what regard? Well, sorry, I thought the forum "Programming Questions - Understanding the language, error messages, etc." would be suitable. First time using structs with Arduino IDE.

There is no aligning/padding of simple structure members for 8 Bit Arduinos, but it seems to be different on the Raspi.

You could try

struct __attribute__((__packed__)) dataStruct{
  uint8_t nrfid;
  uint8_t id;
  uint8_t proto;
  uint16_t val;
  uint16_t sendrequest;
  uint32_t counter;
}myData;

That should block any padding/alignement.

Thanks again, Whandall!
attribute((packed)) in the receiver code did the trick.
I'm sure I'll find the explanation in the link you provided. Of course I'm curious to understand why the observed behavior occurred and what other mechanisms may cross my way.

Best!