重写 contiki 源代码“example-mesh.c”以根据计时器值发送消息
Rewrite in the contiki source code “example-mesh.c” to send messages based on a timer value
Contiki "example-mesh.c"中的示例旨在每次按下按钮时发送消息,但我希望此示例每 5 秒定期发送数据,例如而不是按下按钮,所以我需要重写示例"example-mesh.c"以根据计时器值发送消息。这是代码,但我在微尘输出窗口中运行模拟,只发送 3 条消息,其余的为"数据包超时",我该如何解决这个问题:
#include "contiki.h"
#include "net/rime.h"
#include "net/rime/mesh.h"
#include "contiki-conf.h"
#include "sys/etimer.h"
#include "sys/process.h"
#include "sys/ctimer.h"
#include "dev/leds.h"
#include <stdio.h>
#include <string.h>
#define MESSAGE "Hello"
static struct mesh_conn mesh;
/*---------------------------------------------------------------------------*/
PROCESS(example_mesh_process, "Mesh example");
AUTOSTART_PROCESSES(&example_mesh_process);
/*---------------------------------------------------------------------------*/
static void
sent(struct mesh_conn *c)
{
printf("packet sentn");
}
static void
timedout(struct mesh_conn *c)
{
printf("packet timedoutn");
}
static void
recv(struct mesh_conn *c, const rimeaddr_t *from, uint8_t hops)
{
printf("Data received from %d.%d: %.*s (%d)n",
from->u8[0], from->u8[1],
packetbuf_datalen(), (char *)packetbuf_dataptr(), packetbuf_datalen());
packetbuf_copyfrom(MESSAGE, strlen(MESSAGE));
mesh_send(&mesh, from);
}
const static struct mesh_callbacks callbacks = {recv, sent, timedout};
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(example_mesh_process, ev, data)
{
static struct etimer et;
PROCESS_EXITHANDLER(mesh_close(&mesh);)
PROCESS_BEGIN();
mesh_open(&mesh, 132, &callbacks);
etimer_set(&et, CLOCK_SECOND +50);
while(1) {
rimeaddr_t addr;
PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et));
/* Send a message to node number 1. */
packetbuf_copyfrom(MESSAGE, strlen(MESSAGE));
addr.u8[0] = 1;
addr.u8[1] = 0;
mesh_send(&mesh, &addr);
}
PROCESS_END();
}
/*---------------------------------------------------------------------------*/
我知道
这已经有几年的历史了,但这是我的做法。我使用了examples/rime/example-mesh.c程序,并对其进行了修改,以触发计时器过期而不是按钮按下。我把它加载到TelosB(Sky mote)上,它工作了。是的,Contiki 是用 C 而不是 C++ 写的。
#include "contiki.h"
#include "net/rime/rime.h"
#include "net/rime/mesh.h"
#include "sys/etimer.h"
#include "dev/button-sensor.h"
#include "dev/leds.h"
#include <stdio.h>
#include <string.h>
#define MESSAGE "Hello"
static struct mesh_conn mesh;
/*---------------------------------------------------------------------------*/
PROCESS(example_mesh_process, "Mesh example");
AUTOSTART_PROCESSES(&example_mesh_process);
/*---------------------------------------------------------------------------*/
static void
sent(struct mesh_conn *c)
{
printf("packet sentn");
}
static void
timedout(struct mesh_conn *c)
{
printf("packet timedoutn");
}
static void
recv(struct mesh_conn *c, const linkaddr_t *from, uint8_t hops)
{
printf("Data received from %d.%d: %.*s (%d)n",
from->u8[0], from->u8[1],
packetbuf_datalen(), (char *)packetbuf_dataptr(), packetbuf_datalen());
packetbuf_copyfrom(MESSAGE, strlen(MESSAGE));
mesh_send(&mesh, from);
}
const static struct mesh_callbacks callbacks = {recv, sent, timedout};
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(example_mesh_process, ev, data)
{
PROCESS_EXITHANDLER(mesh_close(&mesh);)
PROCESS_BEGIN();
mesh_open(&mesh, 132, &callbacks);
//SENSORS_ACTIVATE(button_sensor);
static struct etimer et;
while(1) {
linkaddr_t addr;
/* Wait for button click before sending the first message. */
//PROCESS_WAIT_EVENT_UNTIL(ev == sensors_event && data == &button_sensor);
etimer_set(&et, CLOCK_SECOND * 5);
PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et));
printf("Timer Expired!nr");
/* Send a message to node number 1. */
packetbuf_copyfrom(MESSAGE, strlen(MESSAGE));
addr.u8[0] = 1;
addr.u8[1] = 0;
mesh_send(&mesh, &addr);
}
PROCESS_END();
}
/*---------------------------------------------------------------------------*/
相关文章:
- Linux的Cpp上的计时器
- 提升 ASIO 无法识别计时器对象
- 提升 asio 并发计时器取消问题与链
- 使用单体计时器的pthread_cond_timedwait有时会比预期晚超时
- 窗口中的微秒计时器
- 计时器是否从另一个线程启动?
- 如何在 c++ 中创建计时器
- C++回调计时器实现
- 在计时器或主线程外部的命令上销毁/替换线程
- 如何制作每秒从 30 乘 1 倒计时的计时器?
- 保留计时器集合(对象与指针)的最佳方法
- 在网络套接字计时器滴答后增加asio短读错误
- 是否可以仅使用标准 c++/c++11 实现不带"sleep"的计时器?
- 在没有NtSetTimerResolution的Windows上提高计时器分辨率(高分辨率)
- 计时器坏了或者其他什么的
- 功能计时器阻止主功能继续
- IO服务重新启动后,Boost最后期限计时器持续触发
- WM_TIMER消息称为,但没有计时器集
- 为什么在MFC中接收错误的消息而不是计时器消息
- 重写 contiki 源代码“example-mesh.c”以根据计时器值发送消息