Modbus
Modbus is a data communications protocol originally published by Modicon in 1979 for use with its programmable logic controllers. Modbus has become a de facto standard communication protocol and is now a commonly available means of connecting industrial electronic devices. Modbus is popular in industrial environments because it is openly published and royalty-free. It was developed for industrial applications, is relatively easy to deploy and maintain compared to other standards, and places few restrictions - other than the datagram size - on the format of the data to be transmitted. Modbus uses the RS485 or Ethernet as its wiring type. Modbus supports communication to and from multiple devices connected to the same cable or Ethernet network. For example, a device that measures temperature and a different device to measure humidity, both of which communicates the measurements to a computer.
Modbus is often used to connect a plant/system supervisory computer with a remote terminal unit in Supervisory Control and Data Acquisition systems in the electric power industry. Many of the data types are named from industrial control of factory devices, such as Ladder logic because of its use in driving relays: A single physical output is called a coil, and a single physical input is called a discrete input or a contact.
The development and update of Modbus protocols have been managed by the Modbus Organization since April 2004, when Schneider Electric transferred rights to that organization. The Modbus Organization is an association of users and suppliers of Modbus-compliant devices that advocates for the continued use of the technology.
Limitations
- Since Modbus was designed in the late 1970s to communicate to programmable logic controllers, the number of data types is limited to those understood by PLCs at the time. Large binary objects are not supported.
- No standard way exists for a node to find the description of a data object, for example, to determine whether a register value represents a temperature between 30 and 175 degrees.
- Since Modbus is a master/slave protocol, there is no way for a field device to "report an exception" – the master node must routinely poll each field device and look for changes in the data. This consumes bandwidth and network time in applications where bandwidth may be expensive, such as over a low-bit-rate radio link.
- Modbus is restricted to addressing 254 devices on one data link, which limits the number of field devices that may be connected to a master station.
- Modbus transmissions must be contiguous, which limits the types of remote communications devices to those that can buffer data to avoid gaps in the transmission.
- Modbus protocol itself provides no security against unauthorized commands or interception of data.
Modbus object types
Object type | Access | Size | Address Space |
Coil | Read-write | 1 bit | 00001 - 09999 |
Discrete input | Read-only | 1 bit | 10001 - 19999 |
Input register | Read-only | 16 bits | 30001 - 39999 |
Holding register | Read-write | 16 bits | 40001 - 49999 |
Protocol versions
Versions of the Modbus protocol exist for serial port and for Ethernet and other protocols that support the Internet protocol suite. There are many variants of Modbus protocols:- Modbus RTU — This is used in serial communication and makes use of a compact, binary representation of the data for protocol communication. The RTU format follows the commands/data with a cyclic redundancy check checksum as an error check mechanism to ensure the reliability of data. Modbus RTU is the most common implementation available for Modbus. A Modbus RTU message must be transmitted continuously without inter-character hesitations. Modbus messages are framed by idle periods.
- Modbus ASCII — This is used in serial communication and makes use of ASCII characters for protocol communication. The ASCII format uses a longitudinal redundancy check checksum. Modbus ASCII messages are framed by leading colon and trailing newline.
- Modbus TCP/IP or Modbus TCP — This is a Modbus variant used for communications over TCP/IP networks, connecting over port 502. It does not require a checksum calculation, as lower layers already provide checksum protection.
- Modbus over TCP/IP or Modbus over TCP or Modbus RTU/IP — This is a Modbus variant that differs from Modbus TCP in that a checksum is included in the payload as with Modbus RTU.
- Modbus over UDP — Some have experimented with using Modbus over UDP on IP networks, which removes the overheads required for TCP.
- Modbus Plus — Modbus Plus is proprietary to Schneider Electric and unlike the other variants, it supports peer-to-peer communications between multiple masters. It requires a dedicated co-processor to handle fast HDLC-like token rotation. It uses twisted pair at 1 Mbit/s and includes transformer isolation at each node, which makes it transition/edge-triggered instead of voltage/level-triggered. Special hardware is required to connect Modbus Plus to a computer, typically a card made for the ISA, PCI or PCMCIA bus.
- Pemex Modbus — This is an extension of standard Modbus with support for historical and flow data. It was designed for the Pemex oil and gas company for use in process control and never gained widespread adoption.
- Enron Modbus — This is another extension of standard Modbus developed by Enron Corporation with support for 32-bit integer and floating-point variables and historical and flow data. Data types are mapped using standard addresses. The historical data serves to meet an American Petroleum Institute industry standard for how data should be stored.
Communications and devices
Each device communicating on a Modbus is given a unique address.On Modbus RTU, Modbus ASCII and Modbus Plus which are all Rs-485 single cable multi-drop networks, only the node assigned as the Master may initiate a command. All other devices are slaves and respond to requests and commands.
For the protocols using Ethernet such as Modbus TCP, any device can send out a Modbus command thus all can act as a Master, although normally, only one device acts as a Master.
There are many modems and gateways that support Modbus, as it is a very simple and often copied protocol. Some of them were specifically designed for this protocol. Different implementations use wireline, wireless communication, such as in the ISM band, and even Short Message Service or General Packet Radio Service. One of the more common designs of wireless networks makes use of mesh networking. Typical problems that designers have to overcome include high latency and timing issues.
Commands
Modbus commands can instruct a Modbus Device to:- change the value in one of its registers, that is written to Coil and Holding registers.
- read an I/O port: Read data from a Discrete and Coil ports,
- command the device to send back one or more values contained in its Coil and Holding registers.
All Modbus commands contain checksum information to allow the recipient to detect transmission errors.
Frame formats
A Modbus "frame" consists of an Application Data Unit, which encapsulates a Protocol Data Unit :- ADU = Address + PDU + Error check,
- PDU = Function code + Data.
Modbus RTU">Remote Terminal Unit">RTU frame format (primarily used on asynchronous serial data lines like RS-485/[EIA-485])
Note about the CRC:- Polynomial: x16 + x15 + x2 + 1.
- Initial value: 65,535.
- Example of frame in hexadecimal:
01 04 02 FF FF B8 80
.Modbus ASCII frame format (primarily used on 7- or 8-bit asynchronous serial lines)
7A
.LRC is calculated as the sum of 8-bit values, negated and encoded as an 8-bit value. Example: if address, function, and data encode as 247, 3, 19, 137, 0, and 10, their sum is 416. Two's complement trimmed to 8 bits is 96, which will be represented as
60
in hexadecimal. Hence the following frame: :F7031389000A60
.Modbus TCP frame format (primarily used on [Ethernet] networks)
Unit identifier is used with Modbus/TCP devices that are composites of several Modbus devices, e.g. on Modbus/TCP to Modbus RTU gateways. In such case, the unit identifier tells the Slave Address of the device behind the gateway. Natively Modbus/TCP-capable devices usually ignore the Unit Identifier.Available function/command codes
The various reading, writing and other operations are categorized as follows. The most primitive reads and writes are shown in bold. A number of sources use alternative terminology, for example Force Single Coil where the standard uses Write Single Coil.Prominent entities within a Modbus slave are:
- Coils: readable and writable, 1 bit
- Discrete Inputs: readable, 1 bit
- Input Registers: readable, 16 bits, essentially measurements and statuses
- Holding Registers: readable and writable, 16 bits, essentially configuration values
Format of data of requests and responses for main function codes
Function code 1 (read coils) and function code 2 (read discrete inputs)
Request:- Address of first coil/discrete input to read
- Number of coils/discrete inputs to read
- Number of bytes of coil/discrete input values to follow
- Coil/discrete input values
If number of coils/discrete inputs is not a multiple of 8, most significant bit of last byte will be stuffed with zeros.
For example, if eleven coils are requested, two bytes of values are needed. Suppose states of those successive coils are on, off, on, off, off, on, on, on, off, on, on, then the response will be
02 E5 06
in hexadecimal.Because the byte count returned in the reply message is only 8 bits wide and the protocol overhead is 5 bytes, a maximum of 2008 discrete inputs or coils can be read at once.
Function code 5 (force/write single coil)
Request:- Address of coil
- Value to force/write: 0 for off and 65,280 for on
Function code 15 (force/write multiple coils)
Request:- Address of first coil to force/write
- Number of coils to force/write
- Number of bytes of coil values to follow
- Coil values
If number of coils is not a multiple of 8, most significant bit of last byte should be stuffed with zeros. See example for function codes 1 and 2.
Normal response:
- Address of first coil
- number of coils
Function code 4 (read input registers) and function code 3 (read holding registers)
- Address of first register to read
- Number of registers to read
- Number of bytes of register values to follow
- Register values
Function code 6 (preset/write single holding register)
Request:- Address of holding register to preset/write
- New value of the holding register
Function code 16 (preset/write multiple holding registers)
Request:- Address of first holding register to preset/write
- Number of holding registers to preset/write
- Number of bytes of register values to follow
- New values of holding registers
Normal response:
- Address of first preset/written holding register
- Number of preset/written holding registers
Exception responses
Main Modbus exception codes
Coil, discrete input, input register, holding register numbers and addresses
Some conventions govern how access to Modbus entities are referenced.It is important to make a distinction between entity number and entity address:
- Entity numbers combine entity type and entity location within their description table.
- Entity address is the starting address, a 16-bit value in the data part of the Modbus frame. As such its range goes from 0 to 65,535
- coils numbers start with 0 and span from 00001 to 09999,
- discrete input numbers start with 1 and span from 10001 to 19999,
- input register numbers start with 3 and span from 30001 to 39999,
- holding register numbers start with 4 and span from 40001 to 49999.
For example, in order to read holding registers starting at number 40001, corresponding address in the data frame will be 0 with a function code of 3. For holding registers starting at number 40100, address will be 99. Etc.
This limits the number of addresses to 9,999 for each entity. A de facto referencing extends this to the maximum of 65,536.
It simply consists of adding one digit to the previous list:
- coil numbers span from 000001 to 065536,
- discrete input numbers span from 100001 to 165536,
- input register numbers span from 300001 to 365536,
- holding register numbers span from 400001 to 465536.
JBUS mapping
Another de facto protocol closely related to Modbus appeared after it, and was defined by PLC brand April Automates, the result of a collaborative effort between French companies Renault Automation and Merlin Gerin et Cie in 1985: JBUS. Differences between Modbus and JBUS at that time are now irrelevant as this protocol almost disappeared with the April PLC series which AEG Schneider Automation bought in 1994 and then made obsolete. However the name JBUS has survived to some extent.JBUS supports function codes 1, 2, 3, 4, 5, 6, 15, and 16 and thus all the entities described above. However numbering is different with JBUS:
- Number and address coincide: entity #x has address x in the data frame.
- Consequently, entity number does not include the entity type. For example, holding register #40010 in Modbus will be holding register #9, located at address 9 in JBUS.
- Number 0 is not supported. Slave should not implement any real data at this number and address and it can return a null value or throw an error when requested.
Implementations
- Data types
- * IEEE floating-point number
- * 32-bit integer
- * 8-bit data
- * Mixed data types
- * Bit fields in integers
- * Multipliers to change data to/from integer. 10, 100, 1000, 256...
- Protocol extensions
- * 16-bit slave addresses
- * 32-bit data size
- * Word-swapped data
Trade group
Modbus Plus
Despite the name, Modbus Plus is not a variant of Modbus. It is a different protocol, involving token passing.It is a proprietary specification of Schneider Electric, though it is unpublished rather than patented. It is normally implemented using a custom chipset available only to partners of Schneider.