WIZnet Developer Forum

W5500 TCP client get stuck after frequently receiving data from server

Hi,

I am working on W5500. In which, I am running TCP client example code given on wiznet website.

When I am sending data from a server with a 1000-millisecond delay. It is working fine. But when I am sending at a 200-millisecond delay then the code is stuck and in the switch case, it always comes “case SOCK_CLOSED:”.

Below is the code which I have used.

int32_t loopback_tcpc(uint8_t sn, uint8_t* buf, uint8_t* destip, uint16_t destport)
{
int32_t ret; // return value for SOCK_ERRORs
uint16_t size = 0, sentsize=0;

// Destination (TCP Server) IP info (will be connected)
// >> loopback_tcpc() function parameter
// >> Ex)
//	uint8_t destip[4] = 	{192, 168, 0, 214};
//	uint16_t destport = 	5000;

// Port number for TCP client (will be increased)
static uint16_t any_port = 	50000;

// Socket Status Transitions
// Check the W5500 Socket n status register (Sn_SR, The 'Sn_SR' controlled by Sn_CR command or Packet send/recv status)
switch(getSn_SR(sn))
{
case SOCK_ESTABLISHED :
	if(getSn_IR(sn) & Sn_IR_CON)	// Socket n interrupt register mask; TCP CON interrupt = connection with peer is successful
	{

#ifdef LOOPBACK_DEBUG
printf("%d:Connected to - %d.%d.%d.%d : %d\r\n",sn, destip[0], destip[1], destip[2], destip[3], destport);
#endif
setSn_IR(sn, Sn_IR_CON); // this interrupt should be write the bit cleared to ‘1’
}

	//////////////////////////////////////////////////////////////////////////////////////////////
	// Data Transaction Parts; Handle the [data receive and send] process
	//////////////////////////////////////////////////////////////////////////////////////////////
	if((size = getSn_RX_RSR(sn)) > 0) // Sn_RX_RSR: Socket n Received Size Register, Receiving data length
	{
		if(size > DATA_BUF_SIZE) size = DATA_BUF_SIZE; // DATA_BUF_SIZE means user defined buffer size (array)
		ret = recv(sn, buf, size); // Data Receive process (H/W Rx socket buffer -> User's buffer)

		if(ret <= 0) return ret; // If the received data length <= 0, receive failed and process end
		size = (uint16_t) ret;
		sentsize = 0;
       
		// Data sentsize control
                   
		while(size != sentsize)
		{
                          // only receiving and sending to UART
			HAL_UART_Transmit(&huart3, "A", 1, 100);
			size=0;
		}
	}
	//////////////////////////////////////////////////////////////////////////////////////////////
	break;

case SOCK_CLOSE_WAIT :

#ifdef LOOPBACK_DEBUG
//printf("%d:CloseWait\r\n",sn);
#endif
if((ret=disconnect(sn)) != SOCK_OK) return ret;
#ifdef LOOPBACK_DEBUG
printf("%d:Socket Closed\r\n", sn);
#endif
break;

case SOCK_INIT :

#ifdef LOOPBACK_DEBUG
printf("%d:Try to connect to the %d.%d.%d.%d : %d\r\n", sn, destip[0], destip[1], destip[2], destip[3], destport);
#endif
if( (ret = connect(sn, destip, destport)) != SOCK_OK) return ret; // Try to TCP connect to the TCP server (destination)
break;

case SOCK_CLOSED:
	close(sn);
	if((ret=socket(sn, Sn_MR_TCP, any_port++, 0x00)) != sn){
		if(any_port == 0xffff) any_port = 50000;
		return ret; // TCP socket open with 'any_port' port number
	}

#ifdef LOOPBACK_DEBUG
//printf("%d:TCP client loopback start\r\n",sn);
//printf("%d:Socket opened\r\n",sn);
#endif
break;
default:
break;
}
return 1;
}

I forked this W5500 library from an existing one for Arduino and made substantial improvements to it. I’ve had it running on the internet for over 2 years as both a client and a server. You may want to give it a try. Examples are included. GitHub - SapientHetero/Ethernet: Ethernet Library for Arduino

Copyright © 2017 WIZnet Co., Ltd. All Rights Reserved.