码迷,mamicode.com
首页 > 其他好文 > 详细

Tinyos学习笔记(一)

时间:2014-05-16 06:25:16      阅读:435      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   code   c   java   

简述:发送和接受数据的程序分别烧录到两个节点上,发送方发送流水灯数据,接受方接受数据并实现流水灯

1、发送和接受程序用到的组件及其接口如图(通过make telosb docs获得)所示:

bubuko.com,布布扣  bubuko.com,布布扣

2、发送程序sendC.nc代码:

bubuko.com,布布扣
#include "Timer.h"
#include "send.h"

module sendC @safe()
{
    uses{
        interface Boot;
        interface Timer<TMilli> as Timer;

        interface SplitControl as AMControl;
        interface Packet;
        interface AMSend;
    }
}
implementation
{
    uint8_t dataS=1;
    bool busy=FALSE;//if write ‘false‘,`false‘ undeclared here (not in a function)
    message_t pkt;

    void startTimer()//internal function
    {
        call Timer.startPeriodic(500);
    }

    //start radio
    event void Boot.booted()
    {
        call AMControl.start();
    }
    event void AMControl.startDone(error_t err)
    {
        if(SUCCESS==err)
            startTimer();
        else
            call AMControl.start();
    }
    event void AMControl.stopDone(error_t err){}
    
    task void compute()
    {
        if(dataS>=0x04)
            dataS=1;
        else
            dataS=dataS<<1;
    }
    event void Timer.fired()
    {    
        if(!busy)    
        {
            MsgToRadio* trpkt=(MsgToRadio*)(call Packet.getPayload(&pkt,sizeof(MsgToRadio)));
            if(NULL==trpkt)
                return;
            trpkt->nodeid=TOS_NODE_ID;//发送节点的ID,TOS_NODE_ID为在make telosb install,1时设置的
            trpkt->dataS=dataS;
            //AM_BROADCAST_ADDR广播模式,修改AM_BROADCAST_ADDR为2,则节点只向2号节点发,其他节点不响应
            if(call AMSend.send(AM_BROADCAST_ADDR,&pkt,sizeof(MsgToRadio))==SUCCESS)//在if(!busy)里面????
                busy=TRUE;
        }        
        post compute();
    }
    event void AMSend.sendDone(message_t* msg,error_t err)
    {
        if(msg==&pkt)
            busy=FALSE;
    }
}
bubuko.com,布布扣

3、发送程序sendCAppC.nc代码:

bubuko.com,布布扣
configuration sendAppC
{
}
implementation
{
    components MainC,sendC;
    components new TimerMilliC();

    components ActiveMessageC;
    components new AMSenderC(6);

    sendC.Boot->MainC;
    sendC.Timer->TimerMilliC;

    sendC.AMControl->ActiveMessageC;
    sendC.Packet->AMSenderC;
    sendC.AMSend->AMSenderC;

}
bubuko.com,布布扣

4、发送程序send.h代码:

bubuko.com,布布扣
#ifndef SEND_H
#define SEND_H

typedef nx_struct MsgToRadio
{
    nx_uint8_t nodeid;
    nx_uint8_t dataS;
}MsgToRadio;

#endif
bubuko.com,布布扣

5、接受程序核心代码:

bubuko.com,布布扣
//an event-driven process
event message_t* Receive.receive(message_t* msg,void* payload,uint8_t len)
{
    if(len==sizeof(MsgFromRadio))
    {
        MsgFromRadio* frpkt=(MsgFromRadio*)payload;
        call Leds.set(frpkt->dataR);
    }
    return msg;
}
bubuko.com,布布扣

参考网址:http://tinyos.stanford.edu/tinyos-wiki/index.php/Mote-mote_radio_communication

Tinyos学习笔记(一),布布扣,bubuko.com

Tinyos学习笔记(一)

标签:style   blog   class   code   c   java   

原文地址:http://www.cnblogs.com/gaohongchen01/p/3726192.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!