2017-08-17 61 views
0

我有以下問題,我的模擬結果被卡在while循環中。在我看來,原因是我們無法在循環結束之前在mote 1.0上執行recv_uc。 然而,就是這個微塵1.0本身調用Contiki,while循環,函數不執行

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(); 
} 

謝謝。

回答

0

Contiki使用協作多線程,所以如果你的線程沒有產生它,除了實際的硬件中斷外,它不會被中斷。由於我與Contiki一起工作了一段時間,我不確定網絡回調是在中斷處理程序中執行還是在主循環中的專用處理中執行。如果後者是真的,這可以解釋爲什麼你的過程永遠不會離開等待循環。

我會建議由以下替換while循環:

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

設置一個計時器也可能是一個好主意,這取決於你希望多久發送。