complex problem - XBee, external libraries and passing structures as arguments


i have weird problem library creating. library used communicate between arduino modules using xbee series 1 modules. library simple wrapper library around arduino xbee library.

at 1 point need pass structure argument 1 of functions in library. function responsible creating response packet , sending it. illustrate problem using simple example of "echo service" library - arduino sends given address content of packet received.

i want pass function processes packet structure data. however, noticed when pass structure, behavior of other (seemingly non related) parts of code different. namely, if pass structure function value, received packet read xbee not correctly. if pass structure pointer, behavior correct. below there simple example illustrates problem.

library:

examplelib.h
code: [select]

#ifndef examplelib_h
#define examplelib_h

#include "arduino.h"
#include <xbee.h>

#define address_broadcast 0xffff
#define address_pc 0x3333

typedef struct
 {
   int vala;
   int valb;
   int valc;
 } valuesstruct;

class examplelib
{
public:
   examplelib();
   void setserial(stream &serial);
   boolean tryreceivepacket();
   void processpacket();
   // function not working!
   void processpacket(valuesstruct valuesstructdata);
   void processpacket(valuesstruct* valuesstructdata);
private:
   xbee xbee;
   rx16response rx16;
};

#endif


examplelib.cpp
as cna see here, have 3 'processpacket' functions differ argument passed - no argument, structure or structure pointer. in case of no argument , structure pointer behavior of function correct, in case of structure behavior not correct , cannot explain - data read " rx16.getdata()" not correct.

code: [select]

#include "arduino.h"
#include <xbee.h>
#include "examplelib.h"

examplelib::examplelib()
{
xbee = xbee();
rx16 = rx16response();
}

void examplelib::setserial(stream &serial)
{
xbee.setserial(serial);
}

boolean examplelib::tryreceivepacket()
{
xbee.readpacket();

 if (xbee.getresponse().isavailable()) {
   // got something

   if (xbee.getresponse().getapiid() == rx_16_response) {
     // got rx packet

     xbee.getresponse().getrx16response(rx16);
     return true;
   }
   else {
     return false;
   }
 }
 else if (xbee.getresponse().iserror()) {
   //nss.print("error reading packet.  error code: ");  
   //nss.println(xbee.getresponse().geterrorcode());
   // or flash error led
   return false;
 }
 
 return false;
}
void examplelib::processpacket()
{
 byte* packetdata = rx16.getdata();
 byte datalength = rx16.getdatalength();
 serial.print("start l:");
 serial.println(datalength);
 for (int = 0; < datalength; i++) {
   serial.print(packetdata[i]);
serial.print(" - ");
 }
 serial.println("end");
 
 //16-bit addressing: enter address of remote xbee, typically coordinator
 tx16request tx = tx16request(address_pc, packetdata, sizeof(packetdata));
 xbee.send(tx);
}

void examplelib::processpacket(valuesstruct valuesstructdata)
{
 processpacket();
}

void examplelib::processpacket(valuesstruct* valuesstructdata)
{
 processpacket();
}


sketch
code: [select]

#include <xbee.h>
#include <examplelib.h>

examplelib examplelibobj = examplelib();

void setup()
{
 serial.begin(9600);
 examplelibobj.setserial(serial);
}

void loop()
{

 boolean ispacketreceived = examplelibobj.tryreceivepacket();

 if (ispacketreceived) {
   // leave 1 section, rest should commented
   
   //section 1: working
   examplelibobj.processpacket();
   
   //section 2: not working
//    valuesstruct test;
//    test.vala = 0;
//    test.valb = 0;
//    test.valc = 0;
//    examplelibobj.processpacket(test);

   //section 3: working
//  valuesstruct* test;
//  test->vala = 0;
//  test->valb = 0;
//  test->valc = 0;
//  examplelibobj.processpacket(test);
 }
}


basically, trigger same function, , behavior differs if use structure argument (although never use or call structure). mysterious behavior me. have idea can problem?

thanks in advance help.
michal

c (and c++) pass value languages. passing struct (a complex data type) not idea. use global instances, pass reference, or pass pointer. not try pass struct instance directly.



Arduino Forum > Using Arduino > Programming Questions > complex problem - XBee, external libraries and passing structures as arguments


arduino

Comments

Popular posts from this blog

opencv3, tbb and rasp pi 2 - Raspberry Pi Forums

small ethernet problem - Raspberry Pi Forums

Multithumb configuration params not working? - Joomla! Forum - community, help and support