0:09
hello and welcome to controllers Tech
0:13
this is the sixth video in the stm32
0:16
modbus series and today we will see how
0:19
the master will modify a single and
0:21
multiple registers 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 reading coils and
0:35
in today's video the master will send a
0:38
request to modify the register values in
0:40
the stm32 and we will program the stm32
0:44
to handle this request
0:46
basically we will be working with the
0:48
function codes 6 and 16 but from the
0:53
the function code 6 is to write a single
0:56
register where the master can write only
0:58
one memory word that is 16 bits
1:02
the function code 16 is to write the
1:04
multiple registers but the master can
1:07
request a maximum of 123 registers
1:11
I am going to continue the same project
1:13
from the previous video and I have
1:18
here in the main file I have added two
1:22
in case of the function code 6 we will
1:25
call the function write single register
1:27
and for the function code 16 we will
1:30
call write holding registers
1:33
these functions are defined in the
1:40
let's first discuss the single register
1:48
this is the format of the request sent
1:52
it contains the slave ID the function
1:55
code the address of the register the two
1:57
bytes of data Master wants to write and
2:04
in the function we will first extract
2:07
the address of the register
2:09
if the address is more than 49 we will
2:12
send an exception regarding illegal data
2:16
this is because we have only defined a
2:18
total of 50 holding registers in the
2:20
database whose addresses vary from 0 to
2:25
if there is no exception we will modify
2:27
the database with the new data
2:30
the registers are 16-bit in size and
2:33
therefore the data arrive in two bytes
2:35
with higher byte being sent first
2:38
we will combine the two bytes to make a
2:40
16-bit value and store it in the
2:43
register's position in the database
2:46
now comes the response
2:48
in case of single register the slave
2:51
response is exactly same as the Masters
2:55
so here we will simply copy the data
2:57
from the RX buffer into the TX buffer
3:01
finally send the data the CRC will be
3:04
calculated in the send data function
3:09
now let's see the function to write
3:13
once this function is called we will
3:16
first find the address of the start
3:20
then find the number of registers
3:22
requested by the master
3:24
if the number of registers is more than
3:27
123 we will send an exception
3:30
this is as per the modbus standard that
3:33
we saw in the beginning of the video
3:35
then calculate the address of the last
3:37
register requested by the master
3:40
since we have the database for only 50
3:42
registers if the end address is more
3:45
than 49 we will send another exception
3:48
here I have defined an index variable to
3:51
keep track of the bytes in the RX data
3:55
next we will copy the data from the RX
3:58
buffer into the database
4:01
the data in the holding registers is
4:03
16-bit in size so we will combine the
4:06
two bytes from the RX buffer after each
4:09
value has been saved we will increment
4:11
the start byte so that the next value
4:14
can be stored at the next position the
4:17
loop will repeat as many times as the
4:19
number of registers requested by the
4:22
now the slave will send a response to
4:26
the format is shown on the top right
4:29
corner we have the slave ID the function
4:32
code the start register address and the
4:37
finally we will call the send data
4:40
function to send the response
4:42
the CRC will be calculated in the
4:46
let's build and debug the code now
4:52
I have added the holding registers
4:54
database in the live expression
4:56
this is because the values are not going
4:59
to change here but you can see the
5:01
updated values in the live expression
5:03
in the master software we need to open a
5:06
new window to perform the right
5:09
here we have the same configuration that
5:12
we have been using in all the videos so
5:15
let's start by testing the single
5:18
register let's say the master wants to
5:21
write the register at the address 405.
5:24
since it is writing only one register
5:27
the function code is set to 6. here you
5:31
can set the value for the register
5:33
the current value at forty thousand five
5:36
is four thousand four hundred and forty
5:39
and the master wants to update it to one
5:44
here you can see the query prepared by
5:47
the master based on the above setup
5:50
all right the slave sent exactly the
5:52
same response to the master and here you
5:55
can see the data has been updated
5:57
let's try another address
5:59
this is the register at forty thousand
6:03
and the master wants to write 10 000
6:08
you can see the value has been updated
6:11
this change is not going to reflect in
6:14
the database values so you must see it
6:16
in the live expression
6:18
so writing the single registers working
6:22
now let's move to multiple registers
6:25
this time the master wants to write four
6:27
registers starting from 21. you can see
6:31
the function code has been changed to
6:34
16. let's update the register values
6:42
you can see the four registers have been
6:44
updated with new values
6:49
I also tried writing the decimal values
6:52
but since the data is unsigned 16 bit
6:55
the values were rounded off to the
6:59
now let's see some exceptions
7:01
we know the master can request a maximum
7:04
of 123 registers so if it try to request
7:08
124 of them the slave will send an
7:11
exception saying illegal data value
7:14
similarly the database only contains 50
7:17
registers so if the master want to write
7:20
more than that the slave will send an
7:22
exception saying illegal data address
7:25
although it can write the 50th register
7:27
since it's defined in the database
7:37
now let's quickly see if the values we
7:39
wrote are actually readable
7:42
here the master is going to send a read
7:44
request for the register at address 50.
7:47
the function code is set to 3.
7:50
here you can see the updated data that
7:53
we just forwarded that address
7:56
let's read these four registers starting
8:01
let me change these to unsigned integers
8:05
here you can see the updated values of
8:09
so I hope you understood how to perform
8:11
write operations as a slave device
8:14
we will continue with the series and in
8:17
the next video I will cover writing
8:21
that will be the last video of this
8:23
series for the time being
8:26
you can download the code from the link
8:30
leave comments in case of any doubt
8:33
keep watching and have a nice day ahead