Introduction To Modbus - National Instruments
Introduction To Modbus - National Instruments
1 of 6
http://www.ni.com/white-paper/7675/en/
Introduction to Modbus
Publish Date: Aug 01, 2014
Overview
The Modbus industrial protocol was developed in 1979 to make communication possible between automation devices.
Originally implemented as an application-level protocol intended to transfer data over a serial layer, the protocol has expanded
to include implementations over serial, TCP/IP, and the user datagram protocol (UDP). Today, it is a common protocol used by
countless devices for simple, reliable, and efficient communication across a variety of modern networks.
Table of Contents
Introduction to Modbus
Modbus is typically used for Supervisory Control and Data Acquisition (SCADA)-style network communication between
devices. For example, a large server may be used to master a programmable logic controller (PLC) or programmable
automation controller (PAC), while that PLC/PAC may in turn master a sensor, valve, motor, or any other embedded device.
To meet these needs, Modbus was designed as a request-response protocol with a flexible data and function modelfeatures
that are part of the reason it is still in use today.
1. The Request-Response Cycle
The Modbus protocol follows a master and slave architecture where a master transmits a request to a slave and waits for the
response. This architecture gives the master full control over the flow of information, which has benefits on older multidrop
serial networks. Even on modern TCP/IP networks, it gives the master a high degree of control over slave behavior, which is
helpful in some designs.
Modbus manages the access of data simply and flexibly. Natively, Modbus supports two data types: a Boolean value and an
unsigned, 16-bit integer.
In SCADA systems, it is common for embedded devices to have certain values defined as inputs, such as gains or
proportional integral derivative (PID) settings, while other values are outputs, like the current temperature or valve position. To
meet this need, Modbus data values are divided into four ranges (see Table 1). A slave can define as many as 65,536
elements in each range.
Memory Block
Data Type
Master Access
Slave Access
Coils
Boolean
Read/Write
Read/Write
Discrete Inputs
Boolean
Read-only
Read/Write
29/Oct/2016 10:52 AM
2 of 6
http://www.ni.com/white-paper/7675/en/
Holding Registers
Unsigned Word
Read/Write
Read/Write
Input Registers
Unsigned Word
Read-only
Read/Write
Modbus function codes determine how data is accessed and modified by the master. Unlike the data ranges, which are
conceptual, function codes have a well-defined behavior. When a slave is asked to perform a function code, it uses the
parameters of the function to execute that well-defined behavior. Figure 2 shows this link between a function request and the
actual memory of the device.
Figure 2. The Mapping Between a Function Code, Data Ranges, and the Actual Memory of a Slave Device
The most common function codes are named after the conceptual data range they modify or access. For example, read
holding registers takes the action of pulling data out of the memory defined as holding registers and returning it to the master.
Table 2 identifies the most common function codes.
29/Oct/2016 10:52 AM
3 of 6
http://www.ni.com/white-paper/7675/en/
The low-level Modbus API is the preferred option when your application needs a high level of control over the sequencing and
timing of Modbus requests. The low-level API is typically also the preferred choice where flexibility is paramount. In contrast,
the flexibility and power offered by the LabVIEW Modbus API also means that your application code must be more complex to
correctly manage the API. To help you understand this complexity, LabVIEW provides two examples.
Modbus Introductory Example
The first example, Modbus Library.lvproj, provides a basic overview of the APIs functionality. It also demonstrates the
differences between an implementation on a PC and a real-time target. Figure 3 shows the code involved in the Real-Time
Modbus Master example.
29/Oct/2016 10:52 AM
4 of 6
http://www.ni.com/white-paper/7675/en/
instance selector.
Figure 5. Modbus Master and Slave Palettes Showing the Function Codes
Finally, the Modbus instance is closed, de-allocating the memory associated with the instance. This also closes any
references, including the TCP connection or NI-VISA serial references used by the instance.
Only the master example has been discussed thus far; however, every example follows the same basic pattern familiar to most
LabVIEW users: open, read/write, and close.
Finally, although the API does look the same, it is important to understand the key difference. If your device is a master, it must
send a request across the network to the appropriate slave to acquire data. The slave, on the other hand, has its own local
data storage and can access it quickly.
Redundant Master Example
The basic example may suffice for some applications; however, it may not be enough for complicated applications where the
goal is to talk to a sensor or gateway. To help bridge this gap, a sample application shows how to use two masters to
communicate with a given slave. If one of the masters fails and loses connection with either the slave or human machine
interface (HMI), the other master takes over.
Modbus I/O servers, which are in the LabVIEW DSC and LabVIEW Real-Time modules, provide a high-level engine for
communicating over Modbus. Rather than specifying a function code that you wish to send, you register the set of data you
would like to access and the I/O server schedules the requests automatically at the specified rate.
To use I/O servers, you add a new I/O server to the desired target in your project. As with the low-level API, you can choose
between a Modbus master or slave, and these lead to additional parameters. For example, a master has a defined polling
ratethe rate at which every request is sent to the slave, while slaves must wait on those requests and have no predefined
timing.
29/Oct/2016 10:52 AM
5 of 6
http://www.ni.com/white-paper/7675/en/
After the I/O server is created, you may specify the items on the device you wish to read. Unlike the low-level API, where you
must generate and process the requests yourself, Modbus I/O servers let you select from a wide variety of formats and data
types. For example, you can read the holding register at address 0 by mapping a variable to the item 400001, read the first bit
of this register by selecting 400001.1, and read the single precision float that is stored in registers 0 and 1 by selecting
F400001.
After selecting variables to access, you can read or write these variables using shared variable nodes on the block diagram.
You can even alias the variable names.
For complicated applications involving many slave devices that communicate over different protocols, the standard Modbus
I/O might not suffice. A common solution is to use an OPC server, which acts as a data aggregator for all of your systems, and
then use the OPC I/O servers included in the LabVIEW DSC Module to communicate with that OPC server.
Figure 8 shows an example of this architecture, with NI OPC Servers using Modbus to communicate directly with sensors and
OPC UA to communicate with an NI CompactRIO PAC. After data is aggregated in NI OPC Servers, an OPC I/O server can
retrieve data and share it with the LabVIEW application.
Figure 8. A SCADA Application Using Modbus, NI OPC Servers, and OPC I/O Servers
You can also develop a similar architecture that uses the OPC UA driver included in the LabVIEW DSC Module in place of
OPC I/O servers. However, the OPC UA driver is a low-level driver and does not provide the ease of use afforded by OPC I/O
servers.
To develop an application like this, you must first generate a valid configuration for NI OPC Servers to communicate with your
slave devices. This is done by generating channelswhich define a driver configurationand deviceswhich define an
individual endpoint for that driver. After you have configured a device, you can then generate tags.
29/Oct/2016 10:52 AM
6 of 6
http://www.ni.com/white-paper/7675/en/
29/Oct/2016 10:52 AM