标签:style blog class code c java
简述:发送和接受数据的程序分别烧录到两个节点上,发送方发送流水灯数据,接受方接受数据并实现流水灯
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
标签:style blog class code c java
原文地址:http://www.cnblogs.com/gaohongchen01/p/3726192.html