Pages: [1]   Go Down
Author Topic: XBee.api: easy way to send a variable size of payload?  (Read 785 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 32
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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:
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:
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.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 551
Posts: 46240
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 32
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Dear Pauls,

I've tried this:

Code:
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().

Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 551
Posts: 46240
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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;
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 32
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 32
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I did like this:

Code:
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:
outPacketSend(outPacket, 4);
« Last Edit: February 06, 2013, 02:27:24 pm by syrinx » Logged

Pages: [1]   Go Up
Jump to: