新手入门
- 积分
- 6
- 金钱
- 6
- 注册时间
- 2024-2-23
- 在线时间
- 2 小时
|
2金钱
正点原子lwip例程中带操作系统的UDP,我只想实现发送的功能。但其中udpdemo.c中有一个阻塞的接收函数netconn_recv(udpconn, &recvbuf);
通过调整udpconn->recv_timeout = 1;可以调整等待时间长短,最低为1ms。
我删除了接收的部分也就是netconn_recv(udpconn, &recvbuf);这句话以后就不能正常发送,请问如何才能实现只发送呢?
static void udp_thread(void *arg)
{
OS_CPU_SR cpu_sr;
err_t err;
static struct netconn *udpconn;
static struct netbuf *recvbuf;
static struct netbuf *sentbuf;
struct ip_addr destipaddr;
u32 data_len = 0;
struct pbuf *q;
LWIP_UNUSED_ARG(arg);
udpconn = netconn_new(NETCONN_UDP); //创建一个UDP连接
udpconn->recv_timeout = 1;
if (udpconn != NULL) //创建UDP成功
{
err = netconn_bind(udpconn, IP_ADDR_ANY, UDP_DEMO_PORT);
IP4_ADDR(&destipaddr, lwipdev.remoteip[0], lwipdev.remoteip[1], lwipdev.remoteip[2], lwipdev.remoteip[3]); //构造目的IP地址
netconn_connect(udpconn, &destipaddr, UDP_DEMO_PORT); //连接远端主机
if (err == ERR_OK) //绑定完成
{
while (1)
{
if ((udp_flag & LWIP_SEND_DATA) == LWIP_SEND_DATA) //有数据要发送
{
sentbuf = netbuf_new();
//printf("%s\r\n",adcSendBuf);
udp_demo_sendbuf=(char *)adcSendBuf;//读取数据
netbuf_alloc(sentbuf, strlen((char *)udp_demo_sendbuf));
memcpy(sentbuf->p->payload, (void *)udp_demo_sendbuf, strlen((char *)udp_demo_sendbuf));
err = netconn_send(udpconn, sentbuf); //将netbuf的数据发送出去
if (err != ERR_OK)
{
printf("发送失败....\r\n");
netbuf_delete(sentbuf); //删除buf
}
udp_flag &= ~LWIP_SEND_DATA; //清除数据发送标志
netbuf_delete(sentbuf); //删除buf
}
netconn_recv(udpconn, &recvbuf);
if (recvbuf != NULL)
{
OS_ENTER_CRITICAL(); //??????
memset(udp_demo_recvbuf, 0, UDP_DEMO_RX_BUFSIZE);
for (q = recvbuf->p; q != NULL; q = q->next)
{
if (q->len > (UDP_DEMO_RX_BUFSIZE - data_len)) memcpy(udp_demo_recvbuf + data_len, q->payload, (UDP_DEMO_RX_BUFSIZE - data_len)); //????????
else memcpy(udp_demo_recvbuf + data_len, q->payload, q->len);
data_len += q->len;
if (data_len > UDP_DEMO_RX_BUFSIZE) break;
}
OS_EXIT_CRITICAL(); //??????
data_len = 0; //????????data_len?????
printf("%s\r\n", udp_demo_recvbuf);
netbuf_delete(recvbuf); //???buf
}
}
}
else printf("UDP绑定失败\r\n");
}
else printf("UDP连接创建失败\r\n");
}
|
|