[X]关闭

tcp_write()长时间使用出错

文档创建者:江苏-天文
浏览次数:3591
最后更新:2019-07-23
在做以太网传输实验的时候,在汤老师的例程下进行修改,发现使用tcp_write(),tcp_output()函数长时间使用之后,串口就会报错。解决的办法:在使用一次tcp_write和tcp_output之后就对pbuf进行一次清空。算是瞎猫碰死耗子吧。
还有一点就是传输数据的长度不要超出定义的长度,比如一次传输的数据在0-2000之间,那么我传输的数据就是两个字。
以下是主程序:

int main(void)
{

        int Status;
        struct netif *netif, server_netif;
        struct ip_addr ipaddr, netmask, gw;
        err_t err;

        /* the mac address of the board. this should be unique per board */
        unsigned char mac_ethernet_address[] = { 0x00, 0x0a, 0x35, 0x00, 0x01, 0x02 };


        init_intr_sys();
        TcpTmrFlag = 0;

        netif = &server_netif;

        IP4_ADDR(&ipaddr,  192, 168,   1,  10);
        IP4_ADDR(&netmask, 255, 255, 255,  0);
        IP4_ADDR(&gw,      192, 168,   1,  10);

        /*lwip library init*/
        lwip_init();
        /* Add network interface to the netif_list, and set it as default */
        if (!xemac_add(netif, &ipaddr, &netmask, &gw, mac_ethernet_address, XPAR_XEMACPS_0_BASEADDR)) {
                xil_printf("Error adding N/W interface\r\n");
                return -1;
        }
        netif_set_default(netif);

        /* specify that the network if is up */
        netif_set_up(netif);

        /* initialize tcp pcb */
        tcp_send_init();

        Timer_start(&Timer);

        while (1)
        {
                /* call tcp timer every 250ms */
                if(TcpTmrFlag)
                {
                        if(request_pcb->state == CLOSED || (request_pcb->state == SYN_SENT && request_pcb->nrtx == TCP_SYNMAXRTX))
                        {
                                request_pcb = tcp_new();
                                if (!request_pcb) {
                                        xil_printf("txperf: Error creating PCB. Out of Memory\r\n");
                                        return -1;
                                }

                                //ip_set_option(request_pcb, SOF_REUSEADDR);

                                err = tcp_connect(request_pcb, &ipaddress, port, tcp_connected_callback);
                                if (err != ERR_OK) {
                                        xil_printf("txperf: tcp_connect returned error: %d\r\n", err);
                                        return err;
                                }
                        }
                        tcp_tmr();
                        TcpTmrFlag = 0;
                }
                /*receive input packet from emac*/
                xemacif_input(netif);
                /* if connected to the server, start receive data from PL through axidma, then transmit the data to the PC software by TCP*/
                if(tcp_client_connected)
                send_data();
        }
        return 0;

}



void send_data()
{
        struct pbuf *p;

#if __arm__
        int copy = 3;
#else
        int copy = 0;
#endif
        err_t err;
        int Status;
        static int yuan;
        struct tcp_pcb *tpcb = connected_pcb;
        if(num1<2000)
                num1++;
        else
                num1=0;
        err = tcp_write(tpcb, &num1, 2, copy);
                                if (err != ERR_OK) {
                                        xil_printf("txperf: Error on tcp_write: %d\r\n", err);
                                        xil_printf("yuan");
                                        connected_pcb = NULL;
                                        return;
                                }
                                err = tcp_output(tpcb);
                                if (err != ERR_OK) {
                                        xil_printf("txperf: Error on tcp_output: %d\r\n",err);
                                        return;
                                }
                                pbuf_free(p);
}


在send_data()函数的前后定义pbuf结构体指针,最后释放。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则