Modbus 通信協定01
Modbus 通信協定01
0b Modbus Organization
CONTENTS
1 INTRODUCTION ........................................................................................................... 2
1.1 OBJECTIVES ....................................................................................................... 2
1.2 CLIENT / SERVER MODEL................................................................................... 2
1.3 REFERENCE DOCUMENTS ................................................................................. 3
2 ABBREVIATIONS .......................................................................................................... 3
3 CONTEXT ..................................................................................................................... 3
3.1 PROTOCOL DESCRIPTION ................................................................................. 3
3.1.1 General communication architecture ......................................................... 3
3.1.2 MODBUS On TCP/IP Application Data Unit ............................................... 4
3.1.3 MBAP Header description ......................................................................... 5
3.2 MODBUS FUNCTIONS CODES DESCRIPTION .................................................... 6
4 FUNCTIONAL DESCRIPTION........................................................................................ 7
4.1 MODBUS COMPONENT ARCHITECTURE MODEL ............................................... 7
4.2 TCP CONNECTION MANAGEMENT ....................................................................10
4.2.1 Connections management Module ............................................................10
4.2.2 Impact of Operating Modes on the TCP Connection ..................................13
4.2.3 Access Control Module ............................................................................14
4.3 USE of TCP/IP STACK ........................................................................................14
4.3.1 Use of BSD Socket interface ....................................................................15
4.3.2 TCP layer parameterization .....................................................................18
4.3.3 IP layer parameterization .........................................................................19
4.4 COMMUNICATION APPLICATION LAYER ...........................................................20
4.4.1 MODBUS Client .......................................................................................20
4.4.2 MODBUS Server ......................................................................................26
5 IMPLEMENTATION GUIDELINE ...................................................................................32
5.1 OBJECT MODEL DIAGRAM ................................................................................32
5.1.1 TCP management package ......................................................................33
5.1.2 Configuration layer package .....................................................................35
5.1.3 Communication layer package..................................................................36
5.1.4 Interface classes......................................................................................37
5.2 IMPLEMENTATION CLASS DIAGRAM .................................................................37
5.3 SEQUENCE DIAGRAMS......................................................................................39
5.4 CLASSES AND METHODS DESCRIPTION ..........................................................42
5.4.1 MODBUS Server Class ............................................................................42
5.4.2 MODBUS Client Class..............................................................................43
5.4.3 Interface Classes .....................................................................................44
5.4.4 Connexion Management class ..................................................................45
1 INTRODUCTION
1.1 OBJECTIVES
The objective of this document is to present the MODBUS messaging service over
TCP/IP , in order to provide reference information that helps software developers to
implement this service. The encoding of the MODBUS function codes is not described
in this document, for this information please read the MODBUS Application Protocol
Specification [1].
• MODBUS Request,
• MODBUS Confirmation,
• MODBUS Indication,
• MODBUS Response
Request Indication
A MODBUS Request is the message sent on the network by the Client to initiate a
transaction,
The MODBUS messaging services (Client / Server Model) are used for real time
information exchange:
-
1.3 REFERENCE DOCUMENTS
This section gives a list of documents that are interesting to read before this one:
2 ABBREVIATIONS
ADU Application Data Unit
IETF Internet Engineering Task Force
IP Internet Protocol
MAC Medium Access Control
MB MODBUS
MBAP MODBUS Application Protocol
PDU Protocol Data Unit
PLC Programmable Logic Controller
TCP Transport Control Protocol
BSD Berkeley Software Distribution
MSL Maximum Segment Lifetime
3 CONTEXT
A communicating system over MODBUS TCP/IP may include different types of device:
MODBUS
Client
Serial Line
MODBU MODBU
S Client S Client
TCP/IP TCP/IP Client
TCP/IP
MODBUS
MODBUS MODBUS
MODBU MODBU
Server Server
S Server S Server
Serial Line Serial Line
TCP/IP TCP/IP
Server TCP/IP
gateway
MODBUS Serial
The MODBUS protocol defines a simple Protocol Data Unit (PDU) independent of the
underlying communication layers. The mapping of MODBUS protocol on specific buses
or networks can introduce some additional fields on the Application Data Unit (ADU).
ADU
Additional address Function code Data Error check
PDU
The client that initiates a MODBUS transaction builds the MODBUS Application Data
Unit. The function code indicates to the server which kind of action to perform.
PDU
A dedicated header is used on TCP/IP to identify the MODBUS Application Data Unit. It
is called the MBAP header (MODBUS Application Protocol header).
October 24, 2006 http://www.Modbus.org 4/46
MODBUS Messaging on TCP/IP Implementation Guide V1.0b Modbus Organization
This header provides some differences compared to the MODBUS RTU application data
unit used on serial line:
The MODBUS ‘slave address’ field usually used on MODBUS Serial Line is
replaced by a single byte ‘Unit Identifier’ within the MBAP Header. The
‘Unit Identifier’ is used to communicate via devices such as bridges,
routers and gateways that use a single IP address to support multiple
independent MODBUS end units.
All MODBUS requests and responses are designed in such a way that the
recipient can verify that a message is finished. For function codes where
the MODBUS PDU has a fixed length, the function code alone is sufficient.
For function codes carrying a variable amount of data in the request or
response, the data field includes a byte count.
When MODBUS is carried over TCP, additional length information is
carried in the MBAP header to allow the recipient to recognize message
boundaries even if the message has been split into multiple packets for
transmission. The existence of explicit and implicit length rules, and use of
a CRC-32 error check code (on Ethernet) results in an infinitesimal chance
of undetected corruption to a request or response message.
• Transaction Identifier - It is used for transaction pairing, the MODBUS server copies
in the response the transaction identifier of the request.
• Length - The length field is a byte count of the following fields, including the Unit
Identifier and data fields.
• Unit Identifier – This field is used for intra-system routing purpose. It is typically
used to communicate to a MODBUS+ or a MODBUS serial line slave through a
gateway between an Ethernet TCP-IP network and a MODBUS serial line. This field is
set by the MODBUS Client in the request and must be returned with the same value in
the response by the server.
All MODBUS/TCP ADU are sent via TCP to registered port 502.
4 FUNCTIONAL DESCRIPTION
The MODBUS Component Architecture presented here is a general model including
both MODBUS Client and Server Components and usable on any device.
Some devices may only provide the server or the client component.
In the first part of this section a brief overview of the MODBUS messaging service
component architecture is given, followed by a description of each component
presented in the architectural model.
USER
APPLICATION
Layer
Ressource Management
TCP
Management
Stack Connection
Access Ctl
parmeterization Management
TCP/IP Stack
A MODBUS backend interface can be provided allowing indirectly the access to user
application objects.
Four areas can compose this interface: input discrete, output discrete (coils), input
registers and output registers. A pre-mapping between this interface and the user
application data has to be done (local issue).
R
Input Registers Input Registers
W
Output Registers Output Registers
Figure 5 MODBUS Data Model with Figure 6 MODBUS Data Model with only
separate blocks 1 block
MODBUS Client
The MODBUS Client allows the user application to explicitly control information
exchange with a remote device. The MODBUS Client builds a MODBUS request from
parameter contained in a demand sent by the user application to the MODBUS Client
Interface.
The MODBUS Client uses a MODBUS transaction whose management includes waiting
for and processing of a MODBUS confirmation.
The MODBUS Client Interface provides an interface enabling the user application to
build the requests for various MODBUS services including access to MODBUS
application objects. The MODBUS Client interface (API) is not part of this
Specification, although an example is described in the implementation model.
MODBUS Server
The MODBUS Backend Interface is an interface from the MODBUS Server to the user
application in which the application objects are defined.
Informative Note: The TCP/IP discussion in this Specification is based in part upon
reference [2] RFC 1122 to assist the user in implementing the MODBUS Application
Protocol Specification [1] over TCP/IP.
Connection Management
A communication between a client and server MODBUS Module requires the use of a
TCP connection management module. It is in charge to manage globally messaging
TCP connections.
Two possibilities are proposed for the connection management. Either the user
application itself manages TCP connections or the connection management is totally
done by this module and therefore it is transparent for the user application. The last
solution implies less flexibility.
In certain critical contexts, accessibility to internal data of devices must be forbidden for
undesirable hosts. That’s why a security mode is needed and security process may be
implemented if required.
The TCP/IP stack can be parameterized in order to adapt the data flow control, the
address management and the connection management to different constraints specific
to a product or to a system. Generally the BSD socket interface is used to manage the
TCP connections.
In order to equilibrate inbound and outbound messaging data flow between the
MODBUS client and the server, data flow control mechanism is provided in all layers
of MODBUS messaging stack.
The resource management and flow control module is first based on TCP internal flow
control added with some data flow control in the data link layer and also in the user
application level.
Implementation Rules :
2) It is recommended to keep the TCP connection opened with a remote device and not
to open and close it for each MODBUS/TCP transaction,
Remark: However the MODBUS client must be capable of accepting a close request
from the server and closing the connection. The connection can be reopened when
required.
6) A TCP frame must transport only one MODBUS ADU. It is advised against sending
multiple MODBUS requests or responses on the same TCP PDU