STM32 ETHERNET #5. TCP CLIENT
This is the 5th tutorial in the STM32 ETHERNET series, and today we will see how to use our STM32 as the TCP Client. I would recommend you to go through the previous tutorials first, as I won’t explain everything here, like connections.
Go through the link above, and see how the connection was made in the first tutorial. Also see the TCP server tutorial, so that it will be a little easier to understand this one.
Initializing the TCP Client
- First we need to create the TCP Block using the function
- Next we will connect to the server with the
IP 192.168.0.102 and Port 31
Once the client is connected to the server, the client connected callback will be called. Below is the code for the same.
Client Connected Callback
This callback is called, once the connection between the server and the client is successful.
- As you can see above, this callback initializes few other callbacks.
- for example, the tcp_poll callback, tcp_sent callback, tcp_recv callback etc.
- These are needed for the processing, and you shouldn’t modify them.
- Here we are only interested in the tcp_recv callback. This is where we will write our code to handle the incoming data.
Client Receive Callback
tcp_recv initializes the tcp_client_recv function. It is basically a callback, which is called whenever the client receive some data from the server.
We will write our code to handle this received data.
- The dots in the code above shows some predefined code to handle the errors. You should leave it as it is.
- If the state is switched to connected, we can process the received data.
- here tpcb stores all the info about the server and client, and pbuf stores all the info about the data.
- Here I am storing the reference to the incoming buffer into the “es” structure, which is later passed to client handle, so that we can make use of this incoming data.
- Then we need to Acknowledge the received data, and we do that by using the function
tcp_client_hanldleis called to handle the data received from the server.
- We could have handled the data here also, but I am trying to keep the similarities in the coding between different protocols, and this is why I have created that another function just for the purpose of handling data.
- After the data has been processed, we will free the buffer using
Client data Handler
tcp_client_handle handles the incoming data from the server.
- This is the function, where you can write the code according to your requirement.
- I am not doing anything special, but just getting the IP of the server and incrementing the counter variable.
- This counter variable will be later used while sending the data to the server.
- If you want to make use of the incoming data, the information about the data can be found in the “es” structure, and the info about the server and client can be found in the “tpcb” structure.
Sending the data to the server
Just like the UDP client, here also I have created a timer callback, which will be called every second.
inside the callback, we will send the data to the server.
- Here you can see above I am merging the counter value with a string.
pbuf_allocwill allocate the memory for the pbuf we are going to send
pbuf_takewill copy the string into the payload of the pbuf we are sending
tcp_client_sendwill send the pbuf to the server
- And finally we will free the memory using
- SO the counter value will only increment, if the client handle function is called, which will only happen when the client receive some data from the server
- otherwise the client will keep sending the same counter value to the server every second
- As shown in the figure above, the client was sending the old counter value.
- As soon as the server sent some data, the counter was incremented, and the client started sending new counter value every second
- STM32 Ethernet #10 HTTP Server (Basic) using LWIP NETCONN (RTOS) May 18, 2022
- STM32 Ethernet #9 TCP Server and Client using LWIP NETCONN (RTOS) April 26, 2022
- STM32 Ethernet #8 UDP Client using LWIP NETCONN (RTOS) April 16, 2022
- STM32 Ethernet #7 UDP Server using LWIP NETCONN (RTOS) April 9, 2022
- GPS (Neo 6M) with STM32 March 9, 2022
- BME280 with STM32 January 8, 2022