0
votes

I have a trouble with xbee s2b.

I try to receive two joystick values using only xbee placed on sparkfun regulated board (Coordinator API) and process these data on Arduino Uno connected to other xbee s2b placed on explorer board.(Router API). I configured xbees using X-CTU properly and I adjusted DIO0 and DIO1 to ADC[2] on Router Xbee. There is no problem when working with one joystick. But when I try to receive two joystick values at the same time, they are not working correctly. When I look the incoming data on serial monitor, I see;

https://lh3.googleusercontent.com/-20vjr0EchsQ/VqyZXgq84VI/AAAAAAAAA_0/WhEtoOU61vA/s1280-Ic42/Screenshot_3.jpg

My Arduino code is:

int packet[32];

void setup()
{
    Serial.begin(9600);
}

void loop(){
    if (Serial.available() > 0) {
        if (Serial.read() == 0x7E) {
            packet[0] = 0x7E; //start delimiter
            packet[1] = readByte(); //MSB1
            packet[2] = readByte(); //MSB2
            int dataLength = (packet[1] <<  8) | packet[2]; //between the length and the checksum

            printPacket(dataLength+4);
            Serial.println("");
        }
    }
    delay(1000);
}
void printPacket(int k) {
    for(int i=0; i < k; i++) {
        Serial.print(packet, HEX);
        Serial.print(" ");
        delay(1000);
    }
}
int readByte() {
    while (true) {
        if (Serial.available() > 0) {
            return Serial.read();
        }
    }
}

What is the point I missed? Can you help me about this issue. Thank you in advance.

1

1 Answers

1
votes

The delay(1000) statements may be causing you to lose characters from your serial buffer, and might not be necessary.

The code you shared appears incomplete. Where are you reading the dataLength bytes of the packet? How does printPacket() print the bytes?

Your inbound packet buffer should be larger -- I think the XBee S2B can have network payloads of up to 255 characters, in addition to the frame header and checksum.

You've created a blocking readByte() call, which isn't good program design. Consider something like this instead:

unsigned char packet[300];
int packet_index = 0;
int packet_length;

void loop() {
    while (Serial.available() > 0) {
        packet[packet_index++] = Serial.read();
        if (packet_index == 3) {
            packet_length = (packet[1] << 8) | packet[2];
        }
        if (packet_index > 2 && packet_index == packet_length) {
            print_packet();
            packet_index = 0;
        }
    }
}

void print_packet() {
    int i;

    for (i = 0; i < packet_length; ++i) {
        Serial.print(packet[i], HEX);
        Serial.print(" ");
    }
    Serial.println("");
}

If you have too much data to print and you're overflowing your outbound serial buffer, try increasing the speed of the console's serial port to 115200bps. Or print the bytes as you receive them instead of waiting until the packet is complete.