Modbus 通信協定04
Modbus 通信協定04
0b Modbus Organization
The MODBUS Backend service will use the appropriate interface to the user
application :
Either a physical interface based on a serial link, or a dual-port RAM scheme, or
a simple I/O line, or a logical interface based on messaging services provided by
an operating system.
The interface to the User Application may be synchronous or asynchronous.
The MODBUS Backend service will also use the appropriate design pattern to get/set
objects attributes or to trigger services. In some cases, a simple "gateway pattern" will
be adequate. In some other cases, the designer will have to implement a "proxy
pattern" with a corresponding caching strategy, from a simple exchange table history to
more sophisticated replication mechanisms.
The MODBUS Backend service has the responsibility to implement the protocol
transcription in order to interact with the User Application. Therefore, it can have to
implement mechanisms for packet fragmentation/reconstruction, data consistency
guarantee, and synchronization whatever is required.
The MODBUS response PDU must be prefixed with the MBAP header which is built
using data memorized in the transaction context.
• Unit Identifier
The Unit Identifier is copied as it was given within the received MODBUS request
and memorized in the transaction context.
• Length
The server calculates the size of the MODBUS PDU plus the Unit Identifier byte.
This value is set in the "Length" field.
• Protocol Identifier
The Protocol Identifier field is set to 0x0000 (MODBUS protocol), as it was given
within the received MODBUS request.
• Transaction Identifier
This field is set to the "Transaction Identifier" value that was associated with the
original request and memorized in the transaction context.
Then the MODBUS response must be returned to the right MODBUS Client using the
TCP connection memorized in the transaction context. When the response is sent, the
transaction context must be free.
5 IMPLEMENTATION GUIDELINE
The objective of this section is to propose an example of a messaging service
implementation.
The model describes below can be used as a guideline during a client or a server
implementation of a MODBUS messaging service.
User Application
InterfaceUserApplication
Configuration layer
InterfaceIndicationMsg InterfaceResponseMsg
TCP management
InterfaceIndicationMsg
InterfaceRespons eMsg
(from Logical View)
(from Logical View)
-----------------------------------------------------------------------------------------------------------------------------------------------
ConnexionMgt
NbConnectionSupported : Int
Int erfaceConnex ion
NbLocalConnection : Int
NbRemoteConnection : int
GetObjectConnexion()
m_sendData() FreeObjectConnexion()
m_Receivedata() RetreivingObject Connexion()
m_isConnectionAuthorized() TCP management
pac kage
TCPConnexion
MB AP
IsConnexionRequest ()
It emConnexion OpenConnexion() IsMdbHeader()
SocketDescript : Int AcceptConnexion() GetMessagelength()
IPSource : Long CloseConnexion() WriteTransactionId()
PortSource : Long IsEt ablishedConnexion() ReadTransactionId()
PortDest : Long
IPdestination : long
BufferIn : Char*
BufferOut : Char*
Stack TCP IP
Socket interface()
CInterfaceConnexion: The role of this class consists in managing memory pool for
connections.
------------------------------------------------------------------------------------------------------------------------------------------------
Operating Mode
m_Configure()
m_Start()
Configuartion layer
m_Stop()
package
m_Reset()
ConfigurationObject
GlobalState : char
MyModbusAddress : Int
MyIPAddress : long
MyPortNumber : Long
NumberAuthorized_IP : int
ListAuthorized_IP : int
NumberForbidden_IP : Int
ListForbidden_IP : long()
NumberConnect ionSupported : int
TConfigureObject: This class groups all data needed for configuring each other
component. This structure is filled by the method m_Configure from the class
CoperatingMode. Each class needing to be configured gets its own configuration data
from this object. The configuration data is implementation dependent therefore the list
of attributes of this class is provided as an example.
CMODBUSServer
CMODBUSClient
CconnexionMngt
InterfaceUserApplication
(from Logical View)
--------------------------------------------------------------------------------------------------------------------------------------------------------
ModbusPDU Communication
Application layer
package
IsMdbAnalysisOfDataCorrect()
m_BuildModbusException()
m_WritePDU()
ModbusServer
ModbusClient
m_ServerReceivingMessage()
m_ServerModbus() m_ClientReceivingResponse()
m_BuildResponse() m_ClientReceivingMessage()
m_InitServerfunction() m_ClientModbus()
Transaction
TransactionId : int
TimeSnapShot : int
IsTransactionTimeOut()
m_WriteTransactionID()
m_StartTimesnapShoot()
--------------------------------------------------------------------------------------------------------------------------------------------------------
implements the State Graph of MODBUS client and manages transactions for linking
query with response (from network). Query can be sent over network only if class
CoperatingMode has sent both user configuration and right operating modes.
CInterfaceResponseMsg: This Interface class is used for receiving response from the
Server and for sending query from the client to the Network. This class interfaces
packages ‘Communication Application Layer’ and package ‘TCPManagement’ (To
Network). The implementation of this class is device dependent.
First step: A Reading query comes from User Application (method m_Read).
Second Step: The ‘Client’ task receives the MODBUS query (method
m_ClientReceivingMessage). This is the entry point of the Client. To associate the
query with the corresponding response when it will arrive, the Client uses a
Transaction resource (Class CTransaction). The MODBUS query is sent to the
TCP_Management by calling the class interface CInterfaceResponseMsg (method
m_MODBUSRequest)
Fourth step: Once a response has been received from the network, the TCP/IP stack
receives data (method m_EventOnSocket is implicitly called).
If the connection is already established, then the MBAP is read for retrieving the
connection object (connection object gives memory resource and other information).
Data coming from network is read and confirmation is sent to the client task via the
class Interface CInterfaceIndicationMsg (method m_MODBUSConfirmation). Client
task receives the MODBUS Confirmation (method m_ClientReceivingResponse).
Finally the response is written to the user application (method m_WriteData), and
transaction resource is freed.