C 尝试使用线程和计时器使用UDP发送两条不同的消息失败
我想从服务器向客户端发送两种不同类型的消息。我为这两条不同的消息分别创建了两个函数C 尝试使用线程和计时器使用UDP发送两条不同的消息失败,c,udp,contiki,contiki-process,C,Udp,Contiki,Contiki Process,我想从服务器向客户端发送两种不同类型的消息。我为这两条不同的消息分别创建了两个函数data\u signal()和probe\u signal() data\u signal()函数将定期向客户端发送间隔为CLOCK\u SECOND*1的定义消息 我使用间隔时钟秒*30持续时间计时器定期调用探测信号()向客户端发送不同的消息 目前,data\u signal()功能按预期工作。但是,probe\u signal()函数没有向客户端发送消息 我通过在客户端打印消息验证了这一点,但没有收到“pro
data\u signal()
和probe\u signal()
data\u signal()
函数将定期向客户端发送间隔为CLOCK\u SECOND*1
的定义消息
我使用间隔时钟秒*30
持续时间计时器定期调用探测信号()
向客户端发送不同的消息
目前,data\u signal()
功能按预期工作。但是,probe\u signal()
函数没有向客户端发送消息
我通过在客户端打印消息验证了这一点,但没有收到“probe_signal()”消息
此外,如果我注释uip_udp_packet_发送(conn,buf,strlen(buf))代码>数据信号行()
然后我可以从探测信号()接收消息。我认为发生此问题是因为我使用相同的UDP连接
代码:
#包括“contiki.h”
#包括“contiki lib.h”
#包括“contiki net.h”
#定义调试\u FULL
#包括“net/ip/uip debug.h”
#包括“net/ip/uiplib.h”
#包括“net/ipv6/uip-icmp6.h”
#包括
#定义服务器IP“fe80::9a07:2dff:fe3c:8d01”//”:“
#定义客户端端口61617
#定义服务器端口61616
#定义PING\u超时(时钟秒/4)
#定义客户端发送间隔(时钟秒*1)
#定义UDP_LEN_MAX 255
/*---------------------------------------------------------------------------*/
静态uip_ipaddr_t server_addr;
静态结构uip_icmp6_echo_reply_通知icmp_通知;
接收到静态uint8回波;
静态结构uip_udp_conn*conn;
静态结构定时器;
静态字符buf[UDP_LEN_MAX];
静态uint16数据包计数器;
静态uint16探测包计数器;
静态uint16实际数据包计数器;
静态int标志;
/*---------------------------------------------------------------------------*/
进程(ipv6_ble_客户端进程,“ipv6 over ble-客户端进程”);
AUTOSTART_进程(和ipv6_ble_客户端_进程);
/*---------------------------------------------------------------------------*/
无效icmp_应答_处理程序(uip_ipaddr_t*源,uint8_t ttl,
uint8_t*数据,uint16_t数据长度)
{
PRINTF(“收到回音响应\n”);
接收到的回波=1;
}
/*---------------------------------------------------------------------------*/
静态void tcpip_处理程序(void)
{
}
/*---------------------------------------------------------------------------*/
静态空隙
数据_信号(无效)
{
sprintf(buf,“当前数据包计数为:%04u!”,数据包计数器);
PRINTF(“发送消息:\n”,buf);/*此PRINTF是为理解低级代码而注释的*/
uip_udp_数据包_发送(conn、buf、strlen(buf));
数据包计数器++;
}
静态空隙探头信号(空隙)
{
sprintf(buf,“%04u”,探测包计数器);
uip_udp_数据包_发送(conn、buf、strlen(buf));
printf(“发送探头信号”);
}
/*---------------------------------------------------------------------------*/
进程线程(ipv6可执行客户端进程、ev、数据)
{
静态结构定时器t;
进程_BEGIN();
PRINTF(“CC26XX-IPv6-over-BLE客户端已启动\n”);
conn=udp\u new(&server\u addr,UIP\u HTONS(server\u PORT),NULL);
udp_绑定(连接、UIP_HTONS(客户端端口));
定时器设置(&t,时钟秒*30);
etimer_设置(&计时器、客户端发送间隔);
而(1)
{
如果(计时器已过期(&t))
{
flag=1;
如果(标志==1)
{
探测信号();
定时器_重置(&t);
printf(“计时器被重置\n”);
}
}
过程_产量();
如果((ev==过程\事件\计时器)&&&(数据==计时器))
{
数据_信号();
etimer_设置(&计时器、客户端发送间隔);
}
否则如果(ev==tcpip\U事件)
{
printf(“发生TCPIP事件\n”);
tcpip_handler();
}
}
进程_END();
}
/*---------------------------------------------------------------------------*/
如果有人能指导如何解决这个问题,那将是一个很大的帮助。我可以通过更改代码来解决这个问题:
- 将计时器类型更改为
etimer
- 等待
etimer
事件,然后调用probe\u signal()
来自事件和日程安排来自:
Contiki NG构建在基于事件的执行模型上,其中
通常在告诉调度程序
它们正在等待事件,因此暂停执行。这样的
事件可以是计时器过期、传入的网络数据包、,
或正在传送的串行线路消息
进程是协同调度的,这意味着每个进程
负责自动将控制权交回操作人员
没有执行太长时间的系统。因此,应用程序
开发人员必须确保将长期运行的操作拆分为
多进程调度,允许在
他们最后停下来的地方
从同一文件的暂停和让步部分:
相反,PROCESS_YIELD()将把控制权交还给调度程序
不希望在不久之后再次安排。相反
它将等待传入事件,类似于
PROCESS_WAIT_EVENT_UNTIL(),但没有必需的条件参数
有关Etimer库的讨论,请访问
Contiki etimer库提供了一种生成
定时事件。事件计时器将发布事件进程\u事件\u计时器
事件计时器过期时设置计时器的进程。这个
etimer库使用时钟
#include "contiki.h"
#include "contiki-lib.h"
#include "contiki-net.h"
#define DEBUG DEBUG_FULL
#include "net/ip/uip-debug.h"
#include "net/ip/uiplib.h"
#include "net/ipv6/uip-icmp6.h"
#include <string.h>
#define SERVER_IP "fe80::9a07:2dff:fe3c:8d01" //"::"
#define CLIENT_PORT 61617
#define SERVER_PORT 61616
#define PING_TIMEOUT (CLOCK_SECOND / 4)
#define CLIENT_SEND_INTERVAL (CLOCK_SECOND * 1)
#define UDP_LEN_MAX 255
/*---------------------------------------------------------------------------*/
static uip_ipaddr_t server_addr;
static struct uip_icmp6_echo_reply_notification icmp_notification;
static uint8_t echo_received;
static struct uip_udp_conn *conn;
static struct etimer timer;
static char buf[UDP_LEN_MAX];
static uint16_t packet_counter;
static uint16_t probe_packet_counter;
static uint16_t actualSent_packet_counter;
static int flag;
/*---------------------------------------------------------------------------*/
PROCESS(ipv6_ble_client_process, "IPv6 over BLE - client process");
AUTOSTART_PROCESSES(&ipv6_ble_client_process);
/*---------------------------------------------------------------------------*/
void icmp_reply_handler(uip_ipaddr_t *source, uint8_t ttl,
uint8_t *data, uint16_t datalen)
{
PRINTF("echo response received\n");
echo_received = 1;
}
/*---------------------------------------------------------------------------*/
static void tcpip_handler(void)
{
}
/*---------------------------------------------------------------------------*/
static void
data_signal(void)
{
sprintf(buf, "Current packet count is: %04u!",packet_counter);
PRINTF("send message: <%s>\n", buf); /*This printf is commented for understanding the low level code*/
uip_udp_packet_send(conn, buf, strlen(buf));
packet_counter++;
}
static void probe_signal(void)
{
sprintf(buf, "%04u", probe_packet_counter);
uip_udp_packet_send(conn, buf, strlen(buf));
printf("Probe signal is sent");
}
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(ipv6_ble_client_process, ev, data)
{
static struct timer t;
PROCESS_BEGIN();
PRINTF("CC26XX-IPv6-over-BLE client started\n");
conn = udp_new(&server_addr, UIP_HTONS(SERVER_PORT), NULL);
udp_bind(conn, UIP_HTONS(CLIENT_PORT));
timer_set(&t, CLOCK_SECOND * 30);
etimer_set(&timer, CLIENT_SEND_INTERVAL);
while(1)
{
if(timer_expired(&t))
{
flag =1;
if(flag==1)
{
probe_signal();
timer_reset(&t);
printf("Timer is reset\n");
}
}
PROCESS_YIELD();
if((ev == PROCESS_EVENT_TIMER) && (data == &timer))
{
data_signal();
etimer_set(&timer, CLIENT_SEND_INTERVAL);
}
else if(ev == tcpip_event)
{
printf("TCPIP event occured\n");
tcpip_handler();
}
}
PROCESS_END();
}
/*---------------------------------------------------------------------------*/
#include "contiki.h"
#include "contiki-lib.h"
#include "contiki-net.h"
#define DEBUG DEBUG_FULL
#include "net/ip/uip-debug.h"
#include "net/ip/uiplib.h"
#include "net/ipv6/uip-icmp6.h"
#include <string.h>
#define SERVER_IP "fe80::9a07:2dff:fe3c:8d01" //"::"
#define CLIENT_PORT 61617
#define SERVER_PORT 61616
#define PING_TIMEOUT (CLOCK_SECOND / 4)
#define CLIENT_SEND_INTERVAL (CLOCK_SECOND * 1)
#define PROBE_SIGNAL_INTERVAL (CLOCK_SECOND * 30)
#define UDP_LEN_MAX 255
/*---------------------------------------------------------------------------*/
static uip_ipaddr_t server_addr;
static struct uip_icmp6_echo_reply_notification icmp_notification;
static uint8_t echo_received;
static struct uip_udp_conn *conn;
static struct etimer timer,probe_timer;
static char buf[UDP_LEN_MAX];
static uint16_t packet_counter;
static uint16_t probe_packet_counter;
static uint16_t actualSent_packet_counter;
static int flag;
/*---------------------------------------------------------------------------*/
PROCESS(ipv6_ble_client_process, "IPv6 over BLE - client process");
AUTOSTART_PROCESSES(&ipv6_ble_client_process);
/*---------------------------------------------------------------------------*/
void icmp_reply_handler(uip_ipaddr_t *source, uint8_t ttl,
uint8_t *data, uint16_t datalen)
{
PRINTF("echo response received\n");
echo_received = 1;
}
/*---------------------------------------------------------------------------*/
static void tcpip_handler(void)
{
}
/*---------------------------------------------------------------------------*/
static void
data_signal(void)
{
sprintf(buf, "Current packet count is: %04u!",packet_counter);
PRINTF("send message: <%s>\n", buf); /*This printf is commented for understanding the low level code*/
uip_udp_packet_send(conn, buf, strlen(buf));
packet_counter++;
}
static void probe_signal(void)
{
sprintf(buf, "%04u", probe_packet_counter);
uip_udp_packet_send(conn, buf, strlen(buf));
printf("Probe signal is sent");
}
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(ipv6_ble_client_process, ev, data)
{
PROCESS_BEGIN();
PRINTF("CC26XX-IPv6-over-BLE client started\n");
conn = udp_new(&server_addr, UIP_HTONS(SERVER_PORT), NULL);
udp_bind(conn, UIP_HTONS(CLIENT_PORT));
etimer_set(&probe_timer, PROBE_SIGNAL_INTERVAL); // set up a timer event for calling probe_signal() function
etimer_set(&timer, CLIENT_SEND_INTERVAL); // set up a timer event for calling data_signal() function
while(1)
{
PROCESS_YIELD(); // yield waiting for an timer event or other event
if((ev == PROCESS_EVENT_TIMER) && (data == &timer))
{
// timer event received so process it and then reschedule again.
data_signal();
etimer_set(&timer, CLIENT_SEND_INTERVAL);
}
if((ev == PROCESS_EVENT_TIMER) && (data == &probe_timer))
{
// timer event received so process it and then reschedule again.
probe_signal();
etimer_set(&timer, PROBE_SIGNAL_INTERVAL);
}
else if(ev == tcpip_event)
{
printf("TCPIP event occured\n");
tcpip_handler();
}
}
PROCESS_END();
}