从 TCP(FreeRTOS、LWIP)接收数据

Receive data from TCP (FreeRTOS, LWIP)

当我启动这个程序时(见下文),一切正常。但是当我禁用 return 数据返回 TPCIP 时,程序运行不正常。

这是程序,工作正常:[1]:http://s27.postimg.org/seixg63hf/problem_2.jpg

static err_t prijata_data(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
{
    void *data; 
    if (err == ERR_OK && p != NULL) {
        // information of receive data
        tcp_recved(pcb, p->tot_len);
        //free buffer
        pbuf_free(p);   
        data = p->payload;  //copy value to send out of queue
        xQueueSend(queue_ethernet, &data, 10);  //send value to queue
        //send data out
        err = tcp_write(pcb, p->payload, p->len, TCP_WRITE_FLAG_COPY);  
        tcp_sent(pcb, NULL); // No need to call back
    } 
    else 
    {
        pbuf_free(p);
    }
    if (err == ERR_OK && p == NULL) {
        close_conn(pcb);
    }
    return ERR_OK;
}

这个函数在这里: s

static err_t prijimani_dat(void *arg, struct tcp_pcb *pcb, err_t err){
    LWIP_UNUSED_ARG(arg);
    LWIP_UNUSED_ARG(err);
    tcp_setprio(pcb, TCP_PRIO_MIN);
    tcp_recv(pcb, prijata_data);
    tcp_err(pcb, server_err); 
    tcp_poll(pcb, server_poll, 4);
    return ERR_OK;
}

在任务 FreeRTOS 中实现了完整的通信:

extern void TCP_connection(void *pvParameters)
{
    UNUSED(pvParameters);
    const portTickType xDelayTime = 5 / portTICK_RATE_MS;

    struct tcp_pcb *tcp_server;
    tcp_server = tcp_new();

    tcp_bind(tcp_server, IP_ADDR_ANY, TCP_PORT);

    while (1){
        tcp_server = tcp_listen(tcp_server);
        tcp_accept(tcp_server, prijimani_dat);

        vTaskDelay(xDelayTime);
    }
}

但是当我只能从 TCP 读取,并且我不想将数据返回到 TCPIP 时,程序运行不正常。: [2]: http://s21.postimg.org/wlhqh31g5/problem_1.jpg

static err_t prijata_data(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err){
    void *data;

    if (err == ERR_OK && p != NULL) {
        /* information of receive data */
        tcp_recved(pcb, p->tot_len);

        //free buffer
        pbuf_free(p); 

        data = p->payload;  /*copy value to send out of queue*/
        xQueueSend(queue_ethernet, &data, 10);  //send value to queue
    } 
    else 
    {
        pbuf_free(p);
    }

    if (err == ERR_OK && p == NULL) {
        close_conn(pcb);
    }
    return ERR_OK;
}

FreeRTOS Interactive site 中有一个非常古老的示例,说明如何将 FreeRTOS 与 lwIP 结合使用。尽管此示例中包含的许多代码现在已被取代,但与 lwIP 的集成应该仍然有效并为您提供参考。

如果您使用的是 FreeRTOS+TCP examples page 中列出的处理器之一(FreeRTOS+TCP 是 FreeRTOS 自己的 TCP/IP 堆栈),那么这可能会提供进一步的参考。

我尽力了。

有什么错误,但是什么?当我尝试 ping 时,一切正常。

C:\>ping 192.168.0.145 -n 10

Pinging 192.168.0.145 with 32 bytes of data:
Reply from 192.168.0.145: bytes=32 time=2ms TTL=255
Reply from 192.168.0.145: bytes=32 time=4ms TTL=255
Reply from 192.168.0.145: bytes=32 time<1ms TTL=255
Reply from 192.168.0.145: bytes=32 time=1ms TTL=255
Reply from 192.168.0.145: bytes=32 time<1ms TTL=255
Reply from 192.168.0.145: bytes=32 time<1ms TTL=255
Reply from 192.168.0.145: bytes=32 time<1ms TTL=255
Reply from 192.168.0.145: bytes=32 time=1ms TTL=255
Reply from 192.168.0.145: bytes=32 time<1ms TTL=255
Reply from 192.168.0.145: bytes=32 time<1ms TTL=255

Ping statistics for 192.168.0.145:
    Packets: Sent = 10, Received = 10, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 0ms, Maximum = 4ms, Average = 0ms

好的。我很蠢。错误在这里:

我创建了 freeRTOS 任务,我在其中每 5 毫秒循环调用两个函数。

这是错误的:

extern void TCP_connection(void *pvParameters)
{
    UNUSED(pvParameters);
    const portTickType xDelayTime = 5 / portTICK_RATE_MS;

    struct tcp_pcb *tcp_server;
    tcp_server = tcp_new();

    if (tcp_bind(tcp_server, IP_ADDR_ANY, TCP_PORT) != ERR_OK)
        return;

    while (1){  
    tcp_server = tcp_listen(tcp_server);
    tcp_accept(tcp_server, server_accept);

        vTaskDelay(xDelayTime);
    }
}

这很好

extern void TCP_connection(void *pvParameters)
{
    UNUSED(pvParameters);
    const portTickType xDelayTime = 5 / portTICK_RATE_MS;

    struct tcp_pcb *tcp_server;
    tcp_server = tcp_new();

    if (tcp_bind(tcp_server, IP_ADDR_ANY, TCP_PORT) != ERR_OK)
        return;

    tcp_server = tcp_listen(tcp_server);
    tcp_accept(tcp_server, server_accept);

    while (1){  
        vTaskDelay(xDelayTime);
    }
}