문제

I have 2 XBee S1 modules configured in API mode 1. XBee module 1 is a transmitter of a signal and XBee module 2 is a receiver of a signal. And the issue is that receiver does not receives anything from transmitter (or so it seems).

Transmitter configuration is this:

AP 1
MY 1
ID 1984
DL 2
CH C

Everything else is left by default. Transmitter is attached to XBee Explorer. XBee Explorer is connected to PC via USB.

Following piece of code sends signal each 1 second:

public class Main {

    private Main() {
        XBee xbee = new XBee();
        try {
            xbee.open("/dev/ttyUSB0", 9600);
            final XBeeRequest request = new TxRequest16(new XBeeAddress16(0, 2), new int[] { 1 });

            //noinspection InfiniteLoopStatement
            while (true) {
                xbee.sendSynchronous(request);
                Thread.sleep(1000);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            xbee.close();
        }
    }

    public static void main(String[] args) {
        new Main();
    }
}

Java XBee API library was used: https://code.google.com/p/xbee-api/ And I see TX LED flashing every second.

Receiver configuration is this:

AP 1
MY 2
ID 1984
DL 1
CH C

Everything else is left by default. Following wiring for receiver is used: enter image description here

Receiver is attached to the breadboard via XBee Explorer. Continuity test shows that current flows fine between XBee RX and mbed P27, as well as between XBee TX and mbed P28.

And following code on an mbed (LPC1768) runs to receive packets:

Serial terminal(USBTX, USBRX);

while(1) {
    terminal.puts("Reading packet...\r\n");
    xbee.readPacketUntilAvailable();
    terminal.puts("Packet available\r\n");

    XBeeResponse response = xbee.getResponse();
    if (response.isAvailable()) {
        char tmp[20];
        sprintf(tmp, "0x%02X", response.getApiId());
        terminal.puts("Response available at API: ");
        terminal.puts(tmp);
        terminal.puts("\r\n");
        uint8_t api = response.getApiId();
        if (api == RX_16_RESPONSE) {
            Rx16Response rx16 = Rx16Response();
            response.getRx16Response(rx16);
            uint8_t len = rx16.getDataLength();
            char l[20];
            sprintf(l, "%d", len);

            terminal.puts("We have data: ");
            terminal.puts(l);
            terminal.puts("\r\n");
        }
    }
    wait(1);
}

Popular Arduino/mbed library for an XBee API mode was used. Sources are located here: http://mbed.org/users/okini3939/code/XBee/

And the output of a console is: Reading packet... for all times. And RX LED is not flashing on receiver.

도움이 되었습니까?

해결책

The issue was complex (consisted with a few smaller issues). Dumping request and response frames from both ends as @tomlogic suggested was quite helpful.

To resolve these issues I first ensured that hardware part works.

XBee explorer has 4 LEDs. One is power and should always glow red when powered. And 3 others, depending on what explorer you have, may be called rx, tx, rssi or din, dout, rssi.

When transmitting code runs, we first need to make sure that rx and tx are blinking simultaneously every second. I.e. signal is sent each second.

When receiving code runs and calls readPacketUntilAvailable, LEDs on receiving XBee explorer should glow as follows: rssi glows constantly, tx or dout should blink.

Now that when we know hardware is fine, we can debug software part.

My issue was with receiving code. When we are setting up receiving XBee, we need to make sure we do two things. First, we call xbee.begin(baudRate). And second, we should reset module.

reset = 0;
wait_ms(100);
reset = 1;
wait_ms(100);

And last, but not least thing, readPacketUntilAvailable method will not reset response. We call this function in a loop. Whatever first response we get will be repeated, no matter what other data is sent by transmitter.

First response that receiver got in my case was MODEM_STATUS_RESPONSE (0x8A in HEX). And subsequent packets were not read. readPacket must be called instead, since that method will reset previous response.

My receiving code now looks like this:

XBee xbee(p28, p27);
DigitalOut reset(p11);
DigitalOut mbed_led1(LED1);

int main() {
    Serial terminal(USBTX, USBRX);
    while(!terminal.readable()) {
        wait_ms(10);
    }
    terminal.getc();
    mbed_led1 = 1;
    xbee.begin(9600);
    reset = 0;
    wait_ms(100);
    reset = 1;
    wait_ms(100);

    while(1) {
        terminal.puts("Reading packet...\r\n");
        xbee.readPacket(500);
        XBeeResponse response = xbee.getResponse();
        if (response.isAvailable()) {
            terminal.puts("Packet available\r\n");
            XBeeResponse response = xbee.getResponse();

            char tmp[20];
            sprintf(tmp, "0x%02X", response.getApiId());
            terminal.puts("Response available at API: ");
            terminal.puts(tmp);
            terminal.puts("\r\n");
            uint8_t api = response.getApiId();
            if (api == RX_16_RESPONSE) {
                Rx16Response rx16 = Rx16Response();
                response.getRx16Response(rx16);
                uint8_t len = rx16.getDataLength();
                char l[20];
                sprintf(l, "%d", len);

                terminal.puts("We have data of length ");
                terminal.puts(l);
                terminal.puts("\r\n");
            }
        }

        wait(1);
    }
}

다른 팁

Note that because you're running in API mode, ATDL isn't used so you shouldn't need to set it.

The Java library you linked to is documented as only working with ATAP=2. Try changing that setting on the transmitting node.

You should be dumping any packets that come in on the transmitter. The XBee module should generate a "Transmit Status" (0x89) frame that can aid in debugging.

I think the easiest way to read data from the Xbee is just to open the serial port in a separate thread and constantly loop for 0x7E (the start delimiter). That is what I'm doing with my code and it works fine. I used the RxTxSerial library and this tutorial to get it started: http://embeddedfreak.wordpress.com/java-serial-port-trail/ I don't have my code resetting the xbee or having to wait to be able to receive the data properly. If you would like to have a look at my code then I will gladly share and explain it.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top