Modbus

As part of the Edge Xpert distribution, IOTech provides a Modbus Device Service. This allows Edge Xpert to read data from Modbus devices and to issue commands.

The Modbus Device Service provides a means of integrating Modbus devices with Edge Xpert. For more information, refer to the Modbus website.

The Edge Xpert Modbus Device Service can be used to communicate with devices that use either a serial port protocol or an Ethernet protocol that supports the Internet protocol suite, as follows:

  • Modbus RTU

    This is used in serial communication and uses a compact, binary representation of the data for protocol communication.

  • Modbus TCP/IP or Modbus TCP

    This is a Modbus variant used for communications over TCP/IP networks, connecting over port 1502.

The Edge Xpert Modbus Device Service is based on goburrow modbus, a fault-tolerant, fail-fast implementation of the Modbus protocol in Go.

The Modbus Device Service supports the following key features:

  • Read. Data can be read from each primary table, as follows:

    Supported Read Modbus Functions
    Primary Table Modbus Function
    Coils 1
    Discrete inputs 2
    Holding registers 3
    Input registers 4
  • Write. Data can be written to each writable primary table, as follows

    Supported Write Modbus Functions
    Primary Table Modbus Function
    Coils 15
    Holding registers 16
  • Support for Parsing Int32 and Uint32 data types with byte swap and word swap

  • Support for Modbus RTU and TCP connection types

  • Batch read operation. Read multiple values in a single Modbus call from each primary table, as described in the Supported Read Modbus Functions table above

  • Batch write operation. Write multiple values in a single Modbus call to each writable primary table, as described in the Supported Write Modbus Functions table above

The Modbus Device Service supports the following data types:

  • Boolean
  • UInt8, UInt16, UInt32, UInt64
  • Int8, Int16, Int32, Int64
  • Float32
  • Float64

Modbus Attributes

The device profile defines what resources are available on a particular device. Ensure that the following profile attributes are defined in the YAML file:

Required Profile Attributes
Attribute Description
primaryTable HOLDING_REGISTERS, INPUT_REGISTERS, COILS, DISCRETES_INPUT
startingAddress The address in the Modbus device

The Property value type decides how many registers are read. Like Holding registers, a register has 16 bits. If the device manual specifies that a value has two registers, define it as FLOAT32 or INT32 or UINT32 in the device profile.

Once we execute a command, device-modbus knows its value type and register type, startingAddress, and register length. So it can read or write a value using the Modbus protocol.

Optional Attributes

The Modbus Device Service also allows you to define optional attributes. The following optional profile attributes can be defined in the YAML file:

Optional Profile Attributes
Attribute Description
rawType Defines the binary data read from the Modbus device and uses a value descriptor data type to identify the data type that the user wants to receive
isByteSwap Defines whether to swap the byte on reading little-endian data to transform to big-endian format. The Modbus Device Service uses big-endian format. If the Modbus device uses little-endian format, set this attribute to true to correctly convert the data
isWordSwap Defines whether to use 16-bit segments, also known as words, to re-order the byte sequence with big-endian or little-endian formats. To re-order the byte sequence, set this attribute to true

The value type under properties defines the data type to use when sending transformed data to the user based on the rawType attribute. The supported transformations are as follows:

Supported Transformations
From rawType To value type
INT16 FLOAT32
INT16 FLOAT64
UINT16 FLOAT32
UINT16 FLOAT64

Modbus Examples

All examples assume that the Edge Xpert services have been started with at least the --xpert-manager and --device-modbus parameters. A suitable start-up command would be as follows:

edgexpert up --xpert-manager --device-modbus

Modbus TCP Examples

These examples use a Modbus Simulator, called ModbusPal; this is the recommended version. The simulator must be configured for the examples, as described in the following topic:

If you need to perform byte and/or word swapping, see the following topic:

The following examples illustrate how the Modbus TCP Device Service can be used:

Modbus RTU Examples

The following examples illustrate how the Modbus RTU Device Service can be used:

Note

For these examples, you must use a physical Modbus RTU device. You must use the address of your device, for example, /tmp/slave.