在Iter车辆通信中用于Zigbee的程序?

时间:2013-12-06 05:41:06

标签: zigbee

我正在尝试使用zigbee协议,在VANET中建立两辆车之间的通信,作为车辆间通信的一部分。因此,我对此非常陌生,我想知道:

一个。我是否必须对我的Zigbee界面进行编程以建立通信? 湾是否可以编写zigbee接口? C。谁能让我大致了解如何在两个zigbees之间建立(使用编程语言)通信。?

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:0)

一个。要建立连接并进行简单的发送/接收数据(在2个接口/模块之间),您不需要对ZigBee接口进行编程(例如XBee)。只需使用XCTU配置它们即可。但是,如果你想让它们按照你喜欢的方式相互沟通(用你自己的规则),你必须对它们进行编程。

湾如果你有XBee模块,那么你可以使用基于Java的xbee-api。对于其他模块,请查看模块的文档。

℃。此示例取自xbee-api网页(示例代码),该网页使用Java编程语言。这段代码所做的就是搜索附近的所有模块(节点发现):

/**
 * Copyright (c) 2008 Andrew Rapp. All rights reserved.
 *  
 * This file is part of XBee-API.
 *  
 * XBee-API is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *  
 * XBee-API is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *  
 * You should have received a copy of the GNU General Public License
 * along with XBee-API.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.rapplogic.xbee.examples.zigbee;

import java.util.List;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

import com.rapplogic.xbee.api.ApiId;
import com.rapplogic.xbee.api.AtCommand;
import com.rapplogic.xbee.api.AtCommandResponse;
import com.rapplogic.xbee.api.PacketListener;
import com.rapplogic.xbee.api.XBee;
import com.rapplogic.xbee.api.XBeeException;
import com.rapplogic.xbee.api.XBeeResponse;
import com.rapplogic.xbee.api.zigbee.ZBNodeDiscover;
import com.rapplogic.xbee.util.ByteUtils;

/**
 * Example of performing a node discover for Series 2 XBees.
 * You must connect to the coordinator to run this example and
 * have one or more end device/routers that are associated.
 *
 * @author andrew
 *
 */
public class ZBNodeDiscoverExample {

        private final static Logger log = Logger.getLogger(ZBNodeDiscoverExample.class);

        private XBee xbee = new XBee();

        public ZBNodeDiscoverExample() throws XBeeException, InterruptedException {

                try {
                        // replace with your serial port
                        xbee.open("/dev/tty.usbserial-A6005v5M", 9600);


                        // get the Node discovery timeout
                        xbee.sendAsynchronous(new AtCommand("NT"));
                        AtCommandResponse nodeTimeout = (AtCommandResponse) xbee.getResponse();

                        // default is 6 seconds
                        int nodeDiscoveryTimeout = ByteUtils.convertMultiByteToInt(nodeTimeout.getValue()) * 100;                      
                        log.info("Node discovery timeout is " + nodeDiscoveryTimeout + " milliseconds");

                        log.info("Sending Node Discover command");
                        xbee.sendAsynchronous(new AtCommand("ND"));

                        // NOTE: increase NT if you are not seeing all your nodes reported

                        List<? extends XBeeResponse> responses = xbee.collectResponses(nodeDiscoveryTimeout);

                        log.info("Time is up!  You should have heard back from all nodes by now.  If not make sure all nodes are associated and/or try increasing the node timeout (NT)");

                        for (XBeeResponse response : responses) {
                                if (response instanceof AtCommandResponse) {
                                        AtCommandResponse atResponse = (AtCommandResponse) response;

                                        if (atResponse.getCommand().equals("ND") && atResponse.getValue() != null && atResponse.getValue().length > 0) {
                                                ZBNodeDiscover nd = ZBNodeDiscover.parse((AtCommandResponse)response);
                                                log.info("Node Discover is " + nd);                                                    
                                        }
                                }
                        }
                } finally {
                        xbee.close();
                }
        }

        public static void main(String[] args) throws XBeeException, InterruptedException {
                PropertyConfigurator.configure("log4j.properties");
                new ZBNodeDiscoverExample();
        }
}