Modbus write task
Write command channel values to the coils and holding registers of a Modbus server.
A Modbus write task writes the values of command channels to the coils and holding registers of one Modbus server. For example, a button on a schematic can start a pump through a coil of a PLC. Modbus write tasks have no state channels.
Each command writes every address from the lowest to the highest address of the task. Addresses between your channels get the values that the task read at its first command, until you configure the task again. If that first read fails, they get zeros. Keep the addresses of a write task together, or use one task for each group of addresses.
The Driver writes coils with function code 15 and holding registers with function code 16. A device that supports only function codes 05 and 06 cannot be written. One request can hold at most 123 registers or 1968 coils.
Before you start, you need a connected server.
Create a Modbus write task
When you create command channels with a client library, give each one its own index channel. A writer that holds several commands on one index must write all of them in every frame.
Console
Python
TypeScript
- In the devices toolbar, right-click the server and select Create write task.
- Click Add channel. Select Coil or Holding register, then enter the address. For a holding register, select the Data type.
- Click (Start).
The Console creates a command channel and an index channel for each output. For the
server Pump skid PLC, a coil at address 3 gives Pump_skid_PLC_coil_output_3_cmd and
Pump_skid_PLC_coil_output_3_cmd_time. To use a different name, right-click the channel
and select Rename before you start the task.
Python does not create channels. Create them first.
import synnax as sy
from synnax import modbus
dev = client.devices.retrieve(name="Pump skid PLC")
cmd_time = client.channels.create(
name="pump_cmd_time", is_index=True, data_type=sy.DataType.TIMESTAMP
)
pump_cmd = client.channels.create(
name="pump_cmd", index=cmd_time.key, data_type=sy.DataType.UINT8
)
task = modbus.WriteTask(
name="Pump control",
device=dev.key,
channels=[modbus.CoilWriteChannel(address=3, channel=pump_cmd.key)],
)
client.tasks.configure(task)
task.start()TypeScript does not create channels. Create them first.
import { modbus } from "@synnaxlabs/client";
const [dev] = await client.devices.retrieve({ names: ["Pump skid PLC"] });
const cmdTime = await client.channels.create({
name: "pump_cmd_time",
isIndex: true,
dataType: "timestamp",
});
const pumpCmd = await client.channels.create({
name: "pump_cmd",
index: cmdTime.key,
dataType: "uint8",
});
const task = await client.tasks.create({
name: "Pump control",
rack: dev.rack,
type: "modbus_write",
config: modbus.writeConfigZ.parse({
device: dev.key,
channels: [{ type: "coil", address: 3, channel: pumpCmd.key }],
}),
});
await task.start();The task has the options Data saving and Auto start. See
task options. Python WriteTask has no
data_saving_disabled argument.
Channel reference
Each channel has an address, from 0 to 65535, and a channel, the command channel.
Each table shows the Console label of a setting, with its Python field under it.
Troubleshooting
A command changed a register that the task does not use
The register is between two addresses of the task. See the warning at the top of this page.
“Illegal function”
The device does not support function code 15 or 16. The Driver cannot write to it.
“Too many data”
The addresses of the task span more than one request can hold. Move the channels closer together, or split them into two tasks.
The task stopped when the connection dropped
The task stops on any connection error. Start the task again when the server is back.
For other problems, see Connect a Modbus server and Troubleshoot hardware.