tinyos学习笔记5--谈谈TestSerial例程的代码

来源:互联网 发布:软件研发工程师待遇 编辑:程序博客网 时间:2024/04/27 23:04

      本篇试着分析理解TestSerial例程。废话上篇已说的不少,这里不在啰嗦,直接进入主题。

1 实验目标

      先看本实验是实现的什么目标,具体如下。大致的意思是说,这个例子用来测试java的上位机程序可以与节点的通过串口进行通信。java的应用程序以1Hz的频率通过串口发数据包到节点,数据包中包含一个不断增长计数的值。节点收到该值,节点通过3个LED闪灯的方式来显示收到数据的低3位。这个例子与RadioCountToLeds例子类似,只是这个例子是通过串口发数据而不是Radio的方式。同时,节点也1Hz的速率通过串口发数据包给上位机,上位机由java程序处理收到的数据,并显示。

Description:
TestSerial is a simple application that may be used to test that the
TinyOS java toolchain can communicate with a mote over the serial
port. The java application sends packets to the serial port at 1Hz:
the packet contains an incrementing counter. When the mote application
receives a counter packet, it displays the bottom three bits on its
LEDs. (This application is similar to RadioCountToLeds, except that it
operates over the serial port.) Likewise, the mote also sends packets
to the serial port at 1Hz. Upon reception of a packet, the java
application prints the counter's value to standard out.

2 配置文件TestSerialAppC.nc

#include "TestSerial.h"configuration TestSerialAppC {}implementation {  components TestSerialC as App, LedsC, MainC;  components SerialActiveMessageC as AM;  components new TimerMilliC();  App.Boot -> MainC.Boot;  App.Control -> AM;  App.Receive -> AM.Receive[AM_TEST_SERIAL_MSG];  App.AMSend -> AM.AMSend[AM_TEST_SERIAL_MSG];  App.Leds -> LedsC;  App.MilliTimer -> TimerMilliC;  App.Packet -> AM;}
      通过这段代码我们知道了,该例程用到了哪些组件及其接口,以及连接关系。特别重要的是SerialActiveMessageC组件。

3 模块组件文件TestSerialC.nc

#include "Timer.h"#include "TestSerial.h"module TestSerialC {  uses {    interface SplitControl as Control;    interface Leds;    interface Boot;    interface Receive;    interface AMSend;    interface Timer<TMilli> as MilliTimer;    interface Packet;  }}
      这里说明了使用到的6个接口,当然配置文件中已交代了连接关系。值得注意的是,要实现接口中的事件函数,即使是什么都不用做的情况,也要写个空函数放于该文件中。

implementation {  message_t packet;  bool locked = FALSE;  uint16_t counter = 0;  
      实现部分定义了3个变量,packet可以自己查一查这个message_t类型是啥;locked用于发送过程中不再启动一次新的发送,直至上一次的发送结束;counter就是在串口中传递的值。

event void Boot.booted() {    call Control.start();  }
      此处无话。

  event void MilliTimer.fired() {    counter++;    if (locked) {      return;    }    else {      test_serial_msg_t* rcm = (test_serial_msg_t*)call Packet.getPayload(&packet, sizeof(test_serial_msg_t));      if (rcm == NULL) {return;}      if (call Packet.maxPayloadLength() < sizeof(test_serial_msg_t)) {return;      }      rcm->counter = counter;      if (call AMSend.send(AM_BROADCAST_ADDR, &packet, sizeof(test_serial_msg_t)) == SUCCESS) {locked = TRUE;      }    }  }
      这是定时时间到的事件函数,同时也是节点通过串口发送数据所处之处。发送数据之前,先有第一个if检查是否已启动了一次串口发数据。第二个if用于检查payload region(载荷区域)是否小于len即这里的sizeof(test_serial_msg_t)。第三个if判断maximum payload length长度是否小于sizeof(test_serial_msg_t)。通过上述三个if的判断则可以通过调用AMSend.send来发送数据。

event message_t* Receive.receive(message_t* bufPtr,    void* payload, uint8_t len) {    if (len != sizeof(test_serial_msg_t)) {return bufPtr;}    else {      test_serial_msg_t* rcm = (test_serial_msg_t*)payload;      if (rcm->counter & 0x1) {call Leds.led0On();      }      else {call Leds.led0Off();      }      if (rcm->counter & 0x2) {call Leds.led1On();      }      else {call Leds.led1Off();      }      if (rcm->counter & 0x4) {call Leds.led2On();      }      else {call Leds.led2Off();      }      return bufPtr;    }  }
      节点收到数据后,进行的处理,就是用3个LED灯来表示收到数据的低三位咯。

event void AMSend.sendDone(message_t* bufPtr, error_t error) {    if (&packet == bufPtr) {      locked = FALSE;    }  }  event void Control.startDone(error_t err) {    if (err == SUCCESS) {      call MilliTimer.startPeriodic(1000);    }  }  event void Control.stopDone(error_t err) {}}
      此处目前还不太理解的是这个Control是个啥意思?:-) 哪位朋友解答一下。在此先感谢了。


By:霜月孤鸟

2015平安夜

1 0
原创粉丝点击