RT1170 LWIP socket client demo

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

RT1170 LWIP socket client demo

RT1170 LWIP socket client demo

1.Introduction

Recently, some customers need the RT1170 LWIP socket client, so this post is mainly share the socket client code which is based on the RT1170 SDK, it is just a simple demo, which also give the test result based on the NXP official EVKB board.

2. Code modification

Platform:

MIMXRT1170-EVKB

SDK_2_13_1_MIMXRT1170-EVKB

MCUXpresso IDE v11.7.1

Code is based on the SDK project : lwip_ping_freertos_cm7.

This project already add the socket related file, so the modification is simple, just need to add the socket related header file and the app function.

The modification is:

Add socket server IP address, port, and the message which want to sendout.

#define INIT_THREAD_STACKSIZE 1024
/*! @brief Priority of the temporary lwIP initialization thread. */
#define INIT_THREAD_PRIO DEFAULT_THREAD_PRIO
#define   HOST_NAME       "192.168.0.100"
#define BUF_LEN  100
uint8_t senddata[]= "Socket client test";
#define PORT            54321
#define IP_ADDR        "192.168.0.100"

Comment the ping code calling in stack_init API.

//  ping_init(&netif_gw);

Add the socket client thread:

sys_thread_new("socketclient", socketclient_thread, NULL, DEFAULT_THREAD_STACKSIZE, DEFAULT_THREAD_PRIO);

The thread code is:

static void socketclient_thread(void *arg)
{
	  int sock = -1,rece;
	  struct sockaddr_in client_addr;

	  char* host_ip;
	  ip4_addr_t dns_ip;
	  err_t err;

	  uint32_t *pSDRAM= pvPortMalloc(BUF_LEN);//
	    host_ip = HOST_NAME ;
	    PRINTF("host name : %s , host_ip : %s\r\n",HOST_NAME,host_ip);

		//  while(1)
		//  {
			  PRINTF("Start server Connect !\r\n");
			   // create connection
		       sock = socket(AF_INET, SOCK_STREAM, 0);
		       if (sock < 0)
		       {
		         PRINTF("Socket error\n");
		         vTaskDelay(10);
		      //   continue;
		        }
		       client_addr.sin_family = AF_INET;
		       client_addr.sin_port = htons(PORT);
		       client_addr.sin_addr.s_addr = inet_addr(host_ip);
		       memset(&(client_addr.sin_zero), 0, sizeof(client_addr.sin_zero));


		       if (connect(sock, (struct sockaddr *)&client_addr,  sizeof(struct sockaddr)) == -1)
		       {
		          PRINTF("Connect failed!\r\n");
		          closesocket(sock);
		          vTaskDelay(10);
		         // continue;
		       }

		       PRINTF("Connect to server successful!\r\n");

		       //  PRINTF("\r\n************************************************************\n\r");
		       //  PRINTF("\r\n Begin write\n\r");
		       write(sock,senddata,sizeof(senddata));
		       while (1)
		       {
			      //receive data
		          rece = recv(sock, (uint8_t*)pSDRAM, BUF_LEN, 0);//BUF_LEN
		          if (rece <= 0)
		            break;

		          PRINTF("recv %d len data\r\n",rece);
		          PRINTF("%.*s\r\n",rece,(uint8_t*)pSDRAM);
		          write(sock,pSDRAM,rece);

		       }
		       //rec data process
		       memset(pSDRAM,0,BUF_LEN);
		       closesocket(sock);
		    vTaskDelay(10000);//about 10s //10000
		 // }

}

 

3. Test Result

Firstly, use the PC to configure the ENET IP for the server:

    192.168.0.100

 socket_server_config.png

After configuration, customer can use the TCP test tool, eg:USR-TCP232-Test, which is configured to the TCP server, local IP is:192.168.0.100, host port is:54321, then enter the listen mode:

 socket_server_test.png

After the code download to the MIMXRT1170-EVKB, and run it, we can see, the server can detect the connected client IP:192.168/0.102, after the client connect to the server, it will send out the message: “socket client test”, then the server can send out the message to the client, the client will use the UART printf it, also loop back to the server again.

This is the test result video:

Code attached:evkbmimxrt1170_lwip_socket_client_freertos_cm7.7z

 

Attachments
No ratings
Version history
Last update:
‎07-20-2023 11:12 PM
Updated by: