美文网首页
TinyOS·基本发送

TinyOS·基本发送

作者: JokerAIF | 来源:发表于2016-03-11 23:39 被阅读0次

    之前有玩过一些TinyOS的东西,最近又来开始写一些TinyOS的代码,发现这个玩意儿的细节好多,一不留神连编译都过不去……
    好吧,不扯了,希望以后可以一遍编译过,发送的基本代码见下:

    • 1、头文件,申明要用到的头文件、常量,定义数据包的格式等
    // msg.h
    #ifndef MSG_H
    #define MSG_H
    
    #include <Timer.h> // 时间方法相关的头文件
    #include <printf.h> // 打印输出相关的头文件,可以串口输出
    
    enum {
        AM_CONNECT = 6, //组号为6
        TIMER_PERIOD_MILLI = 100 // 发送时间间隔100ms
    };
    
    typedef nx_struct msgDefind {
        nx_uint16_t ID; // 数据包内容,节点ID
    }msgDefind;
    
    #endif
    
    • 2、配置组件文件,说明提供关系
    // SenderAppC.nc
    #include "msg.h"
    // 
    configuration SenderAppC {}
    //
    implementation {
        components MainC;
        components LedsC;
        components SenderC as App;
        components ActiveMessageC;
        components new TimerMilliC() as Timer;
        components new AMSenderC(AM_CONNECT); 
       
        App.Boot->MainC;
        App.Leds->LedsC;
        App.Timer->Timer;
        // 无线发送
        App.Packet->AMSenderC;
        App.AMSend->AMSenderC;
        App.AMControl->ActiveMessageC;
    }
    
    • 3、模块文件,实现方法及方法的使用
    // SenderC.nc
    #include "msg.h"
    //
    module SenderC {
        uses {
            interface Boot;
            interface Leds;
            interface Timer<TMilli> as Timer;
            interface Packet;
            interface AMSend;
            interface SplitControl as AMControl;
        }
    }
    //
    implementation {
        message_t pkt; // 
        bool busy = FALSE;
        //
        event void Boot.booted() {
            call AMControl.start();
        }
        //
        event void AMControl.startDone(error_t err) {
            if(SUCCESS == err) {
                call Timer.startPeriodic(TIMER_PERIOD_MILLI);
            } else {
                call AMControl.start();
            }
        }
        
        event void AMControl.stopDone(error_t err) {}
        
        event void Timer.fired() {
            if(! busy) {
                // 数据包准备 
                msgDefind* btrpkt = (msgDefind*)(call Packet.getPayload(&pkt, sizeof(msgDefind))); 
                
                if(NULL == btrpkt) {
                    return ;
                }
                
                call Leds.led0Toggle();
                // 使用节点ID填充数据包
                btrpkt->ID = TOS_NODE_ID;
                // 广播发送模式
                if(call AMSend.send(AM_BROADCAST_ADDR, &pkt, sizeof(msgDefind)) == SUCCESS) {
                    busy = TRUE;  // 设置天线状态为 忙
                }
            }
        }
        
        event void AMSend.sendDone(message_t* msg, error_t err) {
            if(&pkt == msg) {
                busy = FALSE; // 设置天线状态为 空闲
            }
        }
    }
    
    • 4、Makefile
    #Makefile
    COMPONENT=SenderAppC
    CFLAGS += -DCC2420_DEF_CHANNEL=14 # 通信信道
    CFLAGS += -DCC2420_DEF_RFPOWER=1 # 工作功率
    CFLAGS += -I$(TOSDIR)/lib/printf # 加入printf 库
    include $(MAKERULES)
    

    基本的发送功能就是这样,主要的方法是:

    call AMSend.send(AM_BROADCAST_ADDR, &pkt, sizeof(msgDefind))
    

    以及发送成功后天线的复位方法:

     event void AMSend.sendDone(message_t* msg, error_t err)
    

    这两个方法是发送功能实现的核心方法。

    相关文章

      网友评论

          本文标题:TinyOS·基本发送

          本文链接:https://www.haomeiwen.com/subject/ktdxlttx.html