查看: 1827|回复: 0
打印 上一主题 下一主题

TinyOS学习笔记9-简单的转发程序

[复制链接]
跳转到指定楼层
沙发
发表于 2015-3-25 17:11:44 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本转发程序实现的功能是1号节点发送数据,2号节点接收到数据后转发给3号节点
在BlinkToRadio的基础上,修改BlinkToRadioC.nc如下:
#include <Timer.h>
#include "BlinkToRadio.h"

module BlinkToRadioC {
  uses interface Boot;
  uses interface Leds;
  uses interface Timer<TMilli> as Timer0;
  uses interface Packet;
  uses interface AMPacket;
  uses interface AMSend;
  uses interface Receive;
  uses interface SplitControl as AMControl;
}
implementation {

  uint16_t counter;
  message_t pkt;
  bool busy = FALSE;

  event void Boot.booted() {
    call AMControl.start();
  }

  event void AMControl.startDone(error_t err) {
    if (err == SUCCESS) {
      call Timer0.startPeriodic(TIMER_PERIOD_MILLI);
    }
    else {
      call AMControl.start();
    }
  }

  event void AMControl.stopDone(error_t err) {
  }

  event void Timer0.fired() {
    counter++;
    if (TOS_NODE_ID==1&&!busy) {
      BlinkToRadioMsg* btrpkt =
    (BlinkToRadioMsg*)(call Packet.getPayload(&pkt, sizeof(BlinkToRadioMsg)));
      if (btrpkt == NULL) {
    return;
      }
      btrpkt->nodeid = TOS_NODE_ID;
      btrpkt->counter = counter;
      if (call AMSend.send(2,
          &pkt, sizeof(BlinkToRadioMsg)) == SUCCESS) {
        busy = TRUE;
      }
    }
  }

  event void AMSend.sendDone(message_t* msg, error_t err) {
    if (&pkt == msg) {
      busy = FALSE;
    }
  }

  event message_t* Receive.receive(message_t* msg, void* payload, uint8_t len){
    if (len == sizeof(BlinkToRadioMsg)) {
        BlinkToRadioMsg* btrpkt = (BlinkToRadioMsg*)payload;
        call Leds.set(btrpkt->nodeid);
        if (TOS_NODE_ID==2&&btrpkt->nodeid==1&&!busy) {
            BlinkToRadioMsg* temppkt = (BlinkToRadioMsg*)(call Packet.getPayload(&pkt, sizeof(BlinkToRadioMsg)));
            temppkt->nodeid=btrpkt->nodeid;
            temppkt->counter=btrpkt->counter;         
           if (call AMSend.send(3, &pkt, sizeof(BlinkToRadioMsg)) == SUCCESS) {
               busy = TRUE;
           }
        }
    }
    return msg;
  }
}

经过实验,在没有2号节点的情况下,3号节点收不到数据,加入2号节点后,3号节点收到数据。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 加入因仑

本版积分规则

快速回复 返回顶部 返回列表