Contiki,while循环,函数不会被执行

时间:2017-08-17 08:47:49

标签: c contiki

我有以下问题,我的模拟结果被卡在while循环中。在我看来,原因是我们不能在循环结束之前在mote 1.0上执行recv_uc。 但是,mote 1.0本身就是调用

unicast_send(&my_conn, &addr);

更改

value = true;

我需要避免陷入while循环。 这是代码中最重要的部分。

static void my_func(struct unicast_conn *c, const rimeaddr_t *addr){
  value = true;
...
}

static const struct unicast_callbacks my_call = {my_func};
//only mote 1 execute recv_uc
static void recv_uc(struct unicast_conn *c, const rimeaddr_t *from)
{
  my_msg_t mess_rec;
  rimeaddr_t addr;

  addr.u8[0]= from->u8[0];
  addr.u8[1]= from->u8[1];

  memcpy(&mess_rec, packetbuf_dataptr(), sizeof(mess_rec));
  ...
  packetbuf_copyfrom(&mess_rec, 16);
  unicast_send(&my_conn, &addr);
}

static const struct unicast_callbacks unicast_call = {recv_uc};

PROCESS_THREAD(sending_rand, ev, data)
{
...

PROCESS_BEGIN();
unicast_open(&unicast, 120, &unicast_call); //importante
unicast_open(&my_conn, 130, &my_call); //importante
...
addr.u8[0] = 1;
addr.u8[1] = 0;
if(!rimeaddr_cmp(&addr, &rimeaddr_node_addr)){
...
while(value == false){
  packetbuf_copyfrom(&mess, 16);
  unicast_send(&unicast, &addr);
}
}
PROCESS_END();
}

谢谢。

1 个答案:

答案 0 :(得分:0)

Contiki使用协作式多线程,因此如果您的线程没有产生,它将不会被中断,除了实际的硬件中断。由于我与Contiki合作已经有一段时间了,我不能确定网络回调是在中断处理程序中执行还是在主循环中的专用进程中执行。如果后者是真的,这将解释为什么你的进程永远不会离开等待循环。

我建议用以下内容替换while循环:

while(value == false){
  packetbuf_copyfrom(&mess, 16);
  unicast_send(&unicast, &addr);
  PROCESS_YIELD(); // this allows other processes to run
}

设置计时器也可能是一个好主意,具体取决于您要发送的频率。