• Tinyos学习笔记(一)


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

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

     

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

    #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;
        }
    }

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

    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;
    
    }

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

    #ifndef SEND_H
    #define SEND_H
    
    typedef nx_struct MsgToRadio
    {
        nx_uint8_t nodeid;
        nx_uint8_t dataS;
    }MsgToRadio;
    
    #endif

    5、接受程序核心代码:

    //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;
    }

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

    附录:

    TinyOS的make命令:

    =======================================================================
    中文名:高洪臣
    英文名:Gordon Scott
    E-mail:gaohongchen01@163.com
    =======================================================================
  • 相关阅读:
    python常见异常
    python+selenium动态抓取网页数据
    python基于scrapy配置日志
    Python依赖
    nginx配置详解
    Centos 用户登录失败N次后锁定用户禁止登陆
    CENTOS 7 firewalld详解,添加删除策略
    Centos7搭建Zookeeper 3.4.14集群
    Centos7安装FastDFS整合nginx
    VMware VCSA 6.7配置vSAN存储
  • 原文地址:https://www.cnblogs.com/gaohongchen01/p/3726192.html
Copyright © 2020-2023  润新知