参考TinyOS官方网站实现BlinkToRadio
将官网上的源码片段拼凑成完整的应用实例,并编译和下载到micaz节点中用于测试节点的通讯模块正常工作。
BlinkToRadio.h
- #ifndef BLINKTORADIO_H
- #define BLINKTORADIO_H
- enum {
- TIMER_PERIOD_MILLI = 250,
- AM_BLINKTORADIO = 6
- };
- typedef nx_struct BlinkToRadioMsg {
- nx_uint16_t nodeid;
- nx_uint16_t counter;
- } BlinkToRadioMsg;
- #endif
BlinkToRadioAppC.nc
- // BlinkToRadioAppC.nc,v 1.0 2013-03-09
- /**
- * BlinkToRadio is a simple application that increments a counter,displays the
- * counter's three least significant bits on the three LEDs, and sends a
- * message with the counter value over the radio.
- * It has been implemented using a single timer and counter. We have defined a
- * message format for our application. The we should identify the interfaces
- * (and components) that provid access to the radio and allow us to manipulate
- * the message_t type.Finally, use the commands and events provided by the
- * interfaces to send and receive message.
- *
- * @author [email protected]
- **/
- #include <Timer.h>
- #include "BlinkToRadio.h"
- configuration 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.AMSend -> AMSenderC;
- App.AMPacket -> AMSenderC;
- App.AMControl -> ActiveMessageC;
- App.Receive -> AMReceiverC;
- }
BlinkToRadioAppC.nc
- // BlinkToRadioAppC.nc,v 1.0 2013-03-09
- /**
- * Implementation for BlinkToRadio application. Start to count and broadcast
- * the message when a Timer fires. It also receives the message of count value
- * from the other motes and toggle the LEDs according to the counter's three
- * least significant bits.Other motes also do so.
- *
- **/
- #include <Timer.h>
- #include "BlinkToRadio.h"
- module BlinkToRadioC {
- uses interface Boot;
- uses interface Leds;
- uses interface Timer<TMilli> as Timer0;
- uses interface Packet;
- uses interface AMPacket;
- uses interface AMSend;
- uses interface SplitControl as AMControl;
- uses interface Receive;
- }
- implementation {
- bool busy = FALSE;
- message_t pkt;
- uint16_t counter = 0;
- 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++;
- // call Leds.set(counter);
- if (!busy) {
- BlinkToRadioMsg* btrpkt = (BlinkToRadioMsg*)(call Packet.getPayload(&pkt,sizeof (BlinkToRadioMsg)));
- btrpkt->nodeid = TOS_NODE_ID;
- 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 error) {
- 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;
- call Leds.set(btrpkt->counter);
- }
- return msg;
- }
- }
Makefile
- COMPONENT=BlinkToRadioAppC
- 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灯不根据要求闪烁,或者只点亮第一个红灯之后不再变化。
- event void AMSend.sendDone(message_t* msg, error_t error) {
- if (&pkt == msg) {
- busy = FALSE;
- }
- }
由于将第三行代码中的赋值号(=)误写为恒等判断号(==),所以产生逻辑错误,这就是编码不小心而产生的可恶的且不容易发现的bug,引以为戒。
转载于:https://blog.51cto.com/xjhznick/1150873