中科因仑“3+1”工程特种兵精英论坛
标题:
TinyOS学习笔记9-简单的转发程序
[打印本页]
作者:
leixiaofeng
时间:
2015-3-25 17:11
标题:
TinyOS学习笔记9-简单的转发程序
本转发程序实现的功能是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号节点收到数据。
欢迎光临 中科因仑“3+1”工程特种兵精英论坛 (http://bbs.enlern.com/)
Powered by Discuz! X3.4