Write several coils at once on Modbus

🟡 Intermediate

modbus variable

This example shows how to set several consecutive Modbus coils in a single request from one integer value, here to select the sound of a signal tower.

  • Register a Modbus slave device using slave.modbus.

  • Hold the sound number in an INTEGER value owned by the variables module.

  • Write it on two consecutive coils with an output.modbus using length.

  • Read both coils back with two owner.modbus to see the result.

git checkout origin/osp-modbus-configuration .
git checkout origin/osp-variables-configuration .
git checkout origin/example-modbus-write-multiple-coils .

Prerequisites

  • Modbus connector

  • Variables module

  • A Modbus device or a virtualized Modbus device like oitc/modbus-server:1.1.2

docker container run -p 5020:5020 --name=modbus-test oitc/modbus-server:1.1.2

Use case

The sound module of a signal tower has two selection inputs, Audio 0 and Audio 1, wired on coils 12 and 13 of the Modbus device. Their combination selects the sound:

Sound

Audio 0

Audio 1

None

0

0

1

1

0

2

0

1

3

1

1

Writing each coil with its own output would send two requests, so the tower could briefly play the wrong sound in between. Writing both coils from the sound number sends a single request (Modbus function 15, Write Multiple Coils) and both inputs change together.

Steps

1. Register a Modbus slave

Replace the IP by the IP of the Modbus device.

Content : root/plc/slave-1/slave.modbus

{
    "host": "10.0.2.15:5020",
    "moduleId": "modules.modbus.modbus-1",
    "readPeriod": {
        "value": 1,
        "unit": "SECONDS"
    }
}

2. Create the sound value

The sound number is a plain INTEGER owned by the variables module.

Content : root/sound/value.ospp

{
    "name": "Sound",
    "description": "Sound played by the signal tower: 0 = none, 1 to 3 = sound number",
    "type": "INTEGER"
}

Content : root/sound/owner.variables

{
    "moduleId": "modules.variables.variables-1"
}

3. Create the output on two coils

length gives the number of consecutive coils: the integer is split into bits, bit 0 goes on coil 12 (Audio 0), bit 1 on coil 13 (Audio 1). The valueType stays BOOLEAN, the type of each coil. See multiple coils.

Content : root/sound-selector/output.modbus

{
    "address": 12,
    "endianness": "BIG",
    "modbusType": "COIL",
    "slaveId": "root.plc.slave-1",
    "valueType": "BOOLEAN",
    "length": 2
}

4. Call the output when the sound changes

Content : root/sound/callback.ospp

{
    "linkedOutputs": [
        {
            "outputId": "root.sound-selector"
        }
    ]
}

5. Read the coils back

Two owner.modbus read coils 12 and 13 so that their state can be followed in OnSphere.

Content : root/audio-0/owner.modbus

{
    "address": 12,
    "endianness": "BIG",
    "modbusType": "COIL",
    "slaveId": "root.plc.slave-1",
    "valueType": "BOOLEAN"
}

Content : root/audio-1/owner.modbus

{
    "address": 13,
    "endianness": "BIG",
    "modbusType": "COIL",
    "slaveId": "root.plc.slave-1",
    "valueType": "BOOLEAN"
}

6. Push the configuration and check the result

git add .
git commit -m "Add modbus multiple coils example"
git push

Set root.sound to 3: root.audio-0 and root.audio-1 both become true. Set it to 2: root.audio-0 becomes false and root.audio-1 stays true. A value of 4 or more does not fit on two coils and is refused.

The logs of the Modbus device show a single Write Multiple Coils request per change:

docker container logs -f modbus-test