分类: Windows平台
2013-01-21 10:58:06
经过两周的测试,以下代码可以稳定用于节点的RSSI的收发。以备后用。
发送节点的代码:
includes PongMsg; //includes Timer; module SndPongM { provides interface StdControl; //uses interface StdControl; uses interface Timer; uses interface SendMsg; //uses interface ReceiveMsg; // uses interface ReceiveMsg as PingMsg; uses interface Leds; } implementation { TOS_Msg m_msg; TOS_MsgPtr p_msg; PongMsg_t* pongmsg; command result_t StdControl.init() { pongmsg = (PongMsg_t*)m_msg.data; p_msg = &m_msg; call Leds.init(); return SUCCESS; } command result_t StdControl.start() { call Timer.start(TIMER_REPEAT, 200);//注释掉 return SUCCESS; } command result_t StdControl.stop() { return SUCCESS; } event result_t Timer.fired() { //PongMsg_t* pongmsg=(PongMsg_t*)(p_msg->data); call Leds.redToggle(); //call ReceiveMsg.receive( TOS_MsgPtr msg ); atomic{ pongmsg->src = TOS_LOCAL_ADDRESS; pongmsg->counter=0; pongmsg->dest =TOS_BCAST_ADDR; pongmsg->dest_rssi = 0; pongmsg->dest_lqi = 0; } if(call SendMsg.send(TOS_BCAST_ADDR, sizeof(pongmsg), p_msg)) { call Leds.yellowToggle(); } return SUCCESS; } event result_t SendMsg.sendDone( TOS_MsgPtr msg, result_t success ) { return SUCCESS; } }接收节点接收后,完成计数数据包,并发送给串口:
includes PongMsg; //includes Timer; module RcvPongM { provides interface StdControl; //uses interface StdControl; uses { //interface Timer; interface SendMsg; interface ReceiveMsg; // uses interface ReceiveMsg as PingMsg; interface Leds; } } implementation { TOS_Msg m_msg; TOS_MsgPtr p_msg; PongMsg_t* pongmsg; uint16_t datacounter; command result_t StdControl.init() { pongmsg = (PongMsg_t*)m_msg.data; p_msg=&m_msg; datacounter=0; call Leds.init(); return SUCCESS; } command result_t StdControl.start() { return SUCCESS; } command result_t StdControl.stop() { return SUCCESS; } event result_t SendMsg.sendDone( TOS_MsgPtr msg, result_t success ) { return SUCCESS; } event TOS_MsgPtr ReceiveMsg.receive( TOS_MsgPtr msg ) { PongMsg_t* _pongmsg; _pongmsg = (PongMsg_t*)msg->data; datacounter+=1; atomic{ pongmsg->src = _pongmsg->src; pongmsg->counter = datacounter; pongmsg->dest = TOS_LOCAL_ADDRESS; pongmsg->dest_rssi = msg->strength; pongmsg->dest_lqi = msg->lqi; } if(call SendMsg.send(TOS_UART_ADDR,sizeof(PongMsg_t), p_msg)){ call Leds.yellowToggle(); } return msg; } }当一发一收的时候,接收节点的接收计数器值为那个节点发送的数据包个数。多发一收时,为收到所有数据包的总和。