0:09
hello and welcome to controllers Tech
0:13
this is the third video in the SDM 32
0:16
modbus series and today we will see how
0:19
to write coils and holding registers
0:22
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:39
the connection is the same as how it was
0:42
in the previous video so I am going to
0:44
ship that part 2. we have already
0:47
covered the function codes one to four
0:49
and today we are going to cover the
0:55
we will cover them one by one starting
0:57
with a single coil then single register
1:00
and finally moving to the multi-coils
1:04
here is the project from the previous
1:06
video and today I will continue with the
1:16
let's delete this part as we don't need
1:18
to process the data sent by the slave
1:22
let me delete a few things here
1:28
all right we will start with writing a
1:30
single coil the function code 5.
1:36
the txt data format is going to be like
1:40
there are going to be a total eight
1:42
bytes with the data field consisting of
1:44
two bytes for the coil address and two
1:48
although we are writing a single coil
1:50
which is one bit in size we still need
1:53
to send two bytes of data
1:55
here is an example for writing a single
1:59
as mentioned here in order to set the
2:02
coil we need to send two bytes zero
2:05
cross FF followed by zero
2:08
and to reset the coil we send the two
2:10
bytes again but this time both should be
2:13
zero all right let's try this
2:16
we have the function code 5 to write a
2:21
I am keeping the coil address as zero
2:23
that means the actual coil address will
2:25
be 1. I want to set the coil at this
2:28
location so the data is going to be zero
2:31
cross FF followed by zero the CRC will
2:35
be calculated as usual and we have
2:38
totally eight bytes of the TX data
2:40
let's build and run this project
2:43
I am renaming the project to modbus
3:01
all right let's debug it now
3:08
we don't need this data array anymore
3:11
let's put a break point in the Callback
3:14
the slave will anyway send the response
3:17
so we would know when it does that
3:20
I have enabled the coil field in the
3:24
note that it does not have the function
3:26
code 5 so I am keeping the function code
3:29
1 with read or write enabled
3:32
the function code 1 is for reading the
3:34
coil data and along with it I have
3:37
enabled the right operation
3:39
with the right enabled the software can
3:41
accept the function code 5 so we are
3:44
good to go with this setup
3:46
I am resetting all the coils
3:49
note that the first coil is off
3:52
all right let's run the code now
3:56
we have hit the breakpoint
3:58
here is the TX data and we have the
4:01
slave ID the function code is 5 the coil
4:04
address is zero the data is zero cross
4:07
FF followed by zero and finally the CRC
4:12
if you notice the received data it is
4:15
exactly the same as the TX data
4:18
let's check the slave device
4:21
here you can see the first coil has been
4:27
similarly if we want to reset the coil
4:29
all we need to do is send the two bytes
4:44
now you can see the first coil has been
4:48
so this is how we can force a single
4:50
coil to either set or reset I hope this
4:55
now we will modify a single register but
4:58
let me comment out this code before that
5:10
now we are going to work with the
5:13
function code 6 which is writing a
5:17
here is an example for the same
5:20
the process is pretty much
5:21
straightforward where the master sends
5:23
the two bytes register address followed
5:26
by the two bytes of data
5:28
the function code is 6 this time
5:32
let's say I want to write to the
5:34
register at address 2 that is the
5:36
register at the address 403 and the two
5:40
bytes of data will be 0 cross 12 and 0
5:43
cross 34. we are again sending 8 bytes
5:47
in total so no need to modify anything
5:51
let's build and debug the code
5:54
here I have enabled the holding
5:56
registers in the slave software
5:59
I have enabled the read and write
6:01
operations so the function code 6 will
6:05
we are going to write this register at
6:07
40 0003 so let me first reset it to zero
6:11
all right we have hit the break point
6:14
you can see the TX data and RX data are
6:19
the slave has responded with the same
6:21
data that means the right must have been
6:25
let's check the slave software
6:28
here you can see the register does have
6:34
330 is the decimal equivalent of 0 cross
6:39
it is writing the lower byte first and
6:42
that is because I haven't checked this
6:45
I forgot to do that during recording and
6:48
now it will stay like this throughout
6:52
anyway here you can see the actual data
6:55
in hexadecimal format and it is the same
6:57
as what the master has sent
7:05
let's say we want to write another
7:07
register with the address at forty
7:17
the software has been timed out so I am
7:20
restarting it let's run the code now
7:24
here you can see the data has been
7:26
stored at the location forty thousand
7:29
so we were able to write a single coil
7:32
and a single register
7:34
now we will write multiple coils
7:44
the function code for writing multiple
7:47
coils is 15. as mentioned here we can
7:51
only write maximum 1968 coils at once
7:55
the txt data format for writing multiple
7:58
coils is going to be like this
8:00
the data field here consists of the
8:02
starting coil address the number of
8:04
coils we want to write the number of
8:06
bytes we will send to the slave and the
8:11
each coil is one bit in size so the
8:14
number of coils and number of bytes will
8:17
if we want to write up to 8 coils we can
8:20
send one byte of data
8:22
if we want to write up to 16 coils we
8:26
need to send two bytes of data
8:28
so even if we want to write nine coils
8:31
we still need to send two bytes
8:34
let's write the code now
8:36
here the function code is 15. let's keep
8:40
the coils address 3 which makes the
8:43
actual coil address as 4. this is the
8:46
starting coil address where the
8:50
in this example I am going to modify 15
8:55
the data for these 15 coils is going to
8:57
take 15 bits in total and therefore we
9:00
need to send two bytes
9:02
next we will send the data the first
9:05
byte we send will be used for the first
9:07
eight coils and the next byte will be
9:10
used for the next eight coils and so on
9:25
here is an example to write multiple
9:29
they are also sending two bytes of data
9:31
zero cross C D and 0 cross 0 1. you can
9:36
see how the first byte is used for the
9:38
coils at the address 20-27 and the next
9:41
byte is used for the remaining two coils
9:45
we need to modify the CRC part now
9:48
we have already stored up to the
9:50
position number eight so the CRC data
9:53
will be stored in the positions 9 and
9:55
10. before calculating the CRC the TX
9:59
data already has nine elements in it so
10:02
pass the appropriate value here
10:05
now our TX data buffer is ready we still
10:09
need to modify the size in the send data
10:15
all right everything is ready so build
10:26
here in the slave software all the coils
10:30
are in the reset state
10:32
I am only viewing 20 coils now
10:35
we have set the start coil at the
10:38
all right we have hit the break point
10:46
here you can see the coils have been
10:50
starting from coil 4 we have on off on
10:54
off on on off off off off on off on on
11:02
this is exactly what we sent
11:05
so the coils were modified as per the
11:07
data we sent and this part has worked
11:11
now the final part for this video will
11:14
be writing the multiple registers
11:19
the function code is 16 for writing the
11:22
multiple holding registers
11:25
we can write a maximum of 123 registers
11:30
the function code is 16 for writing
11:34
I am keeping the start address at zero
11:37
that means the actual address will be
11:41
let's say we want to write three
11:45
since each register is 16 bit in size
11:47
the total data for three registers will
11:50
be 48 bits which is 6 bytes
11:53
now we need to write 6 bytes of data
11:56
that we are going to send
11:58
the two bytes data for each register
12:00
with the high byte being sent first
12:06
so here I have prepared the six bytes to
12:09
be sent to the registers
12:11
let's modify the CRC again
12:14
txt data now has a total of 15 bytes and
12:18
we need to modify it in the send data
12:31
let's build and debug the code now
12:37
I am resetting all the registers to zero
12:46
all right let's run the code now
12:48
we have hit the break point means the
12:51
slave must have responded
12:53
you can see the register's forty
12:55
thousand one two and three have been
12:57
modified with the new data
13:00
the decimal format might not look very
13:02
correct but you can see the hexadecimal
13:06
the six bytes have been stored in the
13:11
the slave response consists of the slave
13:13
ID the function code the starting
13:16
register address the number of registers
13:18
that were written and finally the CRC
13:22
so we were able to write the data into
13:24
the holding registers
13:27
this is it for the video
13:29
we saw how to force a single coil how to
13:32
preset a single register and how to
13:34
write multiple coils and registrars
13:37
I hope you understood how the master
13:39
writes these data into the slave device
13:43
we will continue with the series and now
13:46
we will cover the stm32 as a slave where
13:49
it will respond to different queries
13:53
you can download the code from the link
13:57
keep watching and have a nice day ahead