Showing results for 
Search instead for 
Did you mean: 

NetXDuo and ThreadX : Increasing number of TCP Server response

Associate II

Dear Experts,

I’m currently working on a project involving an STM32 board (B-U585I-IOT02A). I would like to use this board as a TCP Server . Therefore, from the STM32CUBEIDE application, I had observed that there is an example project in which it is possible to render this board as a TCP Server (Nx_TCP_Echo_Server). However, my problem was that it only returns the value returned by the TCP Client. So, I wanted to modify this program so as to be able to retrieve the temperature parameters. So, I created a program based on an example ( I wanted to combine these two programs to retrieve the temperature data.

To sum up, the diagram is as follows:


After activating the I2C module in the hal_conf.h file, I had no errors during debugging and was able to retrieve the temperature when sending a request from Node-Red. Nevertheless, this program has a number of flaws. The number of requests is limited, as you can see in the image below:


In the image above, I managed to send a total of five requests and collect the temperature, but unfortunately after the sixth request I lost communication with the STM32 board. I also noticed that the number of requests I could send was random. In fact, after resetting the board,  I am able to send more requests and sometimes I can send fewer.

On the application side, I'm using NetX Duo as well as ThreadX.

Would this have anything to do with memory? Would it be possible to increase the number of requests? If so, how could I do it?

You'll find the sections of main.c program and the app_netxduo.c code below.

Program main.c



int main(void)
  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */

  /* MPU Configuration--------------------------------------------------------*/

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */

  /* Configure the System Power */

  /* USER CODE BEGIN SysInit */
  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */



  HAL_UART_Transmit(&huart1, msg1, sizeof(msg1), 1000);
  //HAL_UART_Transmit(&huart1, msg2, sizeof(msg2), 1000);
  //HAL_UART_Transmit(&huart1, msg3, sizeof(msg3), 1000);

  /* USER CODE END 2 */



  /* We should never get here as control is now taken by the scheduler */
  /* Infinite loop */
  while (1)

    /* USER CODE BEGIN 3 */
  /* USER CODE END 3 */



Program App_netduo.c : 



static VOID App_TCP_Thread_Entry(ULONG thread_input)
  UINT ret;
  UCHAR data_buffer[25];

  ULONG source_ip_address;
  NX_PACKET *data_packet;
  UINT source_port;
  ULONG bytes_read;
  /* create the TCP socket */
  ret = nx_tcp_socket_create(&IpInstance, &TCPSocket, "TCP Server Socket", NX_IP_NORMAL, NX_FRAGMENT_OKAY,
                             NX_IP_TIME_TO_LIVE, WINDOW_SIZE, NX_NULL, NX_NULL);
  if (ret)
  * listen to new client connections.
  * the TCP_listen_callback will release the 'Semaphore' when a new connection is available
  ret = nx_tcp_server_socket_listen(&IpInstance, DEFAULT_PORT, &TCPSocket, MAX_TCP_CLIENTS, tcp_listen_callback);
  if (ret)
    printf("TCP Server listening on PORT %d ..\n", DEFAULT_PORT);
  if(tx_semaphore_get(&Semaphore, TX_WAIT_FOREVER) != TX_SUCCESS)
    /* accept the new client connection before starting data exchange */
    ret = nx_tcp_server_socket_accept(&TCPSocket, TX_WAIT_FOREVER);
    if (ret)

    ULONG socket_state;

    TX_MEMSET(data_buffer, '\0', sizeof(data_buffer));
    /* get the socket state */
    nx_tcp_socket_info_get(&TCPSocket, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &socket_state, NULL, NULL, NULL);
    /* if the connections is not established then accept new ones, otherwise start receiving data */
    if(socket_state != NX_TCP_ESTABLISHED)
      ret = nx_tcp_server_socket_accept(&TCPSocket, NX_IP_PERIODIC_RATE); // Problème n°1 : Lorsque j'envoie une première requête, c'est ici que ça bloque (ret =54)
      ret = 0;
    if(ret == NX_SUCCESS)
      /* receive the TCP packet send by the client */
      ret = nx_tcp_socket_receive(&TCPSocket, &data_packet, NX_WAIT_FOREVER);
      if (ret == NX_SUCCESS)
        HAL_GPIO_TogglePin(LED_GREEN_GPIO_Port, LED_GREEN_Pin);
        /* get the client IP address and  port */
        nx_udp_source_extract(data_packet, &source_ip_address, &source_port);

        /* retrieve the data sent by the client */
        nx_packet_data_retrieve(data_packet, data_buffer, &bytes_read);
        BSP_ENV_SENSOR_GetValue(0, ENV_TEMPERATURE, &temp_value);
        int tmpInt1 = temp_value;
        float tmpFrac = temp_value - tmpInt1;
        int tmpInt2 = trunc(tmpFrac *100);

        snprintf((char *) data_buffer, 25, "{\"TEMPERATURE\" : %d.%02d}\r\n", tmpInt1, tmpInt2);

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

        nx_packet_allocate(&AppPool, &send_packet, NX_TCP_PACKET, NX_WAIT_FOREVER); // Normalement c'est là le problème


        /* immediately resend the same packet */
        ret = nx_tcp_socket_send(&TCPSocket, send_packet, NX_WAIT_FOREVER);


        tx_thread_sleep(5); /* Delay 50ms between each pub */

        if (ret == NX_SUCCESS)
          HAL_GPIO_TogglePin(LED_GREEN_GPIO_Port, LED_GREEN_Pin);


        nx_tcp_socket_disconnect(&TCPSocket, NX_WAIT_FOREVER);
        nx_tcp_server_socket_relisten(&IpInstance, DEFAULT_PORT, &TCPSocket);
      /*toggle the green led to indicate the idle state */




I don't really know if it's related or not, but I'm having a problem when I activate the debugger, I get some kind of window informing that it's trying to access an address and that it's outside the memory area :



Thanks for your help,


Nicolas Gnanalingam

ST Employee

Hello @Nicolas_G ,

you can try increasing the stack size of your project as well as the stack size for the netxduo Handling thread and the ThreadX stack and heap sizes as well .


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.
Associate II

Hello @STea,

Thanks for your reply.

Could you tell me how I can increase the stack size of my project, as well as the NetxDuo and ThreadX Stack an Heap size?

I don't really know how to do this.
However, a colleague and I have seen that you can modify parameters in the following files: app_azure_rtos_config.h and app_netxduo.h.

Unfortunately, changing these parameters didn't improve the situation.


ST Employee

Hello @Nicolas_G ,

you need to modify :

in app_azure_rtos_config.h:

  • #define USE_STATIC_ALLOCATION                1
    #define TX_APP_MEM_POOL_SIZE                     2048
    #define NX_APP_MEM_POOL_SIZE                     225280

In the "stm32h5xx_hal_eth.h":

- Depending on the application scenario, the total TX and RX descriptors may need to be increased by updating respectively the "ETH_TX_DESC_CNT" and "ETH_RX_DESC_CNT"  but this will cost extra memory to allocate.

  • #ifndef ETH_TX_DESC_CNT
  • #define ETH_TX_DESC_CNT 8U
  • #endif /* ETH_TX_DESC_CNT */
  • #ifndef ETH_RX_DESC_CNT
  • #define ETH_RX_DESC_CNT 8U
  • #endif /* ETH_RX_DESC_CNT */

in app_netxduo.h :

  • #define NX_APP_THREAD_STACK_SIZE 3 * 1024
  • #define Nx_IP_INSTANCE_THREAD_SIZE 3 * 1024




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.
Associate II

Hello @STea,

Thank you very much for your reply.

Unfortunately, I made some modifications in the program so I don't really know if it works. Indeed, instead of sending back the temperature data, I would like to push the accelerometer data (ISM330DHCX) into Node-Red. So I've taken a program from a colleague and applied its features to the App_Main_Thread_Entry() function in the app_netxduo.c file. In this function, I've added a new function (which I've called send_raw_data) that lets me display the accelerometer's data in UART.I also store these datas in a two-dimensional array (static char* tab_data[5000];).

