0:09
hello and welcome to controllers Tech
0:13
this is the second video in the SDM 32
0:16
modbus series and today we will see how
0:18
to read coils and discrete inputs
0:21
you must watch the previous video as we
0:24
will continue where we left off last
0:27
also I am not going to explain every
0:30
detail that has been already explained
0:32
in the previous video
0:34
if you are starting the modbus series
0:36
better watch the playlist in the order
0:38
videos are placed the connection is the
0:42
same as how it was in the previous video
0:43
the only change is that I am using the
0:49
here is the code from the previous video
0:51
and we will continue with this one
0:54
the only change I have made is that I am
0:57
using the uart2 today with the f446
1:01
all right let's see the address again
1:04
the coils are one bit memories whose
1:06
address ranges from one to ten thousand
1:09
the master can perform read or write
1:13
the discrete inputs on the other hand
1:15
starts from 1001 to 20 000. and these
1:19
memory locations are read only so the
1:22
master cannot write into it
1:24
the function code 1 can be used to read
1:27
the coils and the function code 2 can be
1:31
used to read the inputs
1:33
both are one bit in size but the maximum
1:36
amount of coils or inputs that can be
1:39
read continuously are limited to 2000. I
1:43
have already explained the data size
1:45
limitation in the previous video and
1:47
this is the result of the same
1:49
anyway the master will send the query in
1:52
the similar way it did for the registers
1:55
it will send the slave address followed
1:57
by the function code then the start
1:59
address of the coil followed by the
2:01
number of coils it wants to read and at
2:06
in total it makes up the 8 bytes
2:09
here is the TX data buffer
2:12
we will keep the same slave address
2:15
the function code will be 1 to read the
2:23
next we need to send the start address
2:26
let's see the slave software I am using
2:30
here I have enabled the function code 1
2:35
they are one bit in size
2:38
the start address and the offset both
2:42
here I am displaying 100 values
2:46
since the offset is also set to 1 the
2:49
master needs to send zero in order to
2:51
access the first coil
2:53
I have already explained it in the
2:55
previous video the master sends the
2:58
address which is the difference between
3:00
the address it wants to access and the
3:03
similarly to access the coil 2 it needs
3:07
to send the address 1 and to access the
3:09
coil 3 it needs to send the address to
3:14
so let's say the master wants to start
3:16
reading from the first coil it should
3:21
the address takes two bytes and
3:23
therefore the TX data buffer 2 and 3 are
3:31
next is the number of coils the master
3:35
this also takes two bytes and let's say
3:38
the master wants to redate coils
3:41
I am starting with eight coils as each
3:43
coil takes one bit and this will make
3:48
the CRC calculation will remain similar
3:52
and finally we will send this query
3:57
let's delete this as it is not useful
3:59
anymore in case of coils
4:02
all right let's build the code and see
4:05
what data are we receiving
4:07
I am adding a breakpoint after the
4:17
let me change the data format to hex and
4:23
all right we have hit the break point
4:26
you can see the TX data buffer contains
4:28
the query sent by the master
4:31
we have the slave ID the function code
4:34
two bytes for the start address the next
4:36
two bytes for the number of coils and
4:39
the last two bytes for the CRC
4:42
you can see the data sent by the master
4:45
this is the actual data sent by the
4:49
the master requested the data for eight
4:51
coils and the slave sent a byte of
4:55
here is the data stored in the coils I
5:00
the master is requesting from the first
5:03
coil itself so let's see the data for
5:05
the first eight coils
5:07
we have on off on on off on off on
5:14
if you combine it as a single byte it
5:16
corresponds to a D in hexadecimal
5:20
we have received the same in the RX data
5:24
the slave response consists of the slave
5:27
ID the function code the number of bytes
5:30
it is sending the actual data itself and
5:35
reading eight coils was a perfect
5:37
situation as the slave also sends the
5:40
data in the byte format
5:44
now let's see what happens if we read 10
5:49
I am keeping everything else the same
5:57
notice the third byte in the RX data
6:00
buffer which indicates how many bytes of
6:02
data is sent by the slave
6:05
the slave is actually sending two bytes
6:08
of data for ten coils
6:10
the first byte is zero cross a d and the
6:15
if you look at the ninth and tenth coils
6:19
the information for the first eight
6:21
coils is stored in the first bytes and
6:24
the data for the ninth and tenth coil is
6:26
stored in the first two bits of the
6:30
so one thing we know from this is the
6:32
slave sends the data in the byte format
6:35
if the master is reading up to eight
6:37
coils it will send one byte of data
6:41
if the master is reading more than eight
6:43
coils up to 16 coils it will send two
6:48
basically the data is going to come in
6:51
bytes the number of bytes depends on the
6:53
number of coils the master has requested
6:56
let's try one more thing
6:59
this time I will read the 10 coils
7:02
starting from the second coil
7:08
you can see we have received the two
7:13
we have zero cross 56 and 2.
7:17
Let's cross check this with the data
7:24
this time we are reading from the second
7:26
coil so we have 0 1 1 0 1 0 1 0 0 1.
7:36
this is exactly what the master has
7:40
I hope you understood how to read these
7:42
coils and how the slave is going to send
7:45
them in a group of bytes
7:47
now after the master has received the
7:49
bytes it still needs to separate them in
7:53
let's define an integer array
7:57
keep in mind the array length should be
7:59
in the multiples of 8 or else the final
8:01
data will get corrupted
8:04
since I am reading 10 coils I am keeping
8:07
the length 16. we will write a program
8:09
inside the Callback function itself
8:13
here is a very simple program to extract
8:16
the bits from the byte buffer the third
8:19
byte of the RX data buffer indicates how
8:21
many bytes of information is sent by the
8:25
and then the information actually starts
8:27
from the fourth byte the RX data 3.
8:31
let's start with J is zero and I zero
8:35
the data and RX data 3 will shift to the
8:38
right by zero places and it will and
8:42
when we use the N1 we are actually
8:45
extracting the least significant bit
8:47
now the I will increment to 1 and the
8:50
data will shift to the right by 1 and
8:53
the least significant bit will be
8:56
we will do this until all the bits from
8:58
the RX data 3 are extracted
9:01
then the J Will increment to 1 and this
9:04
process of extracting the least
9:06
significant bit will again start but for
9:23
you can see here we got the data in the
9:27
let's compare it with the data we had
9:31
we are reading from the coil 2 and we
9:34
have 0 1 1 0 1 0 1 0 0 1.
9:40
this is exactly what we got in the data
9:48
so we were able to read coils and
9:50
extract the bits from the byte format we
9:54
now let's quickly see how to read the
9:58
we will enable the function code 2 in
10:02
the address for the input starts from
10:06
we will keep the same offset so the
10:09
address for the first input will be zero
10:12
and I am displaying 20 values here
10:15
let's give some random values to these
10:26
all right the process for reading the
10:29
inputs is the same as that of the coils
10:32
the only difference is instead of
10:34
function code 1 now we will use code 2.
10:43
I am going to read from the 10 inputs
10:46
starting from the first input itself
10:58
let's build and debug the code
11:10
all right we have got ourselves the
11:13
values in the data buffer
11:15
if you compare them to the ones we have
11:17
stored they are obviously going to be
11:21
so we saw how to read the coils and
11:25
the process is same for both as the
11:28
difference between them is that Master
11:29
can write the data into the coils but on
11:32
the other hand the inputs are read only
11:35
I hope you understood the video
11:38
we will continue with the series and in
11:41
the next video we will write the data
11:43
into the holding registers and coils
11:47
you can download the code from the link
11:51
keep watching and have a nice day ahead