在nesC中可以命令AMSend.send和事件Receive.receive同时工作吗?

时间:2018-01-08 07:49:59

标签: c tinyos nesc

虽然我使用了接口PacketAcknowledgements,但我仍然发现我不能保证一跳之间的通信...在我的实验中,我只使用两个节点,每1秒钟每个节点向另一节点发送一个数据包,然后我发现命令AMsend.send总是被成功调用但事件Receive.receive 总是成功发出信号......它们是否相互冲突?如果命令发送可以抢占事件接收,我该怎么做才能处理它?非常感谢你...

文件RadioCountToLedsC.nc如下:

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

  message_t  packet;

  bool locked = FALSE;
  uint16_t counter = 0;

  event void Boot.booted() {
    dbg("Boot","Application booted.\n");
    call AMControl.start();
  }
task void senddatatask()
{
      counter++;
    if (locked==TRUE) {
      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;
        call PacketAcknowledgements.requestAck(&packet);
        if (call AMSend.send(AM_BROADCAST_ADDR, &packet, sizeof(radio_count_msg_t)) == SUCCESS) {

    locked = TRUE;
        }
      }
}
  event void AMControl.startDone(error_t err) {
    if (err == SUCCESS) {
            call MilliTimer.startPeriodic(1000);

    }
    else {
      call AMControl.start();
    }
  }

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

  event void MilliTimer.fired() {
    post senddatatask();
  }

  event message_t* Receive.receive(message_t* bufPtr, 
                   void* payload, uint8_t len) {
    if (len != sizeof(radio_count_msg_t)) {return bufPtr;}
    else {
      radio_count_msg_t* rcm = (radio_count_msg_t*)payload;
        dbg("RadioCountToLedsC", "Received packet of counter %u.\n", rcm->counter);

      return bufPtr;
    }
  }

  event void AMSend.sendDone(message_t* bufPtr, error_t error) {
    if(&packet == bufPtr && call PacketAcknowledgements.wasAcked(bufPtr)==SUCCESS){
        dbg("RadioCountToLedsC", "packet %u sent.\n", counter); 
        locked = FALSE;
    }
    else
    {
        dbg("RadioCountToLedsC", "NOT ACKED!\n");
    }
    //locked = FALSE;
  }

}

0 个答案:

没有答案