Skip to main content

Read a Modbus device

In this tutorial, you connect the gateway to a Modbus TCP device and read its values with an OPC UA client. The device is a demo air handling unit (AHU) that runs in a Docker container. Its values are ones that you can recognise: 21.4 °C, 4.2 bar, 60 %. At the end, you know how a Modbus register becomes an OPC UA variable, and how to see that an address or a scale is wrong.

Before you start​

  • Do the tutorial Your first gateway with npm. This tutorial uses the same folder and the same commands.
  • Docker, to run the demo device.
  • An OPC UA client, for example UaExpert.

Words on this page​

WordMeaning
registerA 16-bit word in a Modbus device. A holding register can be read and written. An input register can only be read.
coilA bit in a Modbus device that can be read and written, for example a relay.
discrete inputA bit in a Modbus device that can only be read, for example a switch.
pointA named value that you declare in the configuration file: an address and a type. Each point becomes an OPC UA variable.
scaleA number that the gateway multiplies the raw value with. A device often sends 214 for 21.4 °C.

What the gateway does with a point​

Modbus device
holding register 10 = 214
│
▼
Configuration file
point SupplyAirTemperature
at: holding:10 type: int16 scale: 0.1
│ 214 × 0.1
▼
OPC UA address space
Objects
└─ AHU
└─ SupplyAirTemperature = 21.4

The gateway reads the register each second, multiplies the raw value by the scale and writes the result to the variable.

Step 1: Start the demo device​

  1. Run this command:

    docker run -d --name ahu-demo -p 5020:8502 registry.gitlab.sterfive.fr/sterfive/opcua-omni-edge/modbus:latest

    The device listens on TCP port 5020 of your computer. The standard Modbus port 502 needs administrator rights on Linux, so this tutorial uses 5020.

  2. Read the log of the device:

    docker logs ahu-demo

    The log shows the values that the device holds:

    realistic Modbus demo device on 0.0.0.0:8502 (unit 1)
    holding 10 = 21.4 degC (int16 x0.1) holding 12 = 4.2 bar (float32)
    holding 40 = 60 % coil 3 = closed
    input 100 = 230.5 V (float32) input 102 = 12.75 kW
    anything else answers ILLEGAL DATA ADDRESS, like a real device

The values change by up to 0.3 each second, so that you can see the variables update.

Step 2: Write the configuration file​

  1. Open config.yaml, the file of the first tutorial.
  2. Replace its content with this text:
# yaml-language-server: $schema=https://opcua-omni-edge.doc.sterfive.com/schema/config.schema.json

port: 4840

brownfieldDevices:
modbus:
# One connection: the demo air handling unit.
- name: AHU1
type: TCP
address: 127.0.0.1
port: 5020
unitId: 1
pollingRate: 1000
# The gateway makes the Object AHU under Objects.
expose:
parent: /ua:Objects
object: AHU
# Each point becomes a Variable of AHU.
points:
- name: SupplyAirTemperature
at: holding:10
type: int16
scale: 0.1
unit: degC
- name: DischargePressure
at: holding:12
type: float32
unit: bar
- name: FanSpeedSetpoint
at: holding:40
type: uint16
unit: "%"
- name: CoolingValveOpen
at: coil:3
type: bool
- name: FilterDirty
at: discrete:7
type: bool

The connection has 3 parts:

PartWhat it does
address:, port:, unitId:Where the device is. unitId is the Modbus slave id.
expose:Where the device goes in the address space: the Object AHU under Objects.
points:The values to read. at: gives the table and the address. type: gives how to decode the registers: float32 uses 2 registers.

This file has no instances: and no mapping:. With expose:, the gateway makes the Object and its Variables itself.

Step 3: Start the gateway​

  1. Run:

    node_modules/.bin/opcua-omni-edge run -c config.yaml
  2. The gateway is ready when it shows these lines:

    ° install Modbus connection AHU1 127.0.0.1 5020
    Installing binding for type: variable-binding with node: /ua:Objects/own:AHU/own:SupplyAirTemperature
    ...
    server started
    server start at endpoint = opc.tcp://<computer name>:4840

Step 4: Read the values with an OPC UA client​

  1. In UaExpert, connect to opc.tcp://localhost:4840 with the security mode None and the Anonymous user.
  2. In the address space, open Objects, then AHU.
  3. Drag the 5 Variables to the data access view.

The values are:

VariableValueData type
SupplyAirTemperatureAbout 21.4Double
DischargePressureAbout 4.2Float
FanSpeedSetpoint60UInt16
CoolingValveOpentrueBoolean
FilterDirtyfalseBoolean

The status of each value is Good.

Step 5: See what a mistake looks like​

A wrong configuration often gives no error message. It gives a wrong value. Make 2 mistakes on purpose, and learn to see them.

A missing scale​

  1. Stop the gateway with Ctrl+C.
  2. In SupplyAirTemperature, remove the line scale: 0.1.
  3. Start the gateway again and read SupplyAirTemperature.

The value is about 214, and its data type is Int16. The gateway shows the raw register. A value 10 times too large, or 100 times too large, often means a missing scale:. The documentation of the device gives the scale.

  1. Put the line scale: 0.1 back.

A wrong address​

  1. Stop the gateway.
  2. In SupplyAirTemperature, change at: holding:10 to at: holding:300.
  3. Start the gateway again and read the Variables.

SupplyAirTemperature has no value, and its status is BadWaitingForInitialData. The other 4 Variables are still Good: one wrong point does not stop the connection. The log of the gateway gives the cause:

Modbus error Modbus exception 2: Illegal data address (register not supported by device) { type: 'holdingRegisters', start: 300, length: 1 }

The device has no register 300, so it refuses the read. A real device does the same.

  1. Change the address back to holding:10.
note

A wrong address that the device has does not give an error. It gives the value of another register. Compare each value with a value that you know: the display of the device, or its documentation.

Step 6: Clean up​

  1. Stop the gateway with Ctrl+C.

  2. Stop and remove the demo device:

    docker rm -f ahu-demo

What you did​

  • You declared a Modbus connection in brownfieldDevices:, with its address and its points.
  • With expose:, the gateway made an Object for the device and a Variable for each point.
  • You decoded registers with type: and scale:.
  • You found 2 common mistakes from the values and the log.

Next steps​

  • Modbus devices: all the keys of a connection and of a point, the byte order and the poll plan.
  • Mappings: send a point to a node of an information model with modbus: AHU1:SupplyAirTemperature.
  • Instances: make a typed Object, for example a pump from an information model, for the values.
  • Error codes: the codes that the gateway and check show.