简体   繁体   English

XBee S1通信问题

[英]XBee S1 communication issue

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. 我在API模式1中配置了2个XBee S1模块.XBee模块1是信号发送器,XBee模块2是信号接收器。 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。 XBee Explorer is connected to PC via USB. XBee Explorer通过USB连接到PC。

Following piece of code sends signal each 1 second: 下面的代码每1秒发送一次信号:

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. 使用了Java XBee API库: https//code.google.com/p/xbee-api/我看到TX LED每秒闪烁一次。

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: 接下来使用接收器接线: 在此输入图像描述

Receiver is attached to the breadboard via XBee Explorer. 接收器通过XBee Explorer连接到面包板。 Continuity test shows that current flows fine between XBee RX and mbed P27, as well as between XBee TX and mbed P28. 连续性测试表明,电流在XBee RX和mbed P27之间以及XBee TX和mbed P28之间流动良好。

And following code on an mbed (LPC1768) runs to receive packets: 并且mbed上的代码(LPC1768)运行以接收数据包:

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. 使用了用于XBee API模式的流行Arduino / mbed库。 Sources are located here: http://mbed.org/users/okini3939/code/XBee/ 来源位于: http//mbed.org/users/okini3939/code/XBee/

And the output of a console is: Reading packet... for all times. 控制台的输出是:始终Reading packet... And RX LED is not flashing on receiver. RX LED不会在接收器上闪烁。

The issue was complex (consisted with a few smaller issues). 这个问题很复杂(包括一些较小的问题)。 Dumping request and response frames from both ends as @tomlogic suggested was quite helpful. 正如@tomlogic建议的那样,从两端转储请求和响应帧非常有用。

To resolve these issues I first ensured that hardware part works. 为了解决这些问题,我首先确保硬件部分正常工作。

XBee explorer has 4 LEDs. XBee explorer有4个LED。 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 . 另外3个,取决于您拥有的探险家,可以称为rx, tx, rssidin, dout, rssi

When transmitting code runs, we first need to make sure that rx and tx are blinking simultaneously every second. 当传输代码运行时,我们首先需要确保rxtx每秒同时闪烁。 Ie 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. 当接收代码运行并调用readPacketUntilAvailable ,接收XBee资源管理器的LED应发光如下: rssi不断发光, txdout应闪烁。

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. 当我们设置接收XBee时,我们需要确保做两件事。 First, we call xbee.begin(baudRate) . 首先,我们调用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. 最后,但并非最不重要的是, readPacketUntilAvailable方法不会重置响应。 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). 首先响应的是接收器在我的情况下得到的是MODEM_STATUS_RESPONSE (0x8A十六进制)。 And subsequent packets were not read. 并且未读取后续数据包。 readPacket must be called instead, since that method will reset previous response. 必须调用readPacket ,因为该方法将重置先前的响应。

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. 请注意,因为您在API模式下运行,所以不使用ATDL因此您不需要设置它。

The Java library you linked to is documented as only working with ATAP=2 . 您链接到的Java库记录为仅与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. XBee模块应生成“发送状态”(0x89)帧,以帮助调试。

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). 我认为从Xbee读取数据的最简单方法就是在一个单独的线程中打开串口并不断循环为0x7E(起始分隔符)。 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. 我使用了RxTxSerial库和本教程来启动它: http ://embeddedfreak.wordpress.com/java-serial-port-trail/我没有我的代码重置xbee或不得不等待能够接收数据正确。 If you would like to have a look at my code then I will gladly share and explain it. 如果您想查看我的代码,那么我很乐意分享并解释它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM