0:09
hello and welcome to controllers Tech
0:13
this is the seventh video in the SDM 32
0:16
modbus series and today we will see how
0:19
the master will modify a single and
0:21
multiple coils in the slave device
0:24
this is again going to be a continuation
0:26
from the previous video where we
0:28
programmed the stm32 to respond to the
0:31
queries regarding writing single and
0:35
in today's video the master will send a
0:38
request to modify the coils in the stm32
0:41
and we will program the stm32 to handle
0:46
basically we will be working with the
0:48
function codes 5 and 15 but from the
0:53
the function code 5 is to modify a
0:55
single coil whereas the function code 15
0:58
is used to modify multiple coils at once
1:02
as mentioned here the master can modify
1:05
a maximum of 1968 coils at once
1:08
this is going to be the same project we
1:11
used in the previous video
1:13
I have added a few things to it and we
1:16
will only discuss those things
1:19
here in the main file I have added the
1:21
function codes 5 and 15.
1:24
we will call the respective functions
1:29
we are going to modify the coil database
1:31
which I defined in the previous
1:35
let's take a look at the writing single
1:39
here first we will extract the address
1:41
of the coil which the master wants to
1:45
if the address is greater than 199 the
1:48
slave will send an exception
1:51
this is because the database is defined
1:56
next we will calculate the start byte
2:00
this is the byte in the database where
2:02
the modification will be done
2:05
here each byte contains eight coils
2:08
next we will calculate the bit position
2:12
this is the shift in the start byte
2:15
for example if the master wants to
2:18
modify the 14th coil then the start byte
2:21
will be 1 and the bit Position will be
2:24
next comes the data bytes
2:27
the master sends two data bytes
2:31
if the data bytes are zero cross FF
2:33
followed by zero that means the master
2:36
wants to enable the coil
2:38
similarly if the data bytes are zero and
2:41
zero that means the master wants to
2:45
any other values sent by the master does
2:48
not have any effect on the state of the
2:51
here we will look for the next two bytes
2:54
in the RX data buffer
2:56
if the bytes are zero cross FF followed
2:59
by zero that means the master wants to
3:03
so we will add a 1 to the bit position
3:08
similarly if the 2 bytes are both zeros
3:11
we will add a zero to the bit position
3:14
say for example the master wants to
3:17
write a 1 to the 14th coil
3:19
the start byte is one and the big
3:24
so we will shift the one by six places
3:26
to the left and add the data with the
3:31
similarly if the master wants to write a
3:33
zero to the 14th coil we will shift a
3:36
one to the left by six places then
3:38
negate the entire data and perform the
3:42
I hope you understood how the writing is
3:44
taking place here as this will remain
3:47
the same even in case of multiple coils
3:50
now the coil has been modified so the
3:53
slave will start preparing for the
3:56
you can see the response format in the
4:00
here the slave will send exactly the
4:02
same data to the master so we will
4:05
simply copy the data from RX buffer into
4:07
the TX buffer and send it to the master
4:10
the CRC will be calculated in the send
4:16
we will see the working towards the end
4:20
now let's see the function to write
4:24
you can see the Masters query format on
4:29
here we will first extract the address
4:33
then calculate the number of coils
4:35
requested by the master
4:37
if the number of coils requested by the
4:40
master are greater than 1968 the slave
4:43
will send an exception
4:45
this is as per the modulus standard
4:48
then we will calculate the address of
4:52
if this address is more than 199 the
4:56
slave will send another exception
4:59
this is because we have only defined the
5:01
database for 200 coils whose address
5:04
ranges from 0 to 199.
5:08
if everything is good so far we will
5:10
calculate the start byte
5:13
this is the first byte in the database
5:15
where the modification will start
5:18
then the bit position is the position of
5:21
the coil in the start byte
5:23
the index position is to keep track of
5:25
the bit position in the RX data buffer
5:29
I have created an index variable to keep
5:31
track of which byte in the RX data
5:33
buffer we are reading
5:35
the current value is 7 because the
5:38
actual data in the RX buffer starts from
5:42
the approach is going to be very
5:46
we will read one bit from the RX data
5:48
and depending on whether that bit is a 1
5:51
or a zero we will enable and disable the
5:55
here we will shift the current byte in
5:58
the RX buffer by the index position
6:01
this will extract the bit from the RX
6:05
if the bit is a 1 we will enable the
6:07
coil and if the bit is a zero we will
6:12
I have already explained how we do that
6:15
in the single coil section
6:17
after modifying each coil the index
6:20
position and bit position values will
6:24
once the index position is greater than
6:26
7 the index variable will increment so
6:30
that we can read the next byte in the RX
6:33
similarly when the bit position is
6:35
greater than 7 the start byte will
6:38
increment so that the modification can
6:40
be done for the next byte
6:42
since we are reading one bit at a time
6:44
this for Loop will run as many times as
6:47
the number of coils requested by the
6:51
all right let's prepare the response for
6:55
the response format is shown on the top
6:59
here we will copy the first six bytes
7:01
from the RX buffer into the TX buffer
7:05
these contain the slave ID function code
7:08
address of the start coil and the number
7:12
finally send the response using the send
7:17
the CRC will be calculated within the
7:22
all right let's build and debug the code
7:33
here I have added the coil database in
7:36
the live expression so that we can see
7:39
I have switched the data format to
7:45
the data here is as per what we have
7:47
defined in the database
7:51
in the master software we have to click
7:54
the right button and this new window
7:56
will allow us to write the data to the
7:59
let's start the testing
8:02
say for example if the master wants to
8:05
modify the third coil
8:07
here you can see the function code is
8:09
automatically set to 5 because the
8:12
master is writing only one coil
8:14
here I am setting the value 1. you can
8:18
see the actual data is going to be 0
8:20
cross FF followed by zero and if we send
8:24
a zero the data is also zero now
8:28
no matter what value you set here the
8:30
software will only send FF followed by
8:34
you can see the current state of the
8:36
coil it is set to zero and once the
8:39
master sends the request the coil has
8:42
been modified with the new value
8:46
let's say if the master wants to modify
8:49
this coil here this is the coil number
8:52
65 and its value is zero let's say the
8:56
master wants to write a 1 to this
8:59
here you can see the value has been
9:01
modified to 1. let's write a 1 to the
9:06
here the 66th coil has also been
9:10
you can only see the modified values in
9:13
the live expression and they will not be
9:15
visible in the actual database
9:18
the master can also reset the coil by
9:21
writing a zero to the respective coil
9:24
so the single coil has been working well
9:27
Let's test the multiple coils now
9:31
say for example if the master wants to
9:33
write eight coils starting from the 65th
9:37
here we have eight rows to write the
9:42
let's input the values for the
9:46
I am just entering some random values
9:49
as per the eight values we entered we
9:52
have the data byte 0 cross D9
9:55
keep track of this byte here
9:58
you can see the data in the database has
10:01
if you start from the most significant
10:04
bit we have one one zero one one zero
10:08
this is exactly the same as the data
10:11
stored in the database
10:13
now let's say the master wants to write
10:15
10 bits starting from the fifth coil
10:19
here I am just sending one for each coil
10:27
this is the fifth coil where the
10:29
modification will start
10:31
here you can see the modified values
10:34
let me change some of these bits to
10:42
here you can see the updated result for
10:48
before we wrap up this tutorial let's
10:51
read these coils and check the values
10:53
the master will receive
10:59
here the master wants to read 10 coils
11:02
starting from the fifth coil you can see
11:05
the data received contains five ones and
11:10
this is indeed the updated value and not
11:13
the one which we stored in the beginning
11:15
so I hope you understood how the master
11:18
writes the data to the coils
11:20
this is the last video of this series
11:22
for now if I come across something else
11:25
I will make another video but for now
11:28
this is the end of the modbus series
11:31
the link to download the code is in the
11:34
description of the video
11:35
leave comments in case of any doubt
11:38
keep watching and have a nice day ahead