请选择 进入手机版 | 继续访问电脑版
查看: 1916|回复: 0

TinyOS学习笔记8-简单发送接收数据

[复制链接]
发表于 2015-3-25 17:12:36 | 显示全部楼层 |阅读模式
简单发送接收数据
本节为对BlinkToRadio的分析,从而了解简单的收发过程。
我们读BlinkToRadio修改如下:发送本节点的id给其他的节点
BlinkToRadio.h

#ifndef BLINKTORADIO_H
#define BLINKTORADIO_H

enum {
  AM_BLINKTORADIO = 6,     //AM数据包的类型
  TIMER_PERIOD_MILLI = 250 //发送的时间间隔为250ms
};

//定义发送数据包的负载部分
typedef nx_struct BlinkToRadioMsg {
  nx_uint16_t nodeid;  //本节点的id
  nx_uint16_t counter; //计数
} BlinkToRadioMsg;

#endif

BlinkToRadioC.nc 文件分析

#include <Timer.h>
#include "BlinkToRadio.h"

module BlinkToRadioC {
//定义使用的接口
  uses interface Boot;                   //从Boot开始启动
  uses interface Leds;                   //led灯
  uses interface Timer<TMilli> as Timer0;//时间
  uses interface Packet;                 //数据包,在获取负载处用到
  uses interface AMPacket;               //AM数据包,需要实现SendDone()
  uses interface AMSend;                 //AM发送数据
  uses interface Receive;                //接收数据包
  uses interface SplitControl as AMControl; //Split操作
}
implementation {

  uint16_t counter; //发送的次数
  message_t pkt;    //数据包
  bool busy = FALSE; //节点是否忙,开始为不忙

  //从此处启动,开始程序
  event void Boot.booted() {
    call AMControl.start();  //调用Split操作的开始,是否成功在startDone()处判断
  }

  //判断开始是否完成
  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++;//发送数据个数加1
    if (!busy) {//节点不忙,则开始发送数据
      //设置数据包的负载,btr指向数据包的负载部分
      BlinkToRadioMsg* btrpkt =
    (BlinkToRadioMsg*)(call Packet.getPayload(&pkt, sizeof(BlinkToRadioMsg)));
      if (btrpkt == NULL) {//没有数据就不发送
    return;
      }
      btrpkt->nodeid = TOS_NODE_ID; //发送节点的ID,TOS_NODE_ID为在make telosb install,1时设置的
      btrpkt->counter = counter;    //发送数据包的个数
      //开始发送数据,AMSend.send(广播地址,负载数据,负载数据的大小)
      if (call AMSend.send(AM_BROADCAST_ADDR, //修改AM_BROADCAST_ADDR为2,则节点只向2号节点发,其他节点不响应
          &pkt, sizeof(BlinkToRadioMsg)) == SUCCESS) {
        busy = TRUE;//调用发送数据成功,节点正在发送数据,变为忙的状态,是否发送完成在sendDone里判断
      }
    }
  }

  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); //设置led为获取节点的id
    }
    return msg;  //返回数据
  }
}


BlinkToRadioAppC.nc文件,实现连线

#include <Timer.h>
#include "BlinkToRadio.h"

configuration BlinkToRadioAppC {
}
implementation {
  components MainC;
  components LedsC;
  components BlinkToRadioC as App;
  components new TimerMilliC() as Timer0;
  components ActiveMessageC;
  components new AMSenderC(AM_BLINKTORADIO);
  components new AMReceiverC(AM_BLINKTORADIO);

  App.Boot -> MainC;
  App.Leds -> LedsC;
  App.Timer0 -> Timer0;
  App.Packet -> AMSenderC;
  App.AMPacket -> AMSenderC;
  App.AMControl -> ActiveMessageC;
  App.AMSend -> AMSenderC;
  App.Receive -> AMReceiverC;
}
回复

使用道具 举报

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

本版积分规则

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