Go Down

Topic: XBee.api: easy way to send a variable size of payload? (Read 987 times) previous topic - next topic

syrinx


Hello,

I use this API with my XBee Serie 1: https://code.google.com/p/xbee-arduino/

I want to optimise the size of the payload sent. According what I send, I need to send only a payload of 10 bytes, or 15 bytes, or ...

I didn't find an easy way to do that. My current code is:

Code: [Select]
Tx16Request txOutLongPacket3 = Tx16Request(packetDestination, outLongPacket, 3); // TX of outLongPacket
Tx16Request txOutLongPacket4 = Tx16Request(packetDestination, outLongPacket, 4); // TX of outLongPacket
Tx16Request txOutLongPacket5 = Tx16Request(packetDestination, outLongPacket, 5); // TX of outLongPacket
Tx16Request txOutLongPacket6 = Tx16Request(packetDestination, outLongPacket, 6); // TX of outLongPacket
Tx16Request txOutLongPacket7 = Tx16Request(packetDestination, outLongPacket, 7); // TX of outLongPacket
Tx16Request txOutLongPacket8 = Tx16Request(packetDestination, outLongPacket, 8); // TX of outLongPacket
Tx16Request txOutLongPacket9 = Tx16Request(packetDestination, outLongPacket, 9); // TX of outLongPacket
Tx16Request txOutLongPacket10 = Tx16Request(packetDestination, outLongPacket, 10); // TX of outLongPacket
Tx16Request txOutLongPacket11 = Tx16Request(packetDestination, outLongPacket, 11); // TX of outLongPacket
Tx16Request txOutLongPacket12 = Tx16Request(packetDestination, outLongPacket, 12); // TX of outLongPacket
Tx16Request txOutLongPacket13 = Tx16Request(packetDestination, outLongPacket, 13); // TX of outLongPacket
Tx16Request txOutLongPacket14 = Tx16Request(packetDestination, outLongPacket, 14); // TX of outLongPacket
Tx16Request txOutLongPacket15 = Tx16Request(packetDestination, outLongPacket, 15); // TX of outLongPacket
Tx16Request txOutLongPacket16 = Tx16Request(packetDestination, outLongPacket, 16); // TX of outLongPacket
Tx16Request txOutLongPacket17 = Tx16Request(packetDestination, outLongPacket, 17); // TX of outLongPacket
Tx16Request txOutLongPacket18 = Tx16Request(packetDestination, outLongPacket, 18); // TX of outLongPacket
Tx16Request txOutLongPacket19 = Tx16Request(packetDestination, outLongPacket, 19); // TX of outLongPacket
Tx16Request txOutLongPacket20 = Tx16Request(packetDestination, outLongPacket, 20); // TX of outLongPacket
Tx16Request txOutLongPacket21 = Tx16Request(packetDestination, outLongPacket, 21); // TX of outLongPacket


then a function according the size will take the good one:
Code: [Select]
boolean outPacketSend(byte size) {
  switch (size) {
case 3:
xbee.send(txOutLongPacket3);
break;
case 8:
xbee.send(txOutLongPacket8);
break;
case 13:
xbee.send(txOutLongPacket13);
break;
case 35:
xbee.send(txOutLongPacket13);
break;
  }
}


Is it possible to do it more easy?

Thanks a lot.

PaulS

Quote
According what I send, I need to send only a payload of 10 bytes, or 15 bytes, or ...

Why not create the Tx16Request instance when you have something to send? Create it using the required size.

Pre-creating a bunch of requests, with the same packet data, and different lengths, doesn't make sense. The request is not bound to the payload array. That array is copied to the request when the request is created.

syrinx

Dear Pauls,

I've tried this:

Code: [Select]

boolean outPacketSend(byte size) {
  switch (size) {
case 8:
Tx16Request txOutPacket = Tx16Request(packetDestination, outPacket, 8); // TX of outPacket
xbee.send(txOutPacket);
break;
case 12:
.....


I got the error "jump to case label".

I think this request should be written only before setup(), not in main().


PaulS

You'll need to post all of your code, and the exact error message.

In general, creating local variables in a case statement can cause issues. The solution, though, is trivial:
   case 8:
       {
      Tx16Request txOutPacket = Tx16Request(packetDestination, outPacket, 8); // TX of outPacket
      xbee.send(txOutPacket);
     }
      break;

syrinx

You are right, it was missing the {}
It's the first time I used them like this in "switch" instruction.

To make a Tx16Request for each size of payload is not nice at all. But it's working.

Thank you.

syrinx

#5
Feb 06, 2013, 08:12 pm Last Edit: Feb 06, 2013, 08:27 pm by syrinx Reason: 1
I did like this:

Code: [Select]

byte outPacket[] = {  // = 8 bytes
 255, 255, 255, 255, 255, 255, 255, 255 };

boolean outPacketSend(byte *packet, byte size) {
 Tx16Request txOutPacket = Tx16Request(packetDestination, packet, size);
 xbee.send(txOutPacket);
}


I call the function:
Code: [Select]
outPacketSend(outPacket, 4);

Go Up