将官网上的源码片段拼凑成完整的应用实例,并编译和下载到micaz节点中用于测试节点的通讯模块正常工作。

BlinkToRadio.h

 

 
  
  1. #ifndef BLINKTORADIO_H 
  2. #define BLINKTORADIO_H 
  3.  
  4. enum { 
  5.   TIMER_PERIOD_MILLI = 250, 
  6.   AM_BLINKTORADIO = 6 
  7. }; 
  8.  
  9. typedef nx_struct BlinkToRadioMsg { 
  10.   nx_uint16_t nodeid; 
  11.   nx_uint16_t counter; 
  12. } BlinkToRadioMsg; 
  13.  
  14. #endif 

BlinkToRadioAppC.nc

 

 
  
  1. // BlinkToRadioAppC.nc,v 1.0 2013-03-09 
  2. /** 
  3.  * BlinkToRadio is a simple application that increments a counter,displays the  
  4.  * counter's three least significant bits on the three LEDs, and sends a 
  5.  * message with the counter value over the radio. 
  6.  * It has been implemented using a single timer and counter. We have defined a 
  7.  * message format for our application. The we should identify the interfaces 
  8.  * (and components) that provid access to the radio and allow us to manipulate 
  9.  * the message_t type.Finally, use the commands and events provided by the  
  10.  * interfaces to send and receive message. 
  11.  * 
  12.  * @author xjhznick@qq.com 
  13.  **/ 
  14. #include <Timer.h> 
  15. #include "BlinkToRadio.h" 
  16.  
  17. configuration BlinkToRadioAppC { 
  18. implementation { 
  19.   components MainC; 
  20.   components LedsC; 
  21.   components BlinkToRadioC as App; 
  22.   components new TimerMilliC() as Timer0; 
  23.   components ActiveMessageC; 
  24.   components new AMSenderC(AM_BLINKTORADIO); 
  25.   components new AMReceiverC(AM_BLINKTORADIO);   
  26.  
  27.   App.Boot -> MainC; 
  28.   App.Leds -> LedsC; 
  29.   App.Timer0 -> Timer0; 
  30.   App.Packet -> AMSenderC; 
  31.   App.AMSend -> AMSenderC; 
  32.   App.AMPacket -> AMSenderC; 
  33.   App.AMControl -> ActiveMessageC; 
  34.   App.Receive -> AMReceiverC; 

BlinkToRadioAppC.nc

 

 
  
  1. // BlinkToRadioAppC.nc,v 1.0 2013-03-09 
  2.  
  3. /** 
  4.  * Implementation for BlinkToRadio application.  Start to count and broadcast 
  5.  * the  message when a Timer fires. It also receives the message of count value 
  6.  * from the other motes and toggle the LEDs according to the counter's three  
  7.  * least significant bits.Other motes also do so. 
  8.  * 
  9.  **/ 
  10.  
  11. #include <Timer.h> 
  12. #include "BlinkToRadio.h" 
  13.  
  14. module BlinkToRadioC { 
  15.   uses interface Boot; 
  16.   uses interface Leds; 
  17.   uses interface Timer<TMilli> as Timer0; 
  18.   uses interface Packet; 
  19.   uses interface AMPacket; 
  20.   uses interface AMSend; 
  21.   uses interface SplitControl as AMControl; 
  22.   uses interface Receive; 
  23.  
  24. implementation { 
  25.   bool busy = FALSE
  26.   message_t pkt; 
  27.   uint16_t counter = 0; 
  28.    
  29.   event void Boot.booted() { 
  30.     call AMControl.start(); 
  31.   } 
  32.  
  33.   event void AMControl.startDone(error_t err) { 
  34.     if (err == SUCCESS) { 
  35.       call Timer0.startPeriodic(TIMER_PERIOD_MILLI); 
  36.     } 
  37.     else { 
  38.       call AMControl.start(); 
  39.     } 
  40.   }  
  41.    
  42.   event void AMControl.stopDone(error_t err) { 
  43.   } 
  44.  
  45.   event void Timer0.fired() { 
  46.     counter++; 
  47. //    call Leds.set(counter); 
  48.     if (!busy) { 
  49.       BlinkToRadioMsg* btrpkt = (BlinkToRadioMsg*)(call Packet.getPayload(&pkt,sizeof (BlinkToRadioMsg))); 
  50.       btrpkt->nodeid = TOS_NODE_ID; 
  51.       btrpkt->counter = counter; 
  52.       if (call AMSend.send(AM_BROADCAST_ADDR, &pkt, sizeof(BlinkToRadioMsg)) == SUCCESS) { 
  53.         busy = TRUE
  54.       }  
  55.     } 
  56.   } 
  57.  
  58.   event void AMSend.sendDone(message_t* msg, error_t error) { 
  59.     if (&pkt == msg) { 
  60.       busy = FALSE
  61.     } 
  62.   } 
  63.  
  64.   event message_t* Receive.receive(message_t* msg, void* payload, uint8_t len) { 
  65.     if (len == sizeof(BlinkToRadioMsg)) { 
  66.       BlinkToRadioMsg* btrpkt = (BlinkToRadioMsg*)payload; 
  67.       call Leds.set(btrpkt->counter); 
  68.     } 
  69.     return msg; 
  70.   } 
  71.  

Makefile

 

 
  
  1. COMPONENT=BlinkToRadioAppC 
  2. include $(MAKERULES) 

 遇到的问题:

  • 编译中的问题:

make:*** [exe0] error 1 :典型的nesC语法中event必须由user实现,尽管本实例中AMControl.stopDone什么也不做。

warning "*** LOW POWER COMMUNICATIONS DISABLED ***":It is just an informative message that you are not using LPL. You can ignore it and don't need to remove it.

  • 使用vim编辑源码,但是出现系统自带的实例源码可以高亮显示,自己编写的代码却无法高亮显示,则在文件头空出5行即可,一般为注释,即正式的代码从第6行开始编写。
  • 运行时出错:程序编译和下载到节点均正常,但是LED灯不根据要求闪烁,或者只点亮第一个红灯之后不再变化。
 
  
  1. event void AMSend.sendDone(message_t* msg, error_t error) { 
  2.   if (&pkt == msg) { 
  3.     busy = FALSE
  4.   } 

      由于将第三行代码中的赋值号(=)误写为恒等判断号(==),所以产生逻辑错误,这就是编码不小心而产生的可恶的且不容易发现的bug,引以为戒。