arduino passing struct as argument to function

Hi,

I'm using an Arduino library for a radio.

https://www.cooking-hacks.com/documentation/tutorials/extreme-range-lora-sx1272-module-shield-arduino-raspberry-pi-intel-galileo#step3_2

If you do crtl+f and for "Transmission without ACK" you will see example code.

There is a function called sendPacketTimeout(). This function takes a destination and payload packet of type char, for example like this.

char message1 [] = "Hello";

void setup()
{

}
void loop(void)
{
e = sx1272.sendPacketTimeout(8, message1);
}

I've copied the sendPacketTimeout() function declaration from the header file below.

Questions

  1. Can you only pass a char to the function?
  2. Could I create a struct with information and pass that to the function for example like this.
struct messagePacket {
char boardID[3];       
char temperature[3];
char humidity[3];
};

struct messagePacket myPacket;

void setup() {
struct messagePacket myPacket;
Serial.begin(9600);
strcpy(myPacket.boardID, "01");
strcpy(myPacket.temperature, "75");
strcpy(myPacket.humidity, "90"); 
e = sx1272.sendPacketTimeout(8, myPacket;

This the function declaration from the library

uint8_t sendPacketTimeout(uint8_t dest, char *payload);

//! It sends the packet wich payload is a parameter before ending MAX_TIMEOUT.
/*!
\param uint8_t dest : packet destination.
\param uint8_t *payload: packet payload.
\param uint16_t length : payload buffer length.
\return '0' on success, '1' otherwise
*/
uint8_t sendPacketTimeout(uint8_t dest, uint8_t *payload, uint16_t length);

//! It sends the packet wich payload is a parameter before ending 'wait' time.
/*!
\param uint8_t dest : packet destination.
\param char *payload : packet payload.
\param uint16_t wait : time to wait.
\return '0' on success, '1' otherwise
*/

Don't see why not, but I'd be tempted to use the other prototype:

uint8_t sendPacketTimeout(uint8_t dest, uint8_t *payload, uint16_t length);

And for sending:

sx1272.sendPacketTimeout(8, (uint8_t*)(&myPacket), sizeof(messagePacket));

Thank you

Hi Tammy,

Would you mind explaining you explaining what these mean please ? - uint8_t *payload, uint16_t length.

How did you know the syntax for sending? - (uint8_t*)(&myPacket), sizeof(messagePacket).

Thanks

What the function expects came from your post of the function declaration:

uint8_t sendPacketTimeout(uint8_t dest, uint8_t *payload, uint16_t length);

So the dest variable you know about.

The payload is your struct. So we get the address of your struct using &, but this is then a messagePacket*, so we cast to a char pointer by putting (char*) in front. (pointer is just a variable containing the address of where your struct lives in memory).

As for the length, well with just a pointer the function has no idea of the size of your struct/payload. so the sizeof() function returns the size in bytes of what you give it. So in your case, sizeof(messagePacket) will be the size of your struct in bytes (which should be 12 looking at the definition)