Contiki,while循环,函数不执行

Contiki, while loop, functions are not executed

我有以下问题,我的模拟结果卡在了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();
}

谢谢。

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

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

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

设置定时器也是一个好主意,具体取决于您希望发送的频率。