So far, I've managed to store around 3,000 pieces of accelerometer data, but the problem is that after a large amount of data (e.g. 5,000), the program stops as if I'd saturated the memory. One line of the array will store the following string ""x":0.021968, "y":0.032501, "z":10.127225 ". The reason I'm storing all this data in an array is that, eventually, I'd like to push the whole array into a single TCP client request.

The send_raw_data function is as follows:

char** send_raw_data(float** p_dataf_axes, int* count){
	char uart_buf[40];

	printf("Value:  %d\n", *count ); //*count = 40960

	int uart_buf_len;
	int i;

	for(i = 0; i < 5000; i++){
	//for(int i = 0; i < 100; i++){
		if(i % FIFO_BUFFER_SIZE == 0){
			uart_buf_len = sprintf(uart_buf,"\r\nG%d", (int)(i / (FIFO_BUFFER_SIZE)));
			HAL_UART_Transmit(&huart1, (uint8_t *)uart_buf, uart_buf_len, 100);

		uart_buf_len = sprintf(uart_buf, //sizeof(uart_buf),
				*(*p_dataf_axes + i),
				*(*p_dataf_axes + MAX_ACQUISITION_DATA + i),
				*(*p_dataf_axes + MAX_ACQUISITION_DATA * 2 + i));

		// uart_buf_len = sprintf(uart_buf,"a");

		//printf("uart = %s\r\n", uart_buf);

		tab_data[i] = (char *) malloc(sizeof(uart_buf));

		strcpy(tab_data[i], uart_buf);

		//memcpy(tab_data[i], uart_buf, strlen(uart_buf)+1)

		HAL_UART_Transmit(&huart1, (uint8_t *)uart_buf, uart_buf_len, 1000);


	printf("\r\ni vaut %d \n", i);

	uart_buf_len = sprintf(uart_buf,"\r\n");

	HAL_UART_Transmit(&huart1, (uint8_t *)uart_buf, uart_buf_len, 1000);

	return tab_data;



I'd like to have an array with a size of 40960, each row of which stores x, y and z acceleration data.

I've tried making modifications to the azure_rtos_config.h and app_netxduo.h files as recommended above, but the problem is that I have much less data displayed in UART. On the other hand, when I reduce the size of the NX_APP_MEM_POOL_SIZE in the app_azure_rtos_config.h file, I have a lot more data, but unfortunately it's not enough to reach the 40920 data.

What can I do to store so much accelerometer data because it's essential to be able to push all this accelerometer data to the TCP Client later on?

Please help me because I'm really struggling to find a solution to this problem.

Yours sincerely

Nicolas Gnanalingam


this picture shows that the program stopped after certain data sequences.

****Temperature values measurement **** is the message I display at the start of the program, so there's been a reset. 

Hello @Nicolas_G ,

if you are facing constrains with the memory size i suggest you use the External OCTOSPI memory  MX25LM51245G or the PSRAM in memory mapped mode.

this  Memory can be used to store the data you are willing to store and you can easily access it when you are ready to use it and you can offload the Flash memory to make it handle the Threadx and Netxduo communication .


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.