MODBUS TCP Master Slave Logic - Cig-Ap129 - En-P
MODBUS TCP Master Slave Logic - Cig-Ap129 - En-P
MODBUS TCP Master Slave Logic - Cig-Ap129 - En-P
Purpose of the Document This application solution, and the associated RSLogix 5000 project
files, help you use Logix5000™ controllers as Modbus™ RTU masters
or slaves.
The sample RSLogix™ 5000 projects provided with this solution use
ControlLogix controllers. However, you can use the controller tags,
periodic tasks and user-defined data types (described later in this
document) with any Logix5000 controller.
Who Should Use This manual is intended for individuals who program applications that
use Logix5000 controllers on Modbus, such as:
This Document
• software engineers
• control engineers
• application engineers
• Logix5000 controllers
• RSLogix 5000
• Modbus RTU
Before You Begin Before you use this application solution, consider the following:
Table 1
Function code: Name: Data level: Description:
01 Read Coils Bit level This function code is used to read from 1 to 2000 contiguous status
of coils in a remote device. The coils in the response message are
packed as one coil per bit of the data field.
0 = OFF
1 = ON
02 Read Discrete Inputs This function code is used to read from 1 to 2000 contiguous status
of discrete inputs in a remote device. The discrete inputs in the
response message are packed as one input per bit of the data field.
0 = OFF
1 = ON
05 Write Single Coil This function code is used to write a single output to either ON or
OFF in a remote device.
0 = OFF
1 = ON
15 Write Multiple Coils This function code is used to force each coil in a sequence of coils to
either ON or OFF in a remote device.
0 = OFF
1 = ON
03 Read Holding Registers Word (16-bit) This function code is used to read the contents of a contiguous block
level of holding registers in a remote device.
04 Read Input Registers This function code is used to read from 1 up to 120 contiguous input
registers in a remote device.
06 Write Single Register This function code is used to write a single holding register in a
remote device.
16 Write Multiple Registers This function code is used to write from 1 up to 120 contiguous
registers in a remote device.
http://www.modbus.org
http://www.theautomationbookstore.com.
The RSLogix 5000 project files associated with this solution use
RSLogix 5000, version 13. To use these files with an RSLogix 5000
version previous to 13, you must use the Import/Export function.
• controller tags
• periodic tasks
• user-defined data types (in the ModbusMaster.ACD file only)
that you can copy into your RSLogix 5000 project to communicate
with the Logix5000 controllers via Modbus RTU function codes (e.g.
Read Coil Status).
Using Logix5000 Controllers The ModbusMaster.ACD file helps you use a Logix5000 controller as a
master on Modbus. The ACD file contains:
as Masters on Modbus
• 11 controller tags
• 1 periodic task
• 2 user-defined data types
• Mod_Command_Structure
• Mod_Status
from the ModbusMaster.ACD file. You can only copy one UDT
at a time, so repeat the process below for the second UDT.
C. Click Copy.
B. Click Paste.
A. Double-click on
Controller Tags in the
Modbus master file.
C. Highlight the 11
controller tags.
A. Double-click on
Controller Tags in
your project.
D. Right-click on the
bottom tag line.
E. Click Paste in
your project.
RSLogix 5000 keep the new tags together in your RSLogix 5000 project
because all 11 tags use a ‘Mod_’ prefix, as shown below.
Table 2
Tag name: Tag type: Description: Valid values:
Mod_Active BOOL This tag determines whether your 0 = Do not run the program (default)
project runs the new program in a
Logix5000 controller. 1 = Run the program
Table 2
Tag name: Tag type: Description: Valid values:
Mod_Commands[x].ScanNumber INT Determines at which scan the slave Any value from 0 - 12
executes the command element. For Default = 12
example, if you type a 3 in this tag,
the slave only executes this
command element on scan 3 (out of
10).
Table 2
Tag name: Tag type: Description: Valid values:
Mod_Commands[x].Starting INT This value is added to the address Application specific
Address offset (i.e., Mod_Commands[x].
AddressOffsetinMaster tag) to
determine the starting address
when the master reads or writes
data in its data table.
Table 2
Tag name: Tag type: Description: Valid values:
Mod_Echo_MaxTime DINT Maximum time (milliseconds) for We recommend you set this time
the master to wait for a echo from a greater than the time it takes to send
slave before determining the slave the longest MSG in your project.
node is missing.
Mod_Scan_Preset DINT Number of scans the master should 0 - 40
perform. We recommend at least 2.
After you have copied the new UDTs and controller tags into your
RSLogix 5000 project, you must copy the ModTask task, and its
corresponding Modbus_Interface program, to your RSLogix 5000
project.
A. Right-click on
ModTask.
B. Click Copy.
A. Right-click on Task.
B. Click Paste.
A. Right-click on
Modbus_Interface.
B. Click Copy.
A. Right-click on
ModTask.
B. Click Paste.
After you copy the new UDTs, controller tags, ModTask task and
Modbus_Interface program into your RSLogix 5000 project, configure
the communication port for the Logix5000 controller in your
RSLogix 5000 project.
A. Right-click on
your controller.
B. Click Properties.
B. Change the
Read/Write Buffer
Size to accommodate
your project.
C. Click OK.
Once the new controller tags are copied into your RSLogix 5000
project, you need to configure the tag described in Table 3:
Table 3
Before you configure the tags listed above, make sure you understand
how a Modbus master uses the tag configuration to read data from,
and write data to, its data table.
There are 4 tag arrays in Modbus master data tables, one for each of
the following:
Double-click on Controller
Tags in your project.
The sample screen below uses tags that were configured for the
example on page 19.
Finally, you must enable the new program before it will run. To
enable the new program, you must change the Mod_Active controller
tag to 1.
By default, this tag = 0 so you can load the program (using the steps
described in this document) to your RSLogix5000 project without the
program starting.
Once you copy everything from the Modbus Master.ACD file into your
RSLogix 5000 project, monitor your controller’s Main Task to make
sure the Modbus_Interface program is returning data correctly.
Using Logix5000 Controllers The ModbusSlave.ACD file helps you use a Logix5000 controller as a
slave on Modbus. The ACD file contains:
as Slaves on Modbus
• 10 controller tags
• 1 periodic task
A. Double-click on
Controller Tags.
B. Select the 10
controller tags.
A. Double-click on
Controller Tags.
D. Right-click on the
bottom tag line.
E. Click Paste.
RSLogix 5000 pastes the new tags into your RSLogix 5000 project and
keeps them together because all 10 tags use a ‘Mod_’ prefix, as
shown below.
Table 4
Table 4
After you copy the controller tags from the ModbusSlave.ACD file into
your RSLogix 5000 project, you must copy the ModTask task, and its
corresponding program, to your project as well.
A. Right-click on
ModTask.
B. Click Copy.
A. Right-click on Task.
B. Click Paste.
A. Right-click on
Modbus_Interface.
B. Click Copy.
A. Right-click on
ModTask.
B. Click Paste.
However, if you increase the size of the Modbus Data Register tags,
you must also change the size of the corresponding Modbus Register
Range tags that monitor them.
Table 5
If you change the size of this tag: You must change the size of this tag:
Mod_Data_Coils0 Mod_Range_Coils0
Mod_Data_Contacts1 Mod_Range_Contacts1
Mod_Data_InpReg3 Mod_Range_InpReg3
Mod_Data_HoldReg4 Mod_Range_HoldReg4
1. On the Edit Tags tab, highlight the number in the tag type field.
3. Press the Enter key to see the new size reflected in the tag.
3. Press the Enter key to see the new size reflected in the tag.
Finally, you must enable the new program before it will run. To
enable the new program, you must change the Mod_Active controller
tag to 1.
By default, this tag = 0 so you can load the program (using the steps
described in this document) to your RSLogix5000 project without the
program starting.
Once you copy everything from the ModbusSlave.ACD file into your
RSLogix 5000 project, monitor the controller’s Main Task to make sure
the Modbus_Slave program is returning data correctly.
• a command executed
• a command transmitted
• slaves answer correctly after receiving a master’s command
• slaves rejecting a command do so with an error code
or
Table 6
This Program Tag: available in this ACD file: has this description:
MsgCountRec ModbusMaster.ACD file Counts the number of messages received every 10 seconds.
• In the ModbusMaster.ACD file, this tag should closely
ModbusSlave.ACD file match the MsgCountTrans tag. If there is a
considerable difference, then some commands may
have been sent to a slave that does not exist or
cannot respond.
• In the ModbusSlave.ACD file, this tag may not closely
match the MsgCountTrans tag because the slave
receives all messages but only transmit when
addressed.
MsgCountTrans Counts the number of messages transmitted every
10 seconds.
• In the ModbusMaster.ACD file, this tag should closely
match the MsgCountRec tag. If there is a considerable
difference, then some commands may have been sent
to a slave that does not exist or cannot respond.
• In the ModbusSlave.ACD file, this tag may not closely
match the MsgCountRec tag; the slave receives all
messages but only transmit when addressed.
MsgTimeSpent Measures the time used for a transaction from the moment it
is sent by the master until an answer is received from the
slave. If the slave does not answer, this tag will match the
MsgTimeMax program tag.
Mod_Status[x].NodeAddress ModbusMaster.ACD file only This tag corresponds to the Mod_Commands[x].SlaveAddress
tag in the command sent from the master to the slave.
Table 6
This Program Tag: available in this ACD file: has this description:
Mod_Status[x].FunctionCode ModbusMaster.ACD file only This tag corresponds to the Mod_Commands[x].FunctionCode
tag in the command sent from the master to the slave.
Publication CIG-AP129A-EN-P - February 2004 36 Copyright © 2004 Rockwell Automation, Inc. All rights reserved. Printed in the U.S.A.