资源预览内容
第1页 / 共9页
第2页 / 共9页
第3页 / 共9页
第4页 / 共9页
第5页 / 共9页
第6页 / 共9页
第7页 / 共9页
第8页 / 共9页
第9页 / 共9页
亲,该文档总共9页全部预览完了,如果喜欢就下载吧!
资源描述
浙江工业大学计算机学院实 验 报 告实验名称 无线传感网络实验之丢包率检测日 期 2014 年 12 月 30 日 一、 实验内容本次实验主要是通过代码的编写测试节点的发送功率和距离的远近对接收节点的丢包率的影响。对发送功率的设置是通过修改 CC2420.h 文件中的参数实现的。其中距离的远近的调节是容易实现的。而对丢包率的计算是由接收节点的主机 B 将收到的数据包打印到屏幕上,主机 A 烧写的节点则是实现每次发送 100 个数据包。二、程序源代码主机 A(发送方) BlinkToRadio.h#ifndef BLINKTORADIO_H#define BLINKTORADIO_Henum AM_BLINKTORADIO = 6,TIMER_PERIOD_MILLI = 250;typedef nx_struct BlinkToRadioMsg nx_uint16_t nodeid;nx_uint16_t counter; BlinkToRadioMsg;#endif BlinkToRadioAppC.nc#include #include BlinkToRadio.hconfiguration 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; BlinkToRadioC.nc#include #include BlinkToRadio.hmodule BlinkToRadioC uses interface Boot;uses interface Leds;uses interface Timer 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;void setLeds(uint16_t val) if (val & 0x01)call Leds.led0On();else call Leds.led0Off();if (val & 0x02)call Leds.led1On();elsecall Leds.led1Off();if (val & 0x04)call Leds.led2On();elsecall Leds.led2Off();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+;/发送100个数据包if(counter nodeid = 10;btrpkt-counter = counter;if (call AMSend.send(AM_BROADCAST_ADDR, &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;setLeds(btrpkt-counter);return msg; MakefileCOMPONENT=BlinkToRadioAppCinclude $(MAKERULES)主机 B(接收方) BlinkToRadio.h#ifndef BLINKTORADIO_H#define BLINKTORADIO_Henum AM_BLINKTORADIO = 6,TIMER_PERIOD_MILLI = 250;typedef nx_struct BlinkToRadioMsg nx_uint16_t nodeid;nx_uint16_t counter; BlinkToRadioMsg;#endif BlinkToRadioAppC.nc#include #include BlinkToRadio.hconfiguration 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; BlinkToRadioC.nc#include #include BlinkToRadio.h#include printf.hmodule BlinkToRadioC uses interface Boot;uses interface Leds;uses interface Timer 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;uint32_t nowtime;uint16_t Number=0;event void Boot.booted() call AMControl.start();event void AMControl.startDone(error_t err) if (err = SUCCESS) else call AMControl.start();event void AMControl.stopDone(error_t err) event void Timer0.fired() printf(now is:%dn,(call Timer0.getNow();printfflush();call Leds.led0Toggle();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;if(btrpkt-nodeid=10)/=if (!busy) call Leds.led2Toggle();Number+;printf(No.%d pakage is received,the number:%dn,btrpkt-counter,Number);printfflush();if (call AMSend.send(AM_BROADCAST_ADDR, &pkt, sizeof(BlinkToRadioMsg) = SUCCESS) busy = TRUE;return msg; MakefileCOMPONENT=BlinkToRadioAppCCFLAGS += -I$(TOSDIR)/lib/printfinclude $(MAKERULES)三、实验步骤1.在实验四的代码基础上,修改 BlinkToRadioC.nc 中的事件 Timer0.fired()和事件 Receive.receive 中收到数据包后输出对应的信息。2. 在 cygwin 中进行编译并烧写节点,进行测试。3.开始时,设置发送节点的功率为 3,不断拉大两个节点之间的距离,这时就算把接收节点拿到实验室最远的地方,大概有 10 米的距离还是能做到完全接收到所有发送节点发送的数据(这个截图忘了)。4.将发送节点的功率设为 1,在近距离的时候,接收节点能做到完全接收到所有发送节点发送的数据。当距离在超过 1 米时,开始明显得丢包了。四、实验截图主机 B(接收方):功率为 1,距离 1 米时,接收到了 90 个包,共发送了 100 个包,所以这个时候的丢包率为 90%功率为 1,距离为 3 米左右的时候测得丢包率,这个时候可以看到只接收到了 17个数据包,可以看出丢掉的数据包已经很多了。这个时候的丢包率为:17%五、实验总结本次实验的关键还是在代码编写,在实验四的框架下,还是比较容易实现的。为了测试丢包率,就一定要知道发送方发送的节点数,和对应的接收方接收的节点数。这里我们为了方便计算,就让发送节点按照一秒发送 4 个数据包的速率一次性发送 100 个数据包。而实践证明这样有一个很明
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号