Professional Documents
Culture Documents
This document describes the application and use of the Modbus TCP Master Sample
Application. The program was written to run on a Control Logix 5000 processor with
a compatible Ethernet Module.
This sample application can be used standalone by adding any required logic for your application, or can
be added to an existing application following the directions outlined below.
The audience for this document would be Software engineers, controls engineers, and application
engineers familiar with Logix 5000 controllers, RSLogix 5000, and Modbus TCP protocol and its uses.
Knowledge of the use of the Modbus protocol is crucial to using this application.
This application will allow you to create a Modbus TCP Master capable of connecting to multiple Modbus
TCP compatible slaves(s) to read and write control data. There is also a Modbus TCP Slave Sample
application if a slave is desired, i.e. you need to be connected to by a Master.
This application is provided as is and there is no Warranty or implied merchantability.
See the Rockwell Sample Code Download Terms and Conditions.pdf that was downloaded with this
document for more information on Acceptance of Terms and Conditions. Your rights to download and use this
content are limited and can be revoked at any time. No support contract is implied or delivered with this code
or any modified version of this code.
.
Name:
01
Read Coils
02
Read
Discrete
Inputs
05
Write
Single Coil
15
Write
Multiple
Coils
03
Read
Holding
Registers
04
Read Input
Registers
06
Write
Single
Register
Write
Multiple
Registers
16
Data
level:
Bit level
Word
(16-bit)
level
Description:
This function code is used to read from 1 to 2000
contiguous status of coils in a remote device. The
application as configured has a maximum of 256. The
coils in the response message are packed as one coil
per bit of the data field.
0 = OFF
1 = ON
This function code is used to read from 1 to 2000
contiguous status of discrete inputs in a remote device.
The application as configured has a maximum of 256.
The discrete inputs in the response message are
packed as one input per bit of the data field.
0 = OFF
1 = ON
This function code is used to write a single output to
either ON or
OFF in a remote device.
0 = OFF
1 = ON
This function code is used to force each coil in a
sequence of coils to either ON or OFF in a remote
device. The protocol supports a maximum count of
2000. The application as configured has a maximum of
256.
0 = OFF
1 = ON
This function code is used to read the contents of a
contiguous block of holding registers in a remote
device. The protocol has a maximum count of 125.
The application as configured has a maximum of 120.
This function code is used to read from 1 up to 125
contiguous input registers in a remote device. The
application as configured has a maximum of 120.
This function code is used to write a single holding
register in a
remote device
This function code is used to write from 1 up to 125
contiguous registers in a remote device. The
application as configured has a maximum of 120.
42 Controller tags
Note that these limitations can be changed by the user modifying the application.
You will need to open two instances of RSLogix 5000, on with the Modbus Master application the
other with the application you wish to integrate it into. In the Modbus TCP Project, right click on the
PROGRAM FOLDER and select export program as shown to create an L5X file
"ModbusMasterTCP.L5X.
Now in the application you are integrating into, right click on a task and select program import as
shown to import "ModbusMasterTCP.L5X" as shown.
NOTE: It is recommend to import into a periodic task of 50 to 100ms to insure proper operation. Slow
or faster speeds have not been tested.
Tag name:
MBTI_Buf_Len_Sent
Tag type:
DINT
MBTI_Connect_Sock_MSG_00
through
MBTI_Connect_Sock_MSG_03
MBTI_Create_Sock_MSG_00
through
MBTI_Create_Sock_MSG_03
MBTI_Delete_All_MSG
MESSAGE
MBTI_Delete_Socket_MSG_00
through
MBTI_Delete_Socket_MSG_03
MBTI_DestAddress_00 through
MBTI_DestAddress_03
MESSAGE
OpenConnParams
MBTI_Instance
DINT[4]
MBTI_Read_Data_MSG_00
through
MBTI_Read_Data_MSG_03
MBTI_Read_Data_Req
MBTI_Read_Resp
MBTI_Write_Data_MSG_00
through
MBTI_Write_Data_MSG_03
MBTI_Write_Data_Out
MESSAGE
MBTU_Connections
MBConnection[4]
MESSAGE
MESSAGE
READ_DATA_REQ[20]
READ_RESP_STR[20]
MESSAGE
WRT_DATA[20]
Description:
Number of bytes sent
to a device on a write
function.
Messages blocks for
the connections
Valid Values:
Internal buffer.
Internal buffer.
Message blocks to
write data to the
connections
Arrays for the
outgoing messages
for each transaction
Connection
configuration and
status object. Used
to set the destination
address and enable
Do not directly
modify this value.
Instead change
the address in the
MBTIConnections
tag.
MBTU_EnetModulePort
String
MBTU_EnMBTCP
BOOL
MBTU_MB_0xx
BOOL[1024]
MBTU_MB_1xx
BOOL[1024]
MBTU_MB_3xx
INT[256]
MBTU_MB_4xx
INT[256]
MBTU_SocketParam
REQUEST_PARAMET
ERS
MBTU_Transactions_00
through
MBTU_Transactions_04
MBTransactions[5]
* See below
explanation.
Clearing and
setting this bit will
reset the entire
communications
application.
Source for writes
dest. for reads.
See description
below for how to
populate the
UDT.
* MBTU_EnetModulePort this value should be set to '$01$00' for all CompactLogix 5370
processors. This value should be set to '$01$xx where xx is the 1756 backplane slot number, in hex,
of the desired Ethernet module for all 1756 ControlLogix processors, for example '$01$02' for an
Ethernet module located in 1756 backplane slot 2 or '$01$10' for an Ethernet module located in 1756
backplane slot 16.
It is recommended to leave all MSG instructions as configured. All MSG instructions are currently
configured as unconnected See Knowledgebase ID 530345 for information on connected vs.
unconnected messaging.
Configuration Tags
(note: tags starting with MBT are controller scoped, all others are program scoped).
Tag Name
MBTU_EnetModulePort
MBTU_Connections
NumberOfConnection
MBTU_Socket_Param
MBTU_Transactions_00
through
MBTU_Transactions_03
MBTU_EnMBTCP
Description
Path to the Ethernet port being used. This can either be the
port name or the numeric path i.e. $01$01 See below. *
Each element is used to enable the corresponding connection
that is configured. By default there are four connections
configured, but only the first is enabled for use.
Set to the number of connections configured. Do not confuse
with the number enabled, this is the amount the application is
able to handle. The default value is 4 for four connections.
A structure used for creating the socket. Settings include the
port address (default 502 for Modbus TCP Protocol) that the
connections will be made to.
Array of UDT MBTransaction. These are used to define the
read and writes transactions done on each connection. See
the description following on how to configure and use a
transaction.
Master enable bit used to turn the driver on and off. Turning off
will close any connections that were open.
*MBTU_EnetModulePort this value should be set to '$01$00' for all CompactLogix 5370
processors. This value should be set to '$01$xx where xx is the 1756 backplane slot number, in
hex, of the desired Ethernet module for all 1756 ControlLogix processors, for example '$01,$02' for
an Ethernet module located in 1756 backplane slot 2 or '$01,$10' for an Ethernet module located in
1756 backplane slot 16.
Refer to your Ethernet module documentation to configure you Ethernet module. At minimum you
should configure the address and subnet mask or select DHCP.
Configuring a connection:
Each connection is configured using its element in the MBTU_Connections tag. Below is a
description of the values in the MBCOnnection UDT. The value that should be configured prior
to connection is the MBTU_DestAddress.
Tag Name
MBTU_DestAddress
MBTU_Enable
MBTI_NumberOfTransactions
MBTI_Connected
MBTI_ConnLastError
MBConnection
UDT
Description
A string containing the IP address and port to make a
connection to of the form of xxx.xxx.xxx.xxx?port=502. Notice
that port 502 is the default setting for ModbusTCP
communications. Note that port must be in all lower case!
A Boolean that enables and disables this single connection.
States the size of the Transactions array for this connection
minus one. This is basically used as the terminating value for
FOR instructions, therefore the minus 1 value.
A Bool value indicating a connection exists or no connection
exists.
This value is set on various connection level errors when they
occur. A 0 represents all is well on an enabled connection.
Other values are outlined below and covered in the debugging
section. Other errors that can exist for a single transaction are
written to the Transactions UDT.
Note if your changing your MBTU_DestAddress while running, you will need to disable your
connection (enable bit in the connection UDT) wait about 30 seconds, then re-enable the
connection to switch to the new address.
To configure a transaction:
You will need to set the following values.
Enabled
set to one to enable this transaction
PollInterval
set to something 1 or higher. Any other value will disable
the transaction.
Transtype
1, 2, 3, 4, 5, 6, 15, or 16. The read or write Modbus
transaction you wish performed.
UID (possibly)
Typically used if the destination address is a gateway with
more than one device on the other side, or if a particular device
uses a UID. If this is the case enter the devices UID.
BeginAddress
The starting address of the operation minus 1 without the first
digit type identifier. For example, address 40001 in the PAC will be 0
here.
Count
Number of items to read or write (either registers or discrete,
whichever applies).
LocalOffset
This is the offset into the local data arrays (MBTU_MB_Xxx)
at which to begin the data for this operation.
After these have been set, clearing the ReqBuilt bit will build your new transaction string. Also for
write operations, the request string is automatically built on every transmission so the data changes
will be reflected.
To use a transaction:
In the case of reads, checking the Transcomplete and TransStat will let you know the validity of the
transaction and its data. The data can be read from MBTU_MB_0xx for coil operations,
MBTU_MB_1xx for discrete input reads, MBTU_MB_3xx for input register reads, and
MBTU_MB_4xx for holding register operations. Data will always begin at the transactions specified
local offset in those arrays. The TransLastError value will indicate any errors, either local or
Modbus Exceptions received. A 0 here indicates successful transactions, any other values can be
found in the following section on error codes.
Transaction Examples
The following sections will give examples and explanations of each of the supported transaction
types. It should be noted that some devices will require a matching value in the Unit ID (UID) field of
the transactions.
The following tables show the key values in the Transaction structures for each transaction. The
description following the table describes what will happen.
Read Coils
Tag Name
PollInterval
TransType
BeginAddress
Count
LocalOffset
Value
10
1
0
10
0
th
This transaction will poll every 10 time the poll timer times out, which in the case of a 100ms poll
timer, would mean this transaction is done every 1000 milliseconds or 1 second. The TransType is 1
meaning a coil read is going to be done. Begin address is 0, means we will start reading at address
00001 of the device being read. The count is 10, meaning 10 coils will be read and a LocalOffset of
0 will put the data starting at the first element (index 0) of MBTU_MB_0xx and continue on to index 9.
Read Discrete Inputs
Tag Name
PollInterval
TransType
BeginAddress
Count
LocalOffset
Value
20
2
9
10
10
th
This transaction will poll every 20 time the poll timer times out, which in the case of a 100ms poll
timer, would mean this transaction is done every 2 second. The TransType is 2 meaning a discrete
input read is going to be done. Begin address is 9, means we will start reading at address 10010 of
the device being read. The count is 10, meaning 10 coils will be read and with a LocalOffset of 10
the data will be placed starting at index 10 of MBTU_MB_1xx and continue on to index 19.
Read Holding Registers
Tag Name
PollInterval
TransType
BeginAddress
Count
LocalOffset
th
Value
15
3
99
25
100
This transaction will poll every 15 time the poll timer times out, which in the case of a 100ms poll
timer, would mean this transaction is done every 1.5 second. The TransType is 3 meaning a holding
register read is going to be done. Begin address is 99, means we will start reading at address 40100
of the device being read. The count is 25, meaning 25 16 bit holding registers will be read and with a
LocalOffset of 100 the data will be placed at indexes 100 through 124 of MBTU_MB_4xx.
Value
20
4
4
10
5
th
This transaction will poll every 20 time the poll timer times out, which in the case of a 100ms poll
timer, would mean this transaction is done every 2 second. The TransType is 4 meaning an input
register read is going to be done. Begin address is 4, means we will start reading at address 30005
of the device being read. The count is 10, meaning 10 16 bit input registers will be read and with a
LocalOffset of 5, the data will be placed in index 5 through 14 of MBTU_MB_3xx.
Write Single Coil
Tag Name
PollInterval
TransType
BeginAddress
Count
LocalOffset
Value
2
5
24
NA
25
This transaction will poll every 2nd time the poll timer times out, which in the case of a 100ms poll
timer, this transaction is done 5 times a second. The TransType is 5 meaning a coil write is going to
be done. Begin address is 24, means we will write to address 00025 of the device being written to.
The count isnt applicable for a TransType of 5. A LocalOffset of 25 means the data will come from
index 25 of MBTU_MB_0xx.
Also keep in mind that since this is a write operation, every time you change the data to be written
you must clear the ReqBuilt flag in this transaction definition so that the request will be rebuilt with the
new data in it.
Write Single Register
Tag Name
PollInterval
TransType
BeginAddress
Count
LocalOffset
Value
5
6
9
NA
10
th
This transaction will poll every 5 time the poll timer times out, which in the case of a 100ms poll
timer, would mean this transaction is done twice a second. The TransType is 6 meaning it will be
writing a single register. Begin address is 9, means we will write to address 40010 of the device
being written to. The count does not apply to a TransType of 6. A LocalOffset of 10 the data will
come from index 10 of MBTU_MB_4xx.
Also keep in mind that since this is a write operation, every time you change the data to be written
you must clear the ReqBuilt flag in this transaction definition so that the request will be rebuilt with the
new data in it.
Value
10
15
99
25
100
th
This transaction will poll every 10 time the poll timer times out, which in the case of a 100ms poll
timer, would mean this transaction is done every second. The TransType is 15 meaning it will be
writing multiple coils. Begin address is 99, means we will start writing at address 00100 of the device
being written to. The count is 25, meaning 25 single bit coils will be written and with a LocalOffset of
100 the data will come from indexes 100 through 124 of MBTU_MB_0xx.
Also keep in mind that since this is a write operation, every time you change the data to be written
you must clear the ReqBuilt flag in this transaction definition so that the request will be rebuilt with the
new data in it.
Write Multiple Holding Registers
Tag Name
PollInterval
TransType
BeginAddress
Count
LocalOffset
Value
30
16
49
10
50
th
This transaction will poll every 30 time the poll timer times out, which in the case of a 100ms poll
timer, would mean this transaction is done every 3 seconds. The TransType is 16 meaning it will be
writing multiple registers. Begin address is 49, means we will start writing at address 40050 of the
device being written to. The count is 25, meaning 25 registers will be written, and with a LocalOffset
of 50 the data will come from indexes 50 through 59 of MBTU_MB_4xx.
Also keep in mind that since this is a write operation, every time you change the data to be written
you must clear the ReqBuilt flag in this transaction definition so that the request will be rebuilt with the
new data in it.
Retry Timers
Each connection has a retry timer which is used to re-establish communications in the event a
connection is lost or errors out. The timers are named Retry_Timers[] and their default setting is 10
seconds. You can modify these to suite your needs and network environment.
TransType
UID
BeginAddress
Count
Notes/Value restrictions
0 disabled
1 - enabled
If the root poll interval is 0.1
seconds and you wish to run
this transaction every 0.3
seconds, this value would be
set to 3. The poll rate should
not be faster than the time
the remote device can
answer or error -15 will
occur on the connection.
1 Read Coils
2 Read Discrete Inputs
3 Read Holding Registers
4 Read Input Register
5 Write Single Coil
6 Write single Register
15 Write Multiple Coils
16 Write Multiple Registers
This value should be set to the
UID of the device connecting
to. If connecting through a
gateway, this value will be the
UID of the device on the other
side of the gateway.
Address 0001 is 0
10001 is 0
30001 is 0
40001 is 0.
LocalOffset
TransactionComplete
TransStat
Request
ReqBuilt
TransID
TransLastError
Read Only
Read Only.
Transtat values
Transstat values give you a status of the current transaction.
-1 Request was sent but no reply has been received yet.
0 The request was sent and a response with no exception was received.
0
-1
-2
-3
-4
-5
-6
-7
-8
-9
-10
-11
-12
-13
-15
01
02
03
04
05
06
08
0A
0B
-7
Check the Delete all message in rung 2 of the MainRoutine. Most likely cause is the
MBTU_EnetModulePort being incorrectly set.
-8
An error occurred in the Create socket block for that connection. Check the corresponding MSG
blocks error and extended error values in the MainRoutine to see what the issue is. For
example for the first connection, check the MSG block in rung 4 of the main routine. Error values
are listed above for reference.
-9
An error occurred in the Connect socket block for that connection. Check the corresponding MSG
blocks error and extended error values in the MainRoutine to see what the issue is. For example
for the first connection, check the MSG block in rung 6 of the main routine.
Most likely causes of this error is an incorrectly specified address, or the remote device not
being accessible.
-10
A disconnect has occurred. Examine the TransLastError values in the individual transactions for
that connection for additional information.
A write block error has occurred, check the corresponding write MSG instruction for
additional information in the Error and Extended Error values. Most likely cause is a
dropped connection or loss of network connectivity.
-4 & -5
A Read block error has occurred, check the corresponding read MSG instruction for
additional information in the Error and Extended Error values. Most likely cause is a
dropped connection or loss of network connectivity.
References
M ODBUS PROTOCOL SPECIFICATION V1.1B A
Available from http://www.Modbus.org.
literature/.
Revision History
August 31, 2012
ModbusTCP_Master_R100.ACD initial release
October 17, 2012
Updated Master documentation, page 13 incorrectly stated:
'meaning 25 32 bit holding registers' changed to
'meaning 25 16 bit holding registers'
No ladder changes were made.
Document renamed Modbus TCP Master Sample Application R101.
April 5, 2013
Document updated and renamed Modbus TCP Master Sample Application R102.
ACD modified and renamed, ModbusTCP_Master_R102.ACD, _SUB_SET_PATH added
four COP instructions to resolve an issue of not all MSG paths being properly updated.
Minor documentation/comment changes made to ACD.
Modified the Master documentation in the Using the application section, added
information on the amount of controller memory needed.
Modified the Master documentation in the Sample Files in RSLogix 5000 V20 format
section, added KB ID 470365 reference with detailed information on supported controllers.
Modified the Master documentation in the New Modbus Master Controller Tags section,
added KB ID 530345 reference with information on connected vs. unconnected MSGs.
Modified the Master documentation in the description section pg. 1 to enhance the As Is No
Warranty section.
Copyright 2013 Rockwell Automation, Inc. All Rights Reserved. Printed in 2013.