接收和发送Tinyos

时间:2018-05-24 03:21:00

标签: tinyos

我在两个telosb之间有一个程序通信。这是tinyos的一个例子:https://github.com/tinyos/tinyos-release/tree/tinyos-2_1_2/apps/RadioCountToLeds。此代码将发送和接收值计数,如果条件满足,它将打开相应的led。我想将代码分为两部分:发送和接收。我创建了2个应用程序: 接收:

//RadioCountToLedC.nc


#include "Timer.h"
#include "RadioCountToLeds.h"

module RadioCountToLedsC @safe() {
  uses {
    interface Leds;
    interface Boot;
    interface AMSend;
    interface Timer<TMilli> as MilliTimer;
    interface SplitControl as AMControl;
    interface Packet;
  }
}
implementation {

  message_t packet;

  bool locked;
  uint16_t counter = 0;

  event void Boot.booted() {
    call MilliTimer.startPeriodic(250);
    call AMControl.start();
  }

  event void AMControl.startDone(error_t err) {
    if (err == SUCCESS) {
      call MilliTimer.startPeriodic(250);
    }
    else {
      call AMControl.start();
    }
  }

  event void AMControl.stopDone(error_t err) {
    // do nothing
  }

  event void MilliTimer.fired() {
    if(counter<100){
       counter++;
    }else{
       counter=0;
    }
    dbg("RadioCountToLedsC", "RadioCountToLedsC: timer fired, counter is %hu.\n", counter);
    if (locked) {
      return;
    }
    else {
      radio_count_msg_t* rcm = (radio_count_msg_t*)call Packet.getPayload(&packet, sizeof(radio_count_msg_t));
      if (rcm == NULL) {
    return;
      }

      rcm->counter = counter;
      if (call AMSend.send(AM_BROADCAST_ADDR, &packet, sizeof(radio_count_msg_t)) == SUCCESS) {
    dbg("RadioCountToLedsC", "RadioCountToLedsC: packet sent.\n", counter); 
    locked = TRUE;
      }
    }
  }

  event void AMSend.sendDone(message_t* bufPtr, error_t error) {
    if (&packet == bufPtr) {
      locked = FALSE;
    }
  }

}
//RadioToLedAppC.nc

    #include "RadioCountToLeds.h"

    configuration RadioCountToLedsAppC {}
    implementation {
      components MainC, RadioCountToLedsC as App, LedsC;
      components new AMSenderC(AM_RADIO_COUNT_MSG);
      components new AMReceiverC(AM_RADIO_COUNT_MSG);
      components new TimerMilliC();
      components ActiveMessageC;

      App.Boot -> MainC.Boot;

      App.AMSend -> AMSenderC;
      App.AMControl -> ActiveMessageC;
      App.Leds -> LedsC;
      App.MilliTimer -> TimerMilliC;
      App.Packet -> AMSenderC;
    }

发送:

#ifndef RADIO_COUNT_TO_LEDS_H
#define RADIO_COUNT_TO_LEDS_H

typedef nx_struct radio_count_msg {
  nx_uint16_t counter;
} radio_count_msg_t;

enum {
  AM_RADIO_COUNT_MSG = 6,
};

#endif

RadioCountToLed.h

    import java.util.*;
    import java.util.Scanner;
    public class BirthdayReminder
    {
        public static void main(String[] args)
        {
            Scanner input = new Scanner(System.in);
            String birth[] = new String[10];
            String names[] = new String[10];
            String a[] = new String[100];

            for(int i=0; i<10; i++)
            {
                System.out.println("Enter name");
                names[i]= input.nextLine();
                if(names[i].equals("ZZZ"))
                {
                    System.out.println(i);
                    System.out.println(Arrays.toString(names));
                    break;
                }
                else if(i==10)
                {
                    System.out.println(i+1);
                    System.out.println(Arrays.toString(names));
                    for(int j=0;j>0;j++)

                        break;


                }
                else
                {
                    System.out.println("Enter Birthday");
                    birth[i]= input.nextLine();
                    System.out.println(names[i]);
                }

            }



            for(int j=0;j<10;j++){
                System.out.println("Enter name to get their Birthday");
                input = new Scanner(System.in);
                a[j]= input.nextLine();
                boolean valid = false;
                for(int k=0; k<10;k++){
                    if(a[j].equals(names[k])){
                        System.out.println(birth[k]);
                        valid = true;
                    }else if (k == 9 && !valid){
                        System.out.println("Name not found");
                    }

                }

            }

        }
    }

