如何在recv_uc
功能中打印接收的数据?在这种情况下是var的值。我使用packetbuf_copyfrom(&var, 5)
将var
放入已发送的数据包中。
PROCESS(sending_rand, "Sending rand");
AUTOSTART_PROCESSES(&sending_rand);
static void
recv_uc(struct unicast_conn *c, const rimeaddr_t *from)
{
printf("unicast message received from %d.%d\nreceived data: %d\n",
from->u8[0], from->u8[1], /* print data received */);
}
static const struct unicast_callbacks unicast_call = {recv_uc};
static struct unicast_conn unicast;
PROCESS_THREAD(sending_rand, ev, data)
{
static struct etimer et;//oggetto di tipo etimer
int var;
PROCESS_EXITHANDLER(unicast_close(&unicast);)
PROCESS_BEGIN();
unicast_open(&unicast, 129, &unicast_call);
while(1) {
/* Delay 2-4 seconds */
etimer_set(&et, CLOCK_SECOND * 4 + random_rand() % (CLOCK_SECOND * 4));
PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et));
var = random_rand();
packetbuf_copyfrom(&var, 5);
addr.u8[0] = 1;
addr.u8[1] = 0;
if(!rimeaddr_cmp(&addr, &rimeaddr_node_addr)) {
unicast_send(&unicast, &addr);
}
printf("unicast message sent\n");
}
PROCESS_END();
}
答案 0 :(得分:0)
一种方法是使用packetbuf_dataptr()
函数来访问内容。
int var;
memcpy(&var, packetbuf_dataptr(), sizeof(var));
printf("var=%d\n", var);
您的发送代码存在与var
大小相关的错误。它取决于平台,可以是2,4或8个字节。它永远不会是5,所以这是错误的:
packetbuf_copyfrom(&var, 5); // Undefined behavior!
如果尺寸很重要,请始终使用int16_t / uint16_t
或int32_t / uint32_t
!它们是标题中的typedef,有效地包含在每个Contiki文件中。