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
| Word | Meaning |
|---|---|
| register | A 16-bit word in a Modbus device. A holding register can be read and written. An input register can only be read. |
| coil | A bit in a Modbus device that can be read and written, for example a relay. |
| discrete input | A bit in a Modbus device that can only be read, for example a switch. |
| point | A named value that you declare in the configuration file: an address and a type. Each point becomes an OPC UA variable. |
| scale | A 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
-
Run this command:
docker run -d --name ahu-demo -p 5020:8502 registry.gitlab.sterfive.fr/sterfive/opcua-omni-edge/modbus:latestThe device listens on TCP port 5020 of your computer. The standard Modbus port 502 needs administrator rights on Linux, so this tutorial uses 5020.
-
Read the log of the device:
docker logs ahu-demoThe 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 = closedinput 100 = 230.5 V (float32) input 102 = 12.75 kWanything 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
- Open
config.yaml, the file of the first tutorial. - 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:
| Part | What 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
-
Run:
node_modules/.bin/opcua-omni-edge run -c config.yaml -
The gateway is ready when it shows these lines:
° install Modbus connection AHU1 127.0.0.1 5020Installing binding for type: variable-binding with node: /ua:Objects/own:AHU/own:SupplyAirTemperature...server startedserver start at endpoint = opc.tcp://<computer name>:4840
Step 4: Read the values with an OPC UA client
- In UaExpert, connect to
opc.tcp://localhost:4840with the security modeNoneand theAnonymoususer. - In the address space, open
Objects, thenAHU. - Drag the 5 Variables to the data access view.
The values are:
| Variable | Value | Data type |
|---|---|---|
SupplyAirTemperature | About 21.4 | Double |
DischargePressure | About 4.2 | Float |
FanSpeedSetpoint | 60 | UInt16 |
CoolingValveOpen | true | Boolean |
FilterDirty | false | Boolean |
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
- Stop the gateway with Ctrl+C.
- In
SupplyAirTemperature, remove the linescale: 0.1. - 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.
- Put the line
scale: 0.1back.
A wrong address
- Stop the gateway.
- In
SupplyAirTemperature, changeat: holding:10toat: holding:300. - 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.
- Change the address back to
holding:10.
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
-
Stop the gateway with Ctrl+C.
-
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:andscale:. - 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
checkshow.