STM32 ETHERNET #3. UDP CLIENT
This it the third tutorial in the STM32 Ethernet series, and today we will see how to create UDP client using STM32. I have already covered a tutorial about UDP SERVER, you can check it out here.
The hardware connection will be similar to the one I have used in the first video, and you can check it out here.
Connect the Client to the server
- We are going to use the following steps to connect the UDP client to a server
- First of all we will create a new UDP control block, using
- Next I am binding the control block to the local IP and port, using
- Also connect the control block to the server IP and port, using
- Then we will send some data to the server, and set a receive function, which will be called, when the server sends some data to the client.
The Receive callback
The receive callback is called, when the client receives some data from the server. Let’s see ho am I handling this function
- here I am copying the data sent by the server into the buffer.
- Although I am not making any use of this data, but you can utilize it as per your requirement.
- then increment the counter. This counter will be used when we will send the data.
- Finally free the packet buffer.
Sending Data to the Server
- Here I have also created a periodic timer, which will generate an interrupt every 1 second.
- The data will be sent to the server in the timer callback, and hence every 1 second.
- To send the data to the server, first of all we will create a packet buffer, txBuf
- Then we will allocate the memory for this packet buffer
- pbuf_take will be used to copy the data into the packet buffer.
- And finally we will send the data using the udp_send function.