cancel
Showing results for 
Search instead for 
Did you mean: 

NetXDuo TCP Client Notify Issue

NApps.1
Associate II

Hi all,

 

I am currently running tcp echo client example code from st on a NUCLEO-H563ZI which I have got working send and receiving messages over TCP/IP I am trying to get nx_tcp_socket_receive_notify working however when I use the function and connect to the server the callback is never called when the server sends a message any ideas why its not would be great I have attached the code to give a better idea of what I am doing.

 

volatile int data_received_flag = 0;
static VOID App_TCP_Thread_Entry(ULONG thread_input){
	UINT ret;
	UINT count = 0;

	ULONG bytes_read;
	UCHAR data_buffer[512];

	ULONG source_ip_address;
	UINT source_port;

	NX_PACKET *server_packet;
	NX_PACKET *data_packet;

	/* create the TCP socket */
	ret = nx_tcp_socket_create(&NetXDuoEthIpInstance, &TCPSocket, "TCP Server Socket", NX_IP_NORMAL, NX_FRAGMENT_OKAY,
			NX_IP_TIME_TO_LIVE, WINDOW_SIZE, NX_NULL, NX_NULL);


	if (ret != NX_SUCCESS){
		Error_Handler();
	}

	/* bind the client socket for the DEFAULT_PORT */
	ret =  nx_tcp_client_socket_bind(&TCPSocket, DEFAULT_PORT, NX_WAIT_FOREVER);

	if (ret != NX_SUCCESS){
		Error_Handler();
	}



	/* connect to the remote server on the specified port */
	//ret = nx_tcp_client_socket_connect(&TCPSocket, host_ip_address, TCP_SERVER_PORT, NX_WAIT_FOREVER);

	ret = nx_tcp_client_socket_connect(&TCPSocket, TCP_SERVER_ADDRESS, TCP_SERVER_PORT, NX_WAIT_FOREVER);

	if (ret != NX_SUCCESS){
		//Error_Handler();
		uartOut("Can't connect to server", true);
	}else{
		uartOut("Connected to server", true);
	}

	// Register the callback
	ret = nx_tcp_socket_receive_notify(&TCPSocket, tcp_data_received_callback);
	if (ret != NX_SUCCESS){
		//Error_Handler();
		uartOut("Can't setup notify", true);
	}else{
		uartOut("Can setup notify", true);
	}



    while (1)
    {

    	//uartOut("waiting on messaage", true);
        // Main application loop
        if (data_received_flag)
        {
            char receive_buffer[512];

            // Receive data when the flag is set
            ret = nx_tcp_socket_receive(&TCPSocket, &server_packet,NX_APP_DEFAULT_TIMEOUT);

            // Process the received data as needed

            // Clear the flag
            data_received_flag = 0;
            	if (ret == NX_SUCCESS) {
            		/* get the server IP address and  port */
            		nx_udp_source_extract(server_packet, &source_ip_address, &source_port);

            		/* retrieve the data sent by the server */
            		nx_packet_data_retrieve(server_packet, data_buffer, &bytes_read);

            		/* print the received data */
            		PRINT_DATA(source_ip_address, source_port, data_buffer);

            		/* release the server packet */
            		nx_packet_release(server_packet);
            		uartOut(" Server: ", false);
            		uartOut(data_buffer, true);

            		/* toggle the green led on success */
            		HAL_GPIO_WritePin(GPIOI, GPIO_PIN_9, GPIO_PIN_SET);
            	} else {
            		/* no message received exit the loop */
            		// break;
            		uartOut(" Issue getting data back from server", true);
            	}
        }

        // Your main application logic goes here
    }
}

VOID tcp_data_received_callback(NX_TCP_SOCKET * p_socket) {
	// This callback is called when data is received on the TCP socket.
	    data_received_flag = 1;

}

 

1 REPLY 1
STea
ST Employee

Hello @NApps.1 ,

the tcp_receive_notify* Application callback function pointer that is called when one or more packets are received on the socket. if you are receiving packets normally this should trigger the callback to be called. 

i recommend you start from this template implementation of the App_TCP_Thread_Entry then you can fine tune it to your needs 

 

