0:09
hello and welcome to controllers tech
0:13
this is fifth video in the ethernet
0:15
series and today we will cover the tcp
0:20
here the stm32 controller will be used
0:23
as the client and it will communicate
0:25
with the server which we will create in
0:29
so let's start the video and i will
0:31
directly go to the main file
0:34
you can check the connection part in the
0:36
first two videos of this series
0:39
here is the main file and i will start
0:42
with the ping test first
0:44
you can see the same code is being used
0:47
which we have used in all the videos
0:52
ethernet if input calls the appropriate
0:54
function based on the type of the packet
0:58
that's why we have been using the same
1:01
code for all the protocols so far
1:04
let's test the ping now
1:14
all right the flashing is done
1:22
this is the i p that i set up in the
1:28
and it passed the ping test so we are
1:35
now include the tcp client library files
1:47
let's take a quick look at the source
1:49
file to see some important functions
1:52
i took the example provided by the st
1:55
and modified them a little here
1:57
if you have watched the tcp server video
2:00
you will see a lot of similarities here
2:03
these are all the functions that are
2:07
most of them are similar to the tcp
2:12
i have created a periodic timer callback
2:14
just like the udp client video and this
2:17
function will be called every second
2:20
here are the steps to implement a tcp
2:24
create a tcp block connect to the server
2:28
and start communicating
2:30
to create a tcp block we will use the
2:33
same function tcp new then we will
2:36
connect to the tcp server
2:39
here first we will convert the server ip
2:42
address in the integer format
2:46
this is the ip of the computer and you
2:48
can check it by typing ipconfig on
2:50
windows command prompt
3:00
finally we will use tcp connector to
3:03
connect to this server
3:05
let's use some random port later we will
3:08
create the server with same port
3:13
once the client is connected to the
3:15
server this callback will be called
3:21
just like the tcp server here also we
3:24
will initialize few other callbacks
3:30
we are mainly interested in the receive
3:32
callback which will be called whenever
3:34
the server sends some data to the client
3:37
we will check it in a while
3:39
the timer callback which will be called
3:42
every one second here we will send some
3:44
data to the server using the function
3:49
when the server sends some data back the
3:51
received callback will be called
4:00
just like tcp server this function have
4:03
some predefined code to handle the
4:05
errors but we are interested in this
4:07
part if the state is switched to
4:11
here first of all we must acknowledge
4:16
tcp receive function is used for the
4:20
then we will call the client handle
4:22
function to handle the received data
4:28
we will free the buffer
4:30
let's see the client handle function now
4:34
well i am not doing anything special
4:37
i am getting the i p address of the
4:39
server just in case if you need the i p
4:43
and in the end increment the counter
4:50
this counter variable is being used in
4:52
the timer callback function
4:55
here i am sending this message to the
4:57
server which will send the updated
5:01
so whenever the server sends some data
5:04
the counter will increment
5:06
otherwise this callback will keep
5:09
sending the same counter value every
5:12
we will allocate the memory to the
5:14
buffer using p buffaloc function
5:17
then copy the data into the payload of
5:22
and finally send the data to the server
5:25
in the end we will free the buffer
5:28
let's build it once to check for any
5:35
we have few warnings but that's all
5:39
let's write the main function now
5:42
first we will start the timer interrupt
5:44
so that the periodic callback can work
5:48
then initialize the tcp client
5:56
we also need to include the header file
6:04
all right let's build and flash it
6:14
i am going to use the hercules for the
6:33
here i have used the port 31 so let's
6:36
listen to the same port
6:40
we can check the status here in this
6:44
let's run the code now
6:48
we got the client connection success
6:51
here you can see the client sending data
6:58
the count is 1 because we are not
7:01
sending any data to the client and this
7:03
is why the client handle function is not
7:05
being called and the counter is not
7:16
but as soon as the server sends some
7:18
data to the client the counter will
7:20
increment and you can see the updated
7:22
data sent by the client
7:27
and again we can see the updated data
8:00
let's see the process of execution
8:18
here i am setting a break point inside
8:20
the client connected callback
8:23
one inside the receive callback
8:30
another one inside the sent callback
8:44
and one last break point here
8:48
let me clear everything first
8:53
as soon as the client got connected to
8:55
the server we got a hit inside the
9:01
another hit in the handle function
9:06
this is because we are calling the
9:07
client handle function inside the
9:16
now after the client sends the message
9:18
there is another hit inside the sent
9:40
you can see the messages are being sent
9:46
let me remove this break point
9:51
ok now we will send some data to this
9:58
here we got a hit inside the receive
10:09
this function will call the server
10:10
handle and now the counter value will
10:23
the connection got disconnected for
10:25
being idle for some time
10:28
but i think you got the idea of how this
10:32
this is it for this video
10:34
i hope you understood the working for
10:39
we were able to send the data to the
10:41
server and receive some data from it
10:44
you can download the code from the link
10:49
leave comments in case of any doubt
10:52
keep watching and have a nice day ahead