在contiki中,我需要有两个文件,发送方和接收方,发送方向接收方发送数据包。我的问题是,接收器没有输出数据包已收到。
我在接收数据包中尝试了一个 while 循环,我什至尝试创建一个函数,但仍然没有任何效果。
我的sender.c文件
#include "contiki.h"
#include "net/rime.h"
#include "random.h"
#include "dev/button-sensor.h"
#include "dev/leds.h"
#include <stdio.h>
PROCESS(sendReceive, "Hello There");
AUTOSTART_PROCESSES(&sendReceive);
PROCESS_THREAD(sendReceive, ev, data)
{
PROCESS_BEGIN();
static struct abc_conn abc;
static struct etimer et;
static const struct abc_callbacks abc_call;
PROCESS_EXITHANDLER(abc_close(&abc);)
abc_open(&abc, 128, &abc_call);
while(1)
{
/* Delay 2-4 seconds */
etimer_set(&et, CLOCK_SECOND * 2 + random_rand() % (CLOCK_SECOND * 2));
PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et));
packetbuf_copyfrom("Hello", 6);
abc_send(&abc);
printf("Message sent\n");
}
PROCESS_END();
}
我的receiver.c文件
#include "contiki.h"
#include "net/rime.h"
#include "random.h"
#include "dev/button-sensor.h"
#include "dev/leds.h"
#include <stdio.h>
PROCESS(sendReceive, "Receiving Message");
AUTOSTART_PROCESSES(&sendReceive);
PROCESS_THREAD(sendReceive, ev, data)
{
PROCESS_BEGIN();
{
printf("Message received '%s'\n", (char *)packetbuf_dataptr());
}
PROCESS_END();
}
sender.c 文件正在工作,它正在正确发送数据包,问题是接收方似乎没有输出它已收到。
最佳答案
虽然发送很简单——你只需要调用一个函数——但在嵌入式系统中接收数据通常要复杂得多。操作系统需要有一种方法让您的代码知道新数据已从外部到达。在 Contiki 中,这是在内部通过事件完成的,并且从用户的角度来看是通过回调完成的。
所以,实现一个回调函数:
static void
recv_from_abc(struct abc_conn *bc)
{
printf("Message received '%s'\n", (char *)packetbuf_dataptr());
}
在你的接收进程中,创建并打开一个连接,将回调函数的指针作为参数传递:
static struct abc_conn c;
static const struct abc_callbacks callbacks =
{recv_from_abc, NULL};
uint16_t channel = 128; /* matching the sender code */
abc_open(&c, channel, &callbacks);
关于c - 我如何向另一个程序发送消息,并输出消息已收到?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54393241/