Modbus Example Program
Modbus Example Program
Please note that A1B1/A2B2 can not be both the master station and the slave station
MODBUS RTU sets master station RS485-1, D8400-A1B1 port, 9600 8 1 NONE (communication parameters
can be modified at will). The program needs to call ADPRW by polling
M8002
0 [ MOV H3081 D8400 ]
Select string 1
as MODBUS
protocol, 9600,
8,1,N
[ MOV K0 D8401 ]
Select MODBUS
RTU as the
master station
[ MOV K3 D8412 ]
Timeout retries
3 times
[ RST M8410 ]
OFF: ADPRW
sends and
receives data
for serial port 1
[ SET M0 ]
Start serial data
1 transmission
[ SET M10 ]
Restart when
communication
is interrupted
Note that when A1B1 RS485-1 is used as the master station: D8401 is K0, M8410 is 0
30 [ ADPRW K1 K3 K0 K1 D1 ]
Start serial D0 of Station 1
data 1
transmission M8029
[ RST M0 ]
Reset serial
data 1
transmission
[ SET M1 ]
Start serial
data 2
transmission
45 [ ADPRW K2 K0 K0 K1 D2 ]
Start serial D0 of Station 2
data 2
transmission M8029
[ RST M1 ]
Reset serial
data 2
transmission
[ SET M2 ]
Start serial
data 3
transmission
60 [ ADPRW K1 K6 K1 K1 D3 ]
Start serial master station
data 3
transmission M8029
[ RST M2 ]
Reset serial
data 3
transmission
[ SET M3 ]
Start serial
data 4
transmission
M10 K50
91 [ T0 ]
Restart when Delay start time
communication of communication
is interrupted T0 interruption
[ SET M0 ]
Start serial
data 1
transmission
[ RST M10 ]
Restart when
communication
is interrupted
[ MOV K0 D8421 ]
Select MODBUS
RTU as the
master station
[ MOV K3 D8432 ]
Timeout retries
3 times
[ SET M8410 ]
ON: ADPRW sends
and receives data
for serial port 2
[ SET M10 ]
Restart when
communication
is interrupted
[ SET M0 ]
Start serial
data 1
transmission
Note that when A2B2 RS485-2 is used as the master station: D8421 is K0, M8410 is 1
30 [ ADPRW K1 K3 K0 K1 D1 ]
D0 of Station 1
Start serial
data 1
transmission M8029
[ RST M0 ]
Reset serial
data 1
transmission
[ SET M1 ]
Start serial
data 2
transmission
45 [ ADPRW K2 K0 K0 K1 D2 ]
D0 of Station 2
Start serial
data 2
transmission M8029
[ RST M1 ]
Reset serial
data 2
transmission
[ SET M2 ]
Start serial
data 3
transmission
60 [ ADPRW K1 K6 K1 K1 D3 ]
Start serial Master station
data 3
transmission M8029
[ RST M2 ]
Reset serial
data 3
transmission
[ SET M3 ]
Start serial
data 4
transmission
M10 K50
91 [ T0 ]
Restart when Delay start time
communication of communication
is interrupted T0 interruption
[ SET M0 ]
Start serial
data 1
transmission
[ RST M10 ]
Restart when
communication
is interrupted
ADPRW S1 S2 S3 S4 S5
The same command ADPRW is used for writing and reading communication with the slave station.
S1: Slave station No
S2: Function code
S3: Slave variable starting address
S4: Number of read/write variables
S5: Read/write of master station, starting address of word or bit
For example:
Example 1
[ ADPRW K1 K3 K0 K1 D1 ]
Read D0 of station 1 and transmit it to D1 of master station
Read only one variable D0 of No. 1 slave station to D1 of master station with H03 command
Example 2
[ ADPRW K1 K3 K0 K5 D1 ]
Read D0-D4 of station 1 and transmit it to D1-D5 of master station
Read only the five variables D0-D4 of No. 1 slave station with the H03 command and transfer them to
D1-D5 of the master station
[ MOV K1 D8414 ]
Slave station: 1 station
[ MOV K1 D8434 ]
Slave station: 1 station