This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

[参考译文] RM57L843:TCP/IP 客户端代码不起作用

Guru**** 2540720 points


请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

https://e2e.ti.com/support/microcontrollers/arm-based-microcontrollers-group/arm-based-microcontrollers/f/arm-based-microcontrollers-forum/1028345/rm57l843-tcp-ip-client-code-does-not-work

器件型号:RM57L843

您好!

客户正在尝试修改 TCP/IP 客户端的 HDK lwip 示例代码。
基本代码由以下 git 提供(请参阅原始 E2E 帖子)。
http://git.ti.com/hercules_examples/hercules_examples/trees/master/Application/LwIP

客户修改了以下代码:
Hercules _examples-master\Application\LwIP\v00.04.00\examples\HDK\src\lwip_main.c

修改后的代码如下所示。

err_t connected(void *arg, struct tcp_pcb *newpcb, err_t err) // err is always ERR_OK。
{
	err = tcp_write(newpcb, "ABCDE", 5, 0);
    if (err != ERR_OK)
	{
		return err;
	}

	err = tcp_output(newpcb);
    if (err != ERR_OK)
	{
		return err;
	}

	return ERR_OK;
}

void EMAC_LwIP_Main (uint8_t * macAddress) {
    unsigned int 	ipAddr;
///    uint8_t 		testChar;
///    struct in_addr 	devIPAddress;
///    err_t udp_error;
    err_t tcp_error;
	u16_t send_bufs;
//u8_t senddat[]={ 1,  2,  3,  4};//"ABCDE";
//s	struct pbuf send_p;
	
	//iommUnlock();
	//iommMuxEnableMdio();
	//iommMuxEnableMii();
	//iommLock();
	sciInit();
	
	/* Enable the interrupt generation in CPSR register */
	IntMasterIRQEnable();
	_enable_FIQ();

	sciDisplayText(sciREGx, txtCRLF, sizeof(txtCRLF));
	sciDisplayText(sciREGx, txtTitle, sizeof(txtTitle));
	sciDisplayText(sciREGx, txtCRLF, sizeof(txtCRLF));
	sciDisplayText(sciREGx, txtTI, sizeof(txtTI));
	sciDisplayText(sciREGx, txtCRLF, sizeof(txtCRLF));
#ifdef __little_endian__        
    sciDisplayText(sciREGx, txtLittleEndian, sizeof(txtLittleEndian));
    sciDisplayText(sciREGx, txtCRLF, sizeof(txtCRLF));
#else        
    sciDisplayText(sciREGx, txtBigEndian, sizeof(txtBigEndian));
    sciDisplayText(sciREGx, txtCRLF, sizeof(txtCRLF)); #endif

	/* Initialze the lwIP library, using DHCP.*/
	sciDisplayText(sciREGx, txtEnetInit, sizeof(txtEnetInit));
///	ipAddr = lwIPInit(0, macAddress, 0, 0, 0, IPADDR_USE_DHCP);
	/* Uncomment the following if you'd like to assign a static IP address. Change address as required, and uncomment the previous statement. */
///	/*
///	uint8 ip_addr[4] = {  44,   2, 168, 192 };
///	uint8 netmask[4] = {   0, 255, 255, 255 };
///	uint8 gateway[4] = { 254,   2, 168, 192 };

/*    IP4_ADDR(&ipaddr, 192, 168,   2,  44);
    IP4_ADDR(&netmask, 255, 255, 255,   0);
    IP4_ADDR(&gateway, 192, 168,   2, 254);
LWIP_PLATFORM_BYTE_SWAP
LWIP_PLATFORM_HTONS()
LWIP_PLATFORM_HTONL()*/
    IP4_ADDR(&ipaddr,   44,   2, 168, 192); // my node ip addr
    IP4_ADDR(&netmask,   0, 255, 255, 255);
    IP4_ADDR(&gateway, 254,   2, 168, 192);
	ipAddr = lwIPInit(0, macAddress,
///			*((uint32_t *)ip_addr),
///			*((uint32_t *)netmask),
///			*((uint32_t *)gateway),
			ipaddr.addr,
			netmask.addr,
			gateway.addr,
			IPADDR_USE_STATIC);///*/
	sciDisplayText(sciREGx, (uint8_t*)"..DONE", sizeof("..DONE"));
	sciDisplayText(sciREGx, txtCRLF, sizeof(txtCRLF));
	
	if (0 == ipAddr) {
		sciDisplayText(sciREGx, txtCRLF, sizeof(txtCRLF));
		sciDisplayText(sciREGx, txtCRLF, sizeof(txtCRLF));
        sciDisplayText(sciREGx, txtErrorInit, sizeof(txtErrorInit));
        sciDisplayText(sciREGx, txtCRLF, sizeof(txtCRLF));
        sciDisplayText(sciREGx, txtCRLF, sizeof(txtCRLF));
	} else {
#if 0 ///削除
		/* Convert IP Address to string */
		devIPAddress.s_addr = ipAddr;
		txtIPAddrItoA = (uint8_t *)inet_ntoa(devIPAddress);
		LocatorConfig(macAddress, "HDK enet_lwip");
	
		/* Initialize the sample httpd server. */
		sciDisplayText(sciREGx, (uint8_t*)"Starting Web Server", sizeof("Starting Web Server"));
	    httpd_init();
	    sciDisplayText(sciREGx, (uint8_t*)"..DONE", sizeof("..DONE"));
	    sciDisplayText(sciREGx, txtCRLF, sizeof(txtCRLF));
	    
	     /* Loop forever.  All the work is done in interrupt handlers. */
	    while(1)
	    {    	
	        sciDisplayText(sciREGx, txtCRLF, sizeof(txtCRLF));
	        
	        sciDisplayText(sciREGx, txtTitle, sizeof(txtTitle));
	        sciDisplayText(sciREGx, txtCRLF, sizeof(txtCRLF));
	        
	        sciDisplayText(sciREGx, txtTI, sizeof(txtTI));
	        sciDisplayText(sciREGx, txtCRLF, sizeof(txtCRLF));
#ifdef __little_endian__        
	        sciDisplayText(sciREGx, txtLittleEndian, sizeof(txtLittleEndian));
	        sciDisplayText(sciREGx, txtCRLF, sizeof(txtCRLF));
#else        
	        sciDisplayText(sciREGx, txtBigEndian, sizeof(txtBigEndian));
	        sciDisplayText(sciREGx, txtCRLF, sizeof(txtCRLF)); #endif
			sciDisplayText(sciREGx, txtIPAddrTxt, sizeof(txtIPAddrTxt));
	        sciDisplayText(sciREGx, txtIPAddrItoA, 16);
			sciDisplayText(sciREGx, txtCRLF, sizeof(txtCRLF));
			
			sciDisplayText(sciREGx, txtNote1, sizeof(txtNote1));
			sciDisplayText(sciREGx, txtIPAddrItoA, 16);
			sciDisplayText(sciREGx, txtCRLF, sizeof(txtCRLF));
			
	        
	        /* Before printing the next set, wait for a character on the terminal */
	        sciReceive(sciREGx, 1, &testChar);
	    }
#endif
	    IP4_ADDR(&server,   1,   2, 168, 192); // server ip addr
	    pcbRcv = tcp_new();
	    if (!pcbRcv) {
	        exit(1);
        }
//#if 0 //for client
//	tcp_bind(pcbRcv, IP_ADDR_ANY, rsv_port);
//		tcp_arg(pcbRcv, NULL);
	    tcp_error = tcp_connect(pcbRcv, &server, rsv_port, connected); //connect to the server
	    if (tcp_error != ERR_OK)
	    {
	        exit(1);
	    }
		send_bufs = tcp_sndbuf(pcbRcv);
//		tcp_error = tcp_write(pcbRcv, "ABCDE", 5, 0);
//		tcp_error = tcp_write(pcbRcv, senddat, 2, TCP_WRITE_FLAG_COPY);
/*		tcp_error = tcp_write(pcbRcv, send_p.payload, 2, TCP_WRITE_FLAG_COPY);
	    if (tcp_error != ERR_OK)
	    {
	        exit(1);
	    }

		tcp_error = tcp_output(pcbRcv);
	    if (tcp_error != ERR_OK)
	    {
	        exit(1);
	    }
*/
//#endif

#if 0 //for server
	    tcp_bind(pcbRcv, IP_ADDR_ANY, rsv_port);

		/* we do not need any arguments to callback functions */
//		tcp_arg(pcbRcv, NULL);

		/* listen for connections */
//		pcbRcv = tcp_listen(pcbRcv);
        pcbRcv = tcp_listen_with_backlog(pcbRcv, 2);
		if (!pcbRcv) {
//			xil_printf("Out of memory while tcp_listen\n\r");
//			return -3;
		}

		/* specify callback to use for incoming connections */
		tcp_accept(pcbRcv, accept_callback);
#endif //for server
	    while(1){}
	}
}


