0:10
and welcome to controllers tech this is
0:13
second video in the ethernet series
0:15
and today we will start with udp server
0:19
well most of you know the reason for
0:24
i did a poll on the youtube and here is
0:28
so i am going to go with udp first then
0:32
and later with the http and after
0:36
we will move to netcon with rtos
0:40
please watch previous video about the
0:43
as i am not going to explain much about
0:47
udp is the simplest of all the protocols
0:51
this is because it does not need any
0:52
connection to be established between the
0:54
server and the client
0:57
you simply connect and start the
1:00
it have its own advantages and
1:04
you can read about them somewhere else
1:06
as i am not explaining that part
1:09
so let's start the video and create a
1:20
i am using f7 board for this tutorial
1:24
give some name to the project and click
1:31
here it's our cube mx and let's enable
1:38
this entire setup looks too messy so i
1:41
am clearing the pin outs first
1:47
now enable the ethernet rmi high
1:51
make sure you cross check the pins with
1:54
as cube mx sometimes assigns the wrong
1:59
all right now let's enable the clocks
2:03
i am choosing the external high speed
2:05
crystal to provide the clock configure
2:08
the clocks according to your board
2:25
now let's go back to ethernet and change
2:28
as i am using on board module
2:42
enable the interrupt also
2:46
this is it for the ethernet setup now we
2:49
will configure the lightweight ip
2:58
disable the dhcp and here we will
3:01
provide the static ip for our module
3:10
leave everything else as it is i am
3:13
going to use the same memory size
3:20
that's it just leave everything else to
3:26
let me just enable the cache this is it
3:30
click save to generate the project now
3:35
here is our main file first thing we are
3:38
going to do is check the connection
3:40
and to do that we will just do the ping
3:43
same as the last time
3:45
i am fast forwarding this as i have
3:48
already covered this in the previous
3:56
let's build and flash it
4:12
now we will ping to the ip address that
4:14
we assigned to the module
4:26
it's not working looks like something is
4:36
okay this should be input not in it
4:57
it's flashed successfully
5:10
it's working now so our hardware
5:14
and now we can go to the udp part
5:34
let's copy the udp server files in our
5:39
i took some functions from the sds
5:42
and i tried to write a simplified
5:44
version so that it would be easier to
5:47
so let's see what's going on here
5:52
here are the steps to implement a udp
5:56
and this is the source where i got them
5:59
you can check out the website
6:01
anyway in order to implement the udp
6:05
we need to create the udp socket bind it
6:08
to the server address
6:09
wait for the packet to arrive from the
6:11
client process that packet
6:13
and repeat this process let's see how it
6:19
we have the server in it function
6:25
here we are creating the udp block first
6:29
then we need to bind this block to the
6:31
ip address and a local port
6:35
first we need to convert the address to
6:37
a 32-bit integer format
6:47
and then bind it to the block we just
6:50
using the function udp bind
6:56
here 7 is the local port for the module
6:58
that is the server port
7:11
next we need to wait for the packet to
7:13
arrive from the client to do this
7:16
we can set up a receive callback which
7:19
will be called whenever a client sends
7:22
if we check out this function it takes
7:24
the control block as the first parameter
7:27
and the second parameters is the receive
7:36
this receive function is our receive
7:39
callback which have been defined here
7:45
as i mentioned this callback will be
7:48
when the client sends some data to the
7:52
we can do the processing inside this
7:56
or you can create another function for
8:08
here p-buff is the packet buffer
8:10
structure and it contains the
8:12
information related to the packet
8:14
sent by the client information like the
8:18
its size etc we also have the udp
8:23
and it contains the information about
8:25
the client and the server
8:28
information like the ip address port etc
8:33
here we will first create a packet
8:35
buffer that we are going to send
8:40
then i am mixing the data send by the
8:42
client with some additional data
8:46
then we will allocate the memory for
8:51
and copy the data into it
8:55
next we will connect to the client
8:58
the address and port for the client are
9:00
the parameters of this function itself
9:06
and finally send the data to the client
9:10
in the end free all the memories that
9:12
were allocated before
9:15
that's it now let's build it once
9:21
we will call this udp server in it in
9:28
also need to include the header file
9:34
this is the only function we need to
9:36
call and as i mentioned before
9:38
it will initialize the rest of them
9:49
i am going to use the hercules as the
9:55
enter the server address and the server
10:05
and the local port can be anything
10:20
let's start this client will initiate
10:24
so i am sending test
10:29
and we got the response from the server
10:32
this here is the same string that we
10:37
let's try something else
10:48
and you can see we are receiving the
10:59
here you can see the receive callback
11:03
it allocates the ram for the buffer
11:06
then copy the data into the buffer
11:10
then connects to the client and sends
11:16
let's say we want to get the i p address
11:18
of the client for some processing
11:24
the address which is passed here is a
11:26
32-bit integer and we need to convert it
11:29
into the proper address format
11:42
we can do that by using this function
11:57
let's pass the 32-bit address to this
12:00
and it will do the conversion
12:01
and will give us the address as a string
12:09
let's test this part
12:22
i am putting a break point right in
12:25
this receive callback will be called
12:28
when the server receives
12:29
some data from the client
12:38
okay let me put a break point in the
12:42
now you can see we got the address as
12:46
this is the address of the client if you
12:50
you can just run ipconfig on your
12:54
and here is the address
13:05
the client port is 12 which is exactly
13:09
what we are listening at
13:11
if you need the ip and port for some
13:14
you can get them like this
13:24
so here we are making use of the payload
13:26
that we receive from the client
13:29
you can write another function to handle
13:32
i will show that part in the tcp
13:36
as the things are going to get more
13:39
and by the way we can still run ping
13:47
this ethernet if input function handles
13:49
the incoming request
13:51
and based on what protocol is being used
13:54
it can call the necessary functions
13:57
so we will pretty much use the same code
14:00
also this is it about the udp
14:04
server i hope you understood it
14:08
the next video will cover the udp client
14:12
if you are getting hard fault watch the
14:14
connection videos properly
14:16
you can download the code from the link
14:21
leave comments in case of any doubt
14:24
keep watching be safe and have a nice