但是当我跑步它不起作用。如果我在同一个文件RadioCountToLedC.nc中写入Receive和Send,如上例所示。它会运作良好。我的申请有什么问题?

2 个答案:

答案 0 :(得分:0)

我认为你需要

call AMControl.start();

在Receiver的启动功能中。这将打开收音机并允许您接收信息。

PS如果这不能完全解决您的问题,请提供有关您在跑步时发生的事情的详细信息。

答案 1 :(得分:0)

有许多简单的方法可以解决此问题,最简单的方法是仅具有一个控制程序流的变量,或者使用每个节点TinyOS ID来控制流。

例如,您可以(仅进行了4个小更改):

#include "Timer.h"
#include "RadioCountToLeds.h"

/**
 * Implementation of the RadioCountToLeds application. RadioCountToLeds 
 * maintains a 4Hz counter, broadcasting its value in an AM packet 
 * every time it gets updated. A RadioCountToLeds node that hears a counter 
 * displays the bottom three bits on its LEDs. This application is a useful 
 * test to show that basic AM communication and timers work.
 *
 * @author Philip Levis
 * @date   June 6 2005
 */

module RadioCountToLedsC @safe() {
  uses {
    interface Leds;
    interface Boot;
    interface Receive;
    interface AMSend;
    interface Timer<TMilli> as MilliTimer;
    interface SplitControl as AMControl;
    interface Packet;
  }
}
implementation {

  message_t packet;

  bool locked;
  uint16_t counter = 0;

  enum state {TX, RX} mode; //Change 1: Define States

  event void Boot.booted() {
    call AMControl.start();
    mode = RX; // CHANGE 2: Set States - "RX" for receiver & "TX" for transmitter
  }

  event void AMControl.startDone(error_t err) {
    if (err == SUCCESS) {
      if(mode == TX) //CHANGE 3: Only the transmitter sends
          call MilliTimer.startPeriodic(250);
    }
    else {
      call AMControl.start();
    }
  }

  event void AMControl.stopDone(error_t err) {
    // do nothing
  }

  event void MilliTimer.fired() {
    counter++;
    dbg("RadioCountToLedsC", "RadioCountToLedsC: timer fired, counter is %hu.\n", counter);
    if (locked) {
      return;
    }
    else {
      radio_count_msg_t* rcm = (radio_count_msg_t*)call Packet.getPayload(&packet, sizeof(radio_count_msg_t));
      if (rcm == NULL) {
    return;
      }

      rcm->counter = counter;
      if (call AMSend.send(AM_BROADCAST_ADDR, &packet, sizeof(radio_count_msg_t)) == SUCCESS) {
    dbg("RadioCountToLedsC", "RadioCountToLedsC: packet sent.\n", counter); 
    locked = TRUE;
      }
    }
  }

  event message_t* Receive.receive(message_t* bufPtr, 
                   void* payload, uint8_t len) {
    dbg("RadioCountToLedsC", "Received packet of length %hhu.\n", len);

    if(mode == TX) return bufPtr; //CHANGE 4: Only RX processes packets

    if (len != sizeof(radio_count_msg_t)) {return bufPtr;}
    else {
      radio_count_msg_t* rcm = (radio_count_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;
    }
  }

  event void AMSend.sendDone(message_t* bufPtr, error_t error) {
    if (&packet == bufPtr) {
      locked = FALSE;
    }
  }

}

如果您愿意,也可以使用C预处理程序来设置mode变量,而不是在Booted事件中设置模式。

您创建的接收器应用程序的问题是它

i)因为在AMControl.start()事件中没有调用Boot.Booted(),所以不启动收发器。

ii)RadioCountToLedsAppC中的接线不正确,因为未连接任何接口。您需要连接所有接口(引导,指示灯,接收和AMControl)。类似于您在发送者中连接它们的方式。