Modbus 通信協定03
Modbus 通信協定03
Modbus 通信協定03
0b Modbus Organization
Idle
Wait
[Reveive_Response_from_TCP_Mgt]
[Request_from_the_user application]
Waiting_response_timer_expires
[Confirmation OK]
Send negative
confirmation to user Set Waiting
Application response timer
The instantiation of a MODBUS transaction that enables the Client to memorize all
required information in order to bind later the response to the request and to send
the confirmation to the user application.
The encoding of the MODBUS request (PDU + MPAB header). The user application
that initiates the demand has to provide all required information which enables the
Client to encode the request. The MODBUS PDU is encoded according to the
MODBUS Application Protocol Specification [1]. (MB function code, associated
parameters and application data ). All fields of the MBAP header are filled. Then,
the MODBUS request ADU is built prefixing the PDU with the MBAP header
The sending of the MODBUS request ADU to the TCP management module which is
in charge of finding the right TCP socket towards the remote Server. In addition to
the MODBUS ADU the Destination IP address must also be passed.
The following activity diagram describes, more deeply than in Figure 11 MODBUS
Client Activity Diagram, the request building phase.
Instantiate a MB
transaction
[Transaction available]
Initialize the
transaction
Encode the MB
request PDU
Encode the
MBAP header
Send a Send MB
negative request to TCP
confirmation to Mgt
the user
application
The following example describes the MODBUS request ADU encoding for reading the
register # 5 in a remote server :
• Transaction Identifier
The transaction identifier is used to associate the future response with the request.
So, at a time, on a TCP connection, this identifier must be unique. There are
several manners to use the transaction identifier:
- For example, it can be used as a simple "TCP sequence number" with a
counter which is incremented at each request.
- It can also be judiciously used as a smart index or pointer to identify a
transaction context in order to memorize the current remote server and the
pending MODBUS request.
Normally, on MODBUS serial line a client must send one request at a time. This means
that the client must wait for the answer to the first request before sending a second
request. On TCP/MODBUS, several requests can be sent without waiting for a
confirmation to the same server. The MODBUS/TCP to MODBUS serial line gateway is
in charge of ensuring compatibility between these two behaviors.
The number of requests accepted by a server depends on its capacity in term of
number of resources and size of the TCP windows. In the same way the number of
transactions initialized simultaneously by a client depends also on its resource capacity.
This implementation parameter is called "NumberMaxOfClientTransaction" and must
be described as one of the MODBUS client features. Depending of the device type this
parameter can take a value from 1 to 16.
• Unit Identifier
This field is used for routing purpose when addressing a device on a MODBUS+
or MODBUS serial line sub-network. In that case, the “Unit Identifier” carries the
MODBUS slave address of the remote device:
If the MODBUS server is connected to a MODBUS+ or MODBUS Serial Line
sub-network and addressed through a bridge or a gateway, the MODBUS Unit
identifier is necessary to identify the slave device connected on the sub-
network behind the bridge or the gateway. The destination IP address
identifies the bridge itself and the bridge uses the MODBUS Unit identifier to
forward the request to the right slave device.
The MODBUS slave device addresses on serial line are assigned from 1 to
247 (decimal). Address 0 is used as broadcast address.
On TCP/IP, the MODBUS server is addressed using its IP address; therefore, the
MODBUS Unit Identifier is useless. The value 0xFF has to be used.
When addressing a MODBUS server connected directly to a TCP/IP network,
it’s recommended not using a significant MODBUS slave address in the “Unit
Identifier” field. In the event of a re-allocation of the IP addresses within an
automated system and if a IP address previously assigned to a MODBUS
server is then assigned to a gateway, using a significant slave address may
cause trouble because of a bad routing by the gateway. Using a non-
significant slave address, the gateway will simply discard the MODBUS PDU
with no trouble. 0xFF is recommended for the “Unit Identifier" as non-
significant value.
Remark : The value 0 is also accepted to communicate directly to a
MODBUS/TCP device.
Parsing the response consists in verifying the MBAP Header and the MODBUS PDU
response:
MBAP Header
After the verification of the Protocol Identifier that must be 0x0000, the length gives
the size of the MODBUS response.
If the response comes from a MODBUS server device directly connected to the
TCP/IP network, the TCP connection identification is sufficient to unambiguously
identify the remote server. Therefore, the Unit Identifier carried in the MBAP
header is not significant (value 0xFF) and must be discarded.
If the remote server is connected on a Serial Line sub-network and the response
comes from a bridge, a router or a gateway, then the Unit Identifier (value != 0xFF)
identifies the remote MODBUS server which has originally sent the response.
Remark: A positive confirmation is a confirmation that the command was received and
responded to by the server. It does not imply that the server was able to successfully
act on the command (failure to successfully act on the command is indicated by the
MODBUS Exception response).
The following activity diagram describes, more deeply than in Figure 11 MODBUS
Client Activity Diagram, the confirmation processing phase.
[PendingTransaction ]
Use MB transaction to
bind with the request
Discard
Response
[Modbus_protocol] [Other_protocol]
Analyse Response
PDU
Send positive
confirmation to user
application
Wait
So in practice, the client timeouts used in high performance applications are always
likely to be somewhat dependent on network topology and expected client performance.
Applications which are not time critical can often leave timeout values to the normal
TCP defaults, which will report communication failure after several seconds on most
platforms.
Modbus
Client
Serial Line
Modbus Modbus
Client Client
Client TCP/IP
TCP/IP TCP/IP
gateway
Modbus TCP / IP
Modbus Modbus
Modbus Modbus
Server Server
Server Server
Serial Line Serial Line
TCP/IP TCP/IP Server TCP/IP
gateway
The role of a MODBUS server is to provide access to application objects and services
to remote MODBUS clients.
In run time the MODBUS server has to analyze a received MODBUS request, to
process the required action, and to send back a MODBUS response.
Informative Note: The application objects and services of the Backend Interface obtain
the requested data based upon the function code, and the User is responsible.
The following activity diagram describes the main treatments that are processed by the
Server to obtain a MODBUS request from TCP Management, then to analyze the
request, to process the required action, and to send back a MODBUS response.
Idle
[Server init]
Send response
to TCP_Mgt
Some services can be immediately processed by the MODBUS Server itself, with
no direct interaction with the User Application ;
Some services may require also interacting explicitly with the User Application to
be processed ;
The MODBUS server can accept to serve simultaneously several MODBUS requests.
The maximum number of simultaneous MODBUS requests the server can accept is one
of the main characteristics of a MODBUS server. This number depends on the server
design and its processing and memory capabilities. This implementation parameter is
called "NumberMaxOfSeverTransaction" and must be described as one of the
MODBUS server features. It may have a value from 1 to 16 depending on the device
capabilities.
The behavior and the performance of the MODBUS server are significantly affected by
the "NumberMaxOfTransaction" parameter. Particularly, it's important to note that the
number of concurrent MODBUS transactions managed may affect the response time of
a MODBUS request by the server.
Parse the
MBAP header
Instantiate a
MB Transaction
Parse The MB
PDU
[Error on MB PDU]
MB Transaction
refused
[OK]
MB Transaction
accepted
The MODBUS PDU Checking function consists of first parsing the MBAP Header. The
Protocol Identifier field has to be checked :
If it is different from MODBUS protocol type, the indication is simply discarded.
If it is correct (= MODBUS protocol type; value 0x00), a MODBUS transaction is
instantiated.
The maximum number of MODBUS transactions the server can instantiate is defined by
the "NumberMaxOfTransaction" parameter ( A system or a configuration parameter).
Then the MODBUS PDU is parsed. The function code is first controlled :
in case of invalidity a MODBUS exception response is built (Exception code 1 : Invalid
function).
If the function code is accepted, the server initiates the "MODBUS Service
processing" activity.
Transaction_accepted Response_from_user_App
Analyse Response
requested service processing
Local Service
processing Send an invocation to [Completed]
User Application
through MB Backend
interface
[Processing OK]
[Processing not OK]
[Processing Not OK]
[Processing OK]
Build Modbus
Build Modbus Response
Exception Response
The processing of the required MODBUS service can be done in different ways
depending on the device software and hardware architecture as described in the
hereafter examples :
To interact with the User Application, the MODBUS Backend service must implement all
appropriate mechanisms in order to handle User Application transactions and to
manage correctly the User Application invocations and associated responses.