Understanding The Modbus Protocol: @version@ (@date@)
Understanding The Modbus Protocol: @version@ (@date@)
@version@ (@date@)
by Dieter Wimberger
Table of contents
1 2
Modbus Functions......................................................................................................... 3 Exceptions..................................................................................................................... 3 Modbus Data Model...................................................................................................... 3 Serial Modbus Implementations....................................................................................4 IP based Modbus Implementations............................................................................... 6
Modbus Implementations...................................................................................................4
3.1 3.2
1. About
This document introduces the reader to the Modbus protocol. It presents a basic protocol description and discusses the serial and the TCP based implementations.
Table 1: Figure 1: ISO/OSI Context At this level Modbus is a stateless client-server protocol (e.g. much like HTTP), based on transactions, which consist of a request (issued by the client) and a response (issued by the server). In the field where this protocol is usually applied, there exists a concept that is one of the possible schemas governing the lower level communication behavior on a network using a shared signal cable: Master-Slave. To prevent confusion, the following directed relations describe Master-Slave in terms of the Client-Server paradigm: the Master is a Client the Slave is a Server A transaction and it's context is visualized in Figure 2.
Modbus Transaction
Table 2: Figure 2: Modbus Transaction The stateless communication is based on a simple package, that is called Protocol Data Unit (PDU). The protocol specification defines three types of PDU's: Request PDU, consisting of: 1. a code specifying a function (Function Code, 1 byte) 2. and function specific data (Function Data, varying number of bytes) Response PDU, consisting of: 1. the function code corresponding to the request (Function Code, 1 byte) 2. and response specific data (Response Data, varying number of bytes) Exception Response PDU, consisting of: 1. the function code corresponding to the request + 0x80 (128), (Error Code, 1 byte) 2. and a code specifying the exception (Exception Code, 1 byte) Figure 3 presents a visualization of these packages.
Modbus PDU's
Page 2
Built with Apache Forrest http://forrest.apache.org/
2.2. Exceptions
In certain cases, the response from a slave will be an exception. The primary identification of an exception response is the error code (function code + 128), which is further specified by the exception code. Assigned codes and descriptions can be found in the specification.
Page 3
Built with Apache Forrest http://forrest.apache.org/
Discrete Input Discrete Output (Coils) Input Registers Holding Registers (Registers)
3. Modbus Implementations
Basically Modbus has been implemented and used over all types of physical links (wire, fiber and radio) and various types of lower level communication stacks. However, we will concentrate on the two basic types of implementations (which are supported by jamod): 1. Serial: Asynchronous Master/Slave 2. IP: Master/Slave
Page 4
Built with Apache Forrest http://forrest.apache.org/
Table 1: Figure 4: Serial Network Architectures To enable the actual communication for this setups, the implementation extends the PDU with additional fields, better said, it wraps the PDU into a package with a header and an error checksum (see Figure 5). The resulting package is defined by the protocol specification as Application Data Unit (ADU), that has a maximum package size of 256 bytes.
Note:
The maximum package size limitation of 256 bytes applies for all existing Modbus protocol implementations!
Table 2: Figure 5: Serial ADU The header is composed of an address field (1 byte) and the tail is an error checksum over the whole package, including the address field (i.e. header). For transmission the Modbus message (i.e. ADU) is placed into a frame that has a known beginning and ending point, allowing detection of the start and the end of a message and thus partial messages. There exist two transmission modes, which differ in encoding, framing and checksum: 1. ASCII Frames are encoded into two ASCII characters per byte, representing the hexadecimal notation of the byte (i.e. characters 09, AF). The error checksum is represented by a longitudinal redundancy check (LRC; 1 byte) and messages start with a colon (':', 0x3A), and end with a carriage return line feed ("CRLF", 0x0D0A). Pauses of 1 second between characters can occur. 2. RTU Frames are transmitted binary to achieve a higher density. The error checksum is represented by a cyclic redundancy check (16 bit CRC; 2 byte) and messages start and end with a silent interval of at least 3.5 character times. This is most easily implemented as a multiple of character times at the baud rate that is being used on the network. The maximum pause that may occur between two bytes is 1.5 character times. jamod is designed to support both transmission modes, using an implementation which is based on the javax.comm API.
Warning:
The RTU implementation does only support the Master side. It is working by the best effort principle, which means it might not work in a reliable way in a low-lantency real-time context.
It is indeed possible to implement the serial transport based on other serial stack implementations (i.e. replacements for the Java Comm API implementation) like for example SerialPort ( http://www.sc-systems.com/products/serialport/serialport.htm (http://www.sc-systems.com/products/serialport/serialport.htm) ). According to the product
Page 5
Built with Apache Forrest http://forrest.apache.org/
info it supports around 20 platforms and it has been successfully used to implement the two serial transmission modes in Java (Master only, see Field Talk/Java (http://www.focus-sw.com/FTMP_MBJV.html) , a commercial Master protocol pack from Focus Engineering).
Table 1: Figure 6: Modbus/TCP ADU The IP specific header (called MBAP in the specification) is 7 bytes long and composed of the following fields: 1. the invocation identification (2 bytes) used for transaction pairing; formerly called transaction identifier 2. the protocol identifier (2 bytes), is 0 for Modbus by default; reserved for future extensions 3. the length (2 bytes), a byte count of all following bytes 4. the unit identifier (1 byte) used to identify a remote unit located on a non-TCP/IP network
Page 6
Built with Apache Forrest http://forrest.apache.org/
3. and the package size is limited to 256 bytes, which should be easily transferable over any IP capable link (including IP over Serial) without the necessity to split the package. Especially if a real-time communication has to be achieved, it might be of interest to investigate in a Modbus/UDP implementation.
Note:
For learning more about Modbus/UDP, please see: Modbus/UDP Specification (modbus_udp.html) .
Page 7
Built with Apache Forrest http://forrest.apache.org/