static VOID App_TCP_Thread_Entry(ULONG thread_input)
{
  UINT ret;
  UINT count = 0;

  ULONG bytes_read;
  UCHAR data_buffer[512];

  ULONG source_ip_address;
  UINT source_port;

  NX_PACKET *server_packet;
  NX_PACKET *data_packet;

  /* create the TCP socket */
  ret = nx_tcp_socket_create(&NetXDuoEthIpInstance, &TCPSocket, "TCP Server Socket", NX_IP_NORMAL, NX_FRAGMENT_OKAY,
                             NX_IP_TIME_TO_LIVE, WINDOW_SIZE, NX_NULL, NX_NULL);
  if (ret != NX_SUCCESS)
  {
    Error_Handler();
  }

  /* bind the client socket for the DEFAULT_PORT */
  ret =  nx_tcp_client_socket_bind(&TCPSocket, DEFAULT_PORT, NX_WAIT_FOREVER);

  if (ret != NX_SUCCESS)
  {
    Error_Handler();
  }

  /* connect to the remote server on the specified port */
  ret = nx_tcp_client_socket_connect(&TCPSocket, TCP_SERVER_ADDRESS, TCP_SERVER_PORT, NX_WAIT_FOREVER);

  if (ret != NX_SUCCESS)
  {
    Error_Handler();
  }
// Register the callback
	ret = nx_tcp_socket_receive_notify(&TCPSocket, tcp_data_received_callback);
	if (ret != NX_SUCCESS){
		//Error_Handler();
		uartOut("Can't setup notify", true);
	}else{
		uartOut("Can setup notify", true);
	}
  while(count++ < MAX_PACKET_COUNT)
  {
    TX_MEMSET(data_buffer, '\0', sizeof(data_buffer));

    /* allocate the packet to send over the TCP socket */
    ret = nx_packet_allocate(&NxAppPool, &data_packet, NX_UDP_PACKET, TX_WAIT_FOREVER);

    if (ret != NX_SUCCESS)
    {
      break;
    }

    /* append the message to send into the packet */
    ret = nx_packet_data_append(data_packet, (VOID *)DEFAULT_MESSAGE, sizeof(DEFAULT_MESSAGE), &NxAppPool, TX_WAIT_FOREVER);

    if (ret != NX_SUCCESS)
    {
      nx_packet_release(data_packet);
      break;
    }

    /* send the packet over the TCP socket */
    ret = nx_tcp_socket_send(&TCPSocket, data_packet, NX_APP_DEFAULT_TIMEOUT);

    if (ret != NX_SUCCESS)
    {
      break;
    }

    /* wait for the server response */
    ret = nx_tcp_socket_receive(&TCPSocket, &server_packet, NX_APP_DEFAULT_TIMEOUT);

    if (ret == NX_SUCCESS)
    {
      /* get the server IP address and  port */
      nx_udp_source_extract(server_packet, &source_ip_address, &source_port);

      /* retrieve the data sent by the server */
      nx_packet_data_retrieve(server_packet, data_buffer, &bytes_read);

      /* print the received data */
      PRINT_DATA(source_ip_address, source_port, data_buffer);

      /* release the server packet */
      nx_packet_release(server_packet);

      /* toggle the green led on success */
      HAL_GPIO_WritePin(GPIOI, GPIO_PIN_9, GPIO_PIN_SET);
    }
    else
    {
      /* no message received exit the loop */
      break;
    }
  }

  /* release the allocated packets */
  nx_packet_release(server_packet);

  /* disconnect the socket */
  nx_tcp_socket_disconnect(&TCPSocket, NX_APP_DEFAULT_TIMEOUT);

  /* unbind the socket */
  nx_tcp_client_socket_unbind(&TCPSocket);

  /* delete the socket */
  nx_tcp_socket_delete(&TCPSocket);

  /* print test summary on the UART */
  if (count == MAX_PACKET_COUNT + 1)
  {
    printf("\n-------------------------------------\n\tSUCCESS : %u / %u packets sent\n-------------------------------------\n", count - 1, MAX_PACKET_COUNT);
    Success_Handler();
  }
  else
  {
    printf("\n-------------------------------------\n\tFAIL : %u / %u packets sent\n-------------------------------------\n", count - 1, MAX_PACKET_COUNT);
    Error_Handler();
  }
}
VOID tcp_data_received_callback(NX_TCP_SOCKET * p_socket) {
	// This callback is called when data is received on the TCP socket.
	    data_received_flag = 1;

}

 

this implementation can be found in the STM32Cube_FW_H5_V1.1.0\Projects\STM32H573I-DK\Applications\NetXDuo\Nx_TCP_Echo_Client\ example which you can use as a reference point for implementing the nx_tcp_socket_receive_notify .you can try also to set some printfs for debugging inside the Ethernet callback to see the behavior of your software .

BR

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.