Azure NetX Duo application help?
First of all..... is anyone actually using the Azure RTOS package? I'm drinking the cool-aid provided by STM marketing. But, geez! ....how do you get started? I've been reading...and reading...and reading...but can't seem to crack the code (so to speak...:)
Specifically, I have been trying to figure out how the modify the NX_Tcp_Echo_Server example so that it DOESN'T echo the data received from the network. Instead I am forwarding the received data_buffer array ("command") to USART2 which goes to a second board. The second board then responds to the command with a 64 byte "response". I use the HAL_UART_Transmit and Receive functions to do this, which (after fixing the USART2 bug in the example code) works fine. The Receive function returns the 64 bytes in the same data_buffer that was used to send the command.
My problem is....how to I "inject" this data_buffer response into the nx_tcp_socket_send() function? I'm sure there must be a simple process for this, but the information provide is a bit overwhelming, to say the least.
So my problem boils down to, how do I send 64 bytes of data through an existing socket? I'm sure it MUST be simple....right?
Here's the modified thread. After the HAL_Receive funtion returns, the data_buffer contains the 64 bytes of data that I want to return to the socket.
static VOID App_TCP_Thread_Entry(ULONG thread_input)
{
UINT ret;
UCHAR data_buffer[512];
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)
{
Error_Handler();
}
/*
* 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)
{
Error_Handler();
}
else
{
printf("TCP Server listening on PORT %d ..\n", DEFAULT_PORT);
}
if(tx_semaphore_get(&Semaphore, TX_WAIT_FOREVER) != TX_SUCCESS)
{
Error_Handler();
}
else
{
/* accept the new client connection before starting data exchange */
ret = nx_tcp_server_socket_accept(&TCPSocket, TX_WAIT_FOREVER);
if (ret)
{
Error_Handler();
}
}
while(1)
{
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);
}
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)
{
BSP_LED_Toggle(LED_GREEN);
/* 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);
/* print the received data */
PRINT_DATA(source_ip_address, source_port, data_buffer);
HAL_UART_Transmit(&huart2, data_buffer, cmdLen, 0xFFFFFFFFU);
HAL_UART_Receive(&huart2, data_buffer,cmdLen,0x3E8U );//0x3E8U = 1000msec = 1Sec timeout
//Forward response to network
ret = nx_tcp_socket_send(&TCPSocket, data_packet, NX_IP_PERIODIC_RATE);
if (ret == NX_SUCCESS)
{
BSP_LED_Toggle(LED_GREEN);
}
}
else
{
nx_tcp_socket_disconnect(&TCPSocket, NX_WAIT_FOREVER);
nx_tcp_server_socket_unaccept(&TCPSocket);
nx_tcp_server_socket_relisten(&IpInstance, DEFAULT_PORT, &TCPSocket);
}
}
else
{
/*toggle the green led to indicate the idle state */
BSP_LED_Toggle(LED_GREEN);
}
}
}Thanks,
MikeH
