0:13
this is the fifth video in the modbus
0:15
series and we will continue using stm32
0:20
in the previous video we saw how to
0:23
program stm32 to respond to the queries
0:26
regarding reading the holding and input
0:30
today we will see how to respond to the
0:33
queries regarding reading the coils and
0:37
as I mentioned in the previous video we
0:39
will continue using the same project so
0:42
you should watch the previous video
0:45
I am not going to explain anything that
0:47
has already been covered in the last
0:50
so today we are going to work with the
0:52
function codes 1 and 2. note here that
0:56
as per the standards the master can
0:59
request up to 2000 coils or inputs at
1:03
I have already written the code and I
1:06
will explain it in this video
1:08
this is the same modbus slave source
1:11
file but I have added the functions for
1:13
coils and inputs in it in the modbus
1:16
slave header file we had the database
1:18
for the registers but now I have also
1:21
added the database for the coils and
1:24
there are 25 bytes in the database which
1:27
makes up the data for a total of 200
1:31
the only difference in the inputs
1:33
database is that it's defined as a
1:35
constant array so the master will not be
1:37
able to modify it in the upcoming videos
1:40
in the main file I have added two new
1:44
cases inside the switch function
1:46
for the function code 1 we will call the
1:49
read coils function and for the code 2
1:52
we will call the read inputs
1:55
I have also added an exception Handler
1:57
in case an illegal function code arrives
2:01
now let's see the modbus slave source
2:05
here is the read coils function
2:09
the initial part of this function is
2:11
exactly the same as what we covered
2:15
we find the address of the starting coil
2:19
then we find the number of coils the
2:23
if the number of coils is more than 2000
2:26
the slave will send an exception
2:29
then we calculate the address of the
2:33
since we have only defined the database
2:35
for 200 coils if the end address is more
2:38
than 199 the slave will again send an
2:44
if everything goes all right so far we
2:47
will start preparing the TX data buffer
2:50
first I am resetting the TX data buffer
2:54
then start loading the data into the
2:57
first we have the slave ID then the
3:00
function code which is followed by the
3:02
number of data bytes the slave is
3:06
we know the coils are one bit in size so
3:09
if the slave is sending the data for up
3:11
to eight coils it will send one byte
3:14
similarly if the slave is sending the
3:17
data for nine coils to 16 coils it needs
3:22
I am using this formula to calculate the
3:27
say for example if the number of coils
3:29
are 20 the first part would give the
3:32
output equal to 2. since there will be a
3:34
remainder when 20 is divided by 8 the
3:37
second part would give one
3:39
the total bytes in case of 20 coils will
3:42
be 3. similarly if the master has
3:45
requested 24 coils the first part will
3:49
give an output of 3. there is no
3:51
remainder when 24 is divided by 8 so the
3:55
second part would give zero the total
3:58
bytes needed for 24 coils will be equal
4:00
to 3. we will store this byte count in
4:06
then I am defining an index variable to
4:08
keep track of how many bytes have been
4:12
here I have mentioned the method I am
4:14
going to use to copy the actual data
4:17
into the buffer basically I am going to
4:20
read one bit at a time and store it in
4:23
here first we will calculate the start
4:26
byte which is basically the first byte
4:28
in the database where the copying starts
4:31
say for example if the address of the
4:34
start coil is 13 the start byte would be
4:37
1. bit position is the shift we need to
4:41
for the address 13 the shift would be 5.
4:45
so we need to shift the byte to the
4:47
right by five positions to get the
4:50
index position is the shift in the
4:53
current byte of the buffer data we will
4:56
store the bits right to left in the
4:57
buffer so the lower bits will be stored
5:00
at lower positions and higher bits at
5:04
now comes the main section where we
5:06
actually extract these bits and store
5:10
since we are copying one bit at a time
5:12
the for Loop will repeat as many times
5:14
as the number of coils has been
5:16
requested by the master
5:18
here we will first shift the start byte
5:21
to the right by the bit position then
5:23
extract the bit in the first position
5:25
and shift it to the left in the txt data
5:27
buffer by the index position
5:30
say for example if these are the bytes
5:32
in the database and the master has
5:34
requested 10 coils starting from the
5:39
the start address would be 13 and
5:41
therefore the start byte would be 1 with
5:44
bit position equal to 5.
5:46
we will shift the byte 1 by 5 places to
5:49
the right and extract the bit at this
5:52
then we will store it in the zeroth
5:55
position in the TX data 3.
5:57
now the index position and bit positions
6:02
the index position is 1 and bit position
6:07
we will again shift the start byte by
6:09
six places to the right and extract the
6:13
then we will store it in the first
6:15
position in the TX data 3.
6:18
this cycle will continue
6:21
once the bit position is more than 7 we
6:23
will increment the start byte and reset
6:26
the bit position to zero
6:28
similarly once the index position is
6:30
more than seven we will increment the
6:32
index variable so as to fill out the
6:35
next byte in the TX buffer
6:40
if the number of coils is not a multiple
6:42
of 8 we need to increment the index
6:45
variable again before we can start
6:49
then we will call a function to send the
6:52
data to the uart where the CRC will be
6:54
calculated in the function itself
6:57
reading inputs is exactly the same so no
7:01
need to explain it again
7:05
all right let's build the code and debug
7:11
here is the master software again
7:14
the slave ID is 7. we will start reading
7:18
from the first coil and we will read
7:20
only one coil the function code is 1.
7:24
based on the above configuration the
7:27
master has prepared the query
7:29
let's run the code now
7:35
the master has received the response
7:38
from the slave and here you can see the
7:42
since we only requested data for one
7:44
coil here it had received a 1. if you
7:48
look at the database here is the data
7:50
for the first coil and it's a one
7:53
now let's say we want to read six coils
7:56
starting from the first coil itself
7:59
here we have received the data for the
8:02
six coils and this is exactly the same
8:04
as what it was defined in the database
8:08
let's change the start coil now and this
8:11
time I will read six coils starting from
8:21
since the start coil is 19 the data will
8:25
start from here here are the six bits
8:28
and these are the same as what the
8:36
let's read 14 coils now starting from
8:41
since the master has requested more than
8:44
eight coils the data will arrive in two
8:48
here you can see the first six bits they
8:51
are from the third byte of the database
8:54
then the next eight bits are from the
8:56
fourth byte of the database
9:02
for the final test I am going to read 16
9:05
coils starting from the ninth coil
9:12
basically these are the bytes the master
9:18
this is exactly the same data what the
9:25
now let's quickly see the function code
9:27
2 that is for reading the inputs
9:30
the inputs address ranges from 1001 to
9:34
20 000. here I am requesting the 16
9:38
inputs starting from the very first one
9:41
here we have received the 16 bits and
9:44
they are exactly the same as the first
9:46
two bytes in the inputs database
9:51
let's quickly see the exceptions now
9:54
we know the master can request a maximum
9:56
of 2000 coils or inputs at once
10:00
so here I am going to request 2001
10:04
and we got an exception saying illegal
10:08
this is because we have set the limit of
10:10
2000 coils in the code
10:15
for another exception that sir Master
10:18
wants to read three coils starting from
10:24
since I have only defined the database
10:27
for 200 coils if the Masters request has
10:30
exceeded the last coil available in the
10:32
database the slave will again send an
10:36
this time it says illegal date address
10:40
although if the master wants to read
10:42
only the 200th coil it can do that since
10:45
the data for 200 coils is available in
10:50
here this bit represents the data for
10:54
but if it wants to read 20 first coil it
10:58
will get the exception
11:00
so I hope you understood how to program
11:02
the stm32 to respond to these coils and
11:08
we will continue with the series and in
11:10
the next video I will cover the master
11:12
writing the registers and coils
11:15
that would probably be the last video of
11:19
this is it for today's video
11:22
you can download the code from the link
11:26
leave comments in case of any doubt
11:29
keep watching and have a nice day ahead