客户尝试将设备连接到 PC (服务器)、但无法连接。
代码位于 EMAC_LwIP_Main()末尾的 while (1)循环中。
tcp_connect()返回 ERR_OK。

还有事吗?
示例代码似乎是 http。 是否需要对 TCP/IP 等进行初始化?

顺便说一下、如果将上述代码配置为 TCP/IP 服务器、则其工作正常(注释掉"#if 0 // for server")。

谢谢、此致、
田志郎一郎

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    尊敬的  Koichiro San:

    老实说、我不是以太网的人。 我只读了代码、对我来说看起来很好。 您的服务器的"192.168.2.1"和 rev_port 是否正确? 建立连接后,应调用 call-back 函数 connect()。 如果在 err = tcp_output (newpcb)处添加断点;返回值是多少?

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    您好 QJ、

    [报价 userid="45190" URL"~/support/microcontrollers/arm-based-microcontrollers-group/arm-based-microcontrollers/f/arm-based-microcontrollers-forum/1028345/rm57l843-tcp-ip-client-code-does-not-work/3802742 #3802742"]您的服务器的"192.168.2.1"和 rev_port 是否正确?[/quot]

    是的。 没错。 客户确认 PC (服务器)与 PC (客户端)的连接正常。

    [引用 userid="45190" url="~/support/microcontrollers/arm-based-microcontrollers-group/arm-based-microcontrollers/f/arm-based-microcontrollers-forum/1028345/rm57l843-tcp-ip-client-code-does-not-work/3802742 #3802742">建立连接后,应调用 call-back 函数 connected()。 如果在 err = tcp_output (newpcb)处添加断点、则返回值是多少?

    实际上,未调用 call-back 函数 connected()。 这就是问题所在。
    客户尝试在 connected ()函数的顶部添加断点   
    "err = tcp_write (newpcb、"ABCDE"、5、0);"
    但代码不会在这里停止。 因此无法确认返回值。

    客户还有一些其他问题。
    1) 1)我们不需要特定的初始化即可使用 TCP/IP?
    (无需修改 lwipopts.h?)

    2) 2)我们是否有 lwIP-1.4.1的文档?
    客户已检查以下文件夹下的文档。
    \Hercules 示例主设备\Application\LwIP\v00.04.00\docs
    但需要更详细的信息、例如:
    -每个 lwipopts.h 定义的含义是什么?
    -使用客户端/TCP/IP 需要哪些配置?

    谢谢、此致、
    田志郎一郎

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。
    [引用 userid="97804" URL"~/support/microcontrollers/arm-based-microcontrollers-group/arm-based-microcontrollers/f/arm-based-microcontrollers-forum/1028345/rm57l843-tcp-ip-client-code-does-not-work/3802994 #3802994]1)我们不需要特定的初始化即可使用 TCP/IP?

    在  lwipopts.h 中启用 TCP:

    #define LWIP_TCP             1

    [引用 userid="97804" URL"~/support/microcontrollers/arm-based-microcontrollers-group/arm-based-microcontrollers/f/arm-based-microcontrollers-forum/1028345/rm57l843-tcp-ip-client-code-does-not-work/3802994 #3802994"] 2)我们是否有 lwIP-1.4.1的文档?

    lwipopts.h 允许用户配置堆栈及其所有模块。 用户无需定义所有 LwIP 选项:如果未定义选项、则使用 opt.h 文件中定义的默认值。 因此、lwipopts.h 提供了一种覆盖大部分 lwIP 行为的方法。

    应启用 LWIP_TCP。 您可以使用默认 TCP 设置、例如 tcp_mss

    tcp_mss: TCP 最大段大小

    tcp_snd_BUF: 用于连接的 TCP 发送缓冲区空间。

    MEMP_NUM_tcp_PCB: TCP PCB 结构的总数

    https://lwip.fandom.com/wiki/LwIP_Wiki

    https://lwip.fandom.com/wiki/Tuning